Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Simple sed problem [SOLVED]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
chiefbag
Guru
Guru


Joined: 01 Oct 2010
Posts: 542
Location: The Kingdom

PostPosted: Tue Jan 29, 2013 11:10 am    Post subject: Simple sed problem [SOLVED] Reply with quote

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:

Code:

itest
To:


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
View user's profile Send private message
tomk
Bodhisattva
Bodhisattva


Joined: 23 Sep 2003
Posts: 7221
Location: Sat in front of my computer

PostPosted: Tue Jan 29, 2013 11:21 am    Post subject: Re: Simple sed problem Reply with quote

chiefbag wrote:
Code:

itest
To:


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
View user's profile Send private message
chiefbag
Guru
Guru


Joined: 01 Oct 2010
Posts: 542
Location: The Kingdom

PostPosted: Tue Jan 29, 2013 11:28 am    Post subject: Reply with quote

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
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Tue Jan 29, 2013 11:34 am    Post subject: Re: Simple sed problem [SOLVED] Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum