View previous topic :: View next topic |
Author |
Message |
chiefbag Guru
Joined: 01 Oct 2010 Posts: 542 Location: The Kingdom
|
Posted: Tue Jan 29, 2013 11:10 am Post subject: Simple sed problem [SOLVED] |
|
|
For some reason I am having issues trying to execute a simple sed command.
What I'm trying to do is insert a line into a file after a line of a certain pattern
From the dev manual http://devmanual.gentoo.org/tools-reference/sed/index.html it gives the below example however it does not appear to work.
Anyone know what I am doing wrong or is there an easier way to do this?
The command executes without error but file remains as is.
Created a file with the following:
Command I run
Code: | sed -i -e '/^To: $/a Bob' data.in |
From the stat command I can see that sed must be attempting some change to the file.
Code: |
stat data.in
File: 'data.in'
Size: 11 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 8001109 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ XXX) Gid: ( 1000/ XXX)
Access: 2013-01-29 11:08:25.875020840 +0000
Modify: 2013-01-29 11:08:25.875020840 +0000
Change: 2013-01-29 11:08:25.875020840 +0000
Birth: -
$ sed -i -e '/^To: $/a Bob' data.in
$ stat data.in
File: 'data.in'
Size: 11 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 8001108 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ XXX) Gid: ( 1000/ XXX)
Access: 2013-01-29 11:08:44.505020914 +0000
Modify: 2013-01-29 11:08:44.505020914 +0000
Change: 2013-01-29 11:08:44.505020914 +0000
Birth: - |
Last edited by chiefbag on Tue Jan 29, 2013 11:29 am; edited 1 time in total |
|
Back to top |
|
|
tomk Bodhisattva
Joined: 23 Sep 2003 Posts: 7221 Location: Sat in front of my computer
|
Posted: Tue Jan 29, 2013 11:21 am Post subject: Re: Simple sed problem |
|
|
There is no space after the To: so the regex doesn't match, if you change it to the following it works:
Code: | sed -i -e '/^To:$/a Bob' data.in |
_________________ Search | Read | Answer | Report | Strip |
|
Back to top |
|
|
chiefbag Guru
Joined: 01 Oct 2010 Posts: 542 Location: The Kingdom
|
Posted: Tue Jan 29, 2013 11:28 am Post subject: |
|
|
Thanks
I should have continued reading the info:
Code: | Basic Atoms
Atom Purpose
text Literal text
\( \) Grouping
\| Alternation, a or b
* \? \+ \{\} Repeats, see below
. Any single character
^ Start of line
$ End of line
[abc0-9] Any one of
[^abc0-9] Any one character except
[[:alpha:]] POSIX character class, see below
\1 .. \9 Backreference
\x (any special character) Match character literally
\x (normal characters) Shortcut, see below |
|
|
Back to top |
|
|
mv Watchman
Joined: 20 Apr 2005 Posts: 6747
|
Posted: Tue Jan 29, 2013 11:34 am Post subject: Re: Simple sed problem [SOLVED] |
|
|
chiefbag wrote: | Code: | sed -i -e '/^To: $/a Bob' data.in |
|
You require an exact match (start of line, "T", "o", ":", " ", end of line). I suppose in your file the space (" ") before he end of the line is missing, and so the test fails for all of your lines. |
|
Back to top |
|
|
|