Hello All,
I have following file contents
cat file
#line=aaaaaa
#line=bbbbbb
#line=cccccc
#line=dddddd
line=eeeeee
#comment=11111
#comment=22222
#comment=33333
#comment=44444
comment=55555
Testing script
Good Luck!
I would like to comment line
line=eeeeee
and insert a new line
line=ffffff
immediate after this.
I wrote following command to comment my line:
sed 's/\(^line=*\)/#\1/' manish
And Google for inserting a new line and found following solution:
sed '/^line=/ a\
line=ffffff' manish
- I tried to combine both commands in a single command using �|� but I am not able to get it correctly. Can anyone please help me to achieve this objective?
- 2nd command to insert a new line is divided in 2 lines (using �\�). I tried several option to make it in a single line but every time it gives me some error. Can you please help me to combine 2nd command in a single line?
Output should look like:
cat file
#line=aaaaaa
#line=bbbbbb
#line=cccccc
#line=dddddd
#line=eeeeee
line=ffffff
#comment=11111
#comment=22222
#comment=33333
#comment=44444
comment=55555
Testing script
Good Luck!
Thank you.
Manish
Try:
sed 's/\(^line=.*\)/#\1\nline=ffffff/' manish
bartus11,
Many thanks for your reply.
It commented the required line but inserted the line in the commented line not in next line.
sed 's/\(^line=.*\)/#\1\nline=ffffff/' manish
#line=aaaaaa
#line=bbbbbb
#line=cccccc
#line=dddddd
#line=eeeeeenline=ffffff
#comment=11111
#comment=22222
#comment=33333
#comment=44444
comment=55555
Testing script
Good Luck!
My requirment is to get line as:
#line=eeeeee
line=ffffff
Try this:
perl -pe 's/^(line=.*)/#$1\nline=ffffff/' manish
1 Like
\n
is not a standard sed feature in the replacement part of the substitute command. Try:
sed 's/^line=.*/#&\
line=ffffff/' file
It is giving following error:
sed: 0602-404 Function s/^line=.*/#&\
line=ffffff cannot be parsed.
Try:
sed 's/^line=.*/#&\
line=ffffff/' file
1 Like
I left out a trailing slash. Try:
sed 's/^line=.*/#&\
line=ffffff/' file
--edit--
Don Cragun beat me to it
1 Like
Hello,
Once I insert a new line then I would like to put some comment in front of that line.
I am able to achieve this using following separate command:
sed 's/\(^line=.*\)/\1 #Code Change Comment/' manish
But I wonder if I can achieve this within previous command. I did try options but not succeeded.
Please help me if there is any way to get this.
Thanks.
Hi, I am not sure what you mean, one of these?
$ sed 's/^line=.*/#&\
line=ffffff # code change/' file
#line=aaaaaa
#line=bbbbbb
#line=cccccc
#line=dddddd
#line=eeeeee
line=ffffff # code change
#comment=11111
#comment=22222
#comment=33333
#comment=44444
comment=55555
$ sed 's/^line=.*/#& #code change\
line=ffffff/' file
#line=aaaaaa
#line=bbbbbb
#line=cccccc
#line=dddddd
#line=eeeeee #code change
line=ffffff
#comment=11111
#comment=22222
#comment=33333
#comment=44444
comment=55555
Yes. Got it!! Thanks.
sed 's/^line=.*/#&\
line=$restoreufs $comment/' manish
where
restoreufs="line=ffffff"
comment="This comment line"
When I am trying to read restoreufs & comment variable values then it is not able to expand $restoreufs and $comment values
Output:
#line=eeeeee
line=$restoreufs $comment
#comment=11111
I tried to use "double cotes" and "eval" then instead of inserting a new line it is appending data in existing line
sed "s/^line=.*/#&\
line=$restoreufs $comment/" manish
OR
eval "sed 's/^line=.*/#&\
line=$restoreufs $comment/' manish"
Output:
#line=dddddd
#line=eeeeeeline=line=ffffff This comment line
#comment=11111
Que: How to display the variable values correctly in new line.
My actual command in script is:
sed 's/^restoredb2=.*/#&\
restoredb2=$restoredb2_value # $comment/' /home/script/restore_$(basename $PWD).sh
Try combining single quotes and double quotes, for example:
sed 's/^line=.*/#&\
'"$restoreufs # $comment/" file
or use an extra escape for the escape character itself:
sed "s/^line=.*/#&\\
$restoreufs # $comment/" file
Try it like this:
restoreufs="line=ffffff"
comment="This comment line"
sed "s/^line=.*/#&\
\n${restoreufs} ${comment}/" file
$ test.sh
#line=aaaaaa
#line=bbbbbb
#line=cccccc
#line=dddddd
#line=eeeeee
line=ffffff This comment line
#comment=11111
#comment=22222
#comment=33333
#comment=44444
comment=55555
sed "s/^restoredb2=.*/#&\\
restoredb2=$restoredb2_value # $comment/" restore_$(basename $PWD).sh
I am using above command to look for a line starting with word "restoredb2=" and commenting it. Also, I am inserting new data line in the immediate next line.
Issue: When I am running above command then it is not able to read a line starting with space and followed by my search string.
For example::
Able to read:
restoredb2=testing12345
But not able to read
restoredb2=testing12345 (Please note initial 2 spaces)
Please help me to modify command to work similarly in case it find search string directly or find it with leading spaces.
Thank you.
Well the ^ means beginning of the line, so you could try something like this:
sed "s/^ *restoredb2...
is there are no TABs
Thank you very much for your help.
This worked fine for spaces.
I have not been in situation to deal with TAB but it's a good idea to put a logic for TAB as well.
I tried following option to deal with tab and space at same time but no luck:
sed "s/^ *\t*restoredb2=.*/#&\\
restoredb2=$restoredb2_value # $comment/" restore_$(basename $PWD).sh
and
sed "s/^ *\\t*restoredb2=.*/#&\\
restoredb2=$restoredb2_value # $comment/" restore_$(basename $PWD).sh
Still it is reading SPACE but not TAB.
It would be great if you can help me in this as well.
Some sed's understand \t
, so then you can use:
s/^[ \t]*restoredb....
otherwise instead of \t
you could enter and actual TAB character, using Ctrl-V - TAB
But I would use this:
sed 's/^[[:space:]]*restoredb...
1 Like
\t
did not work with my sed.
Following options ran successfully:
sed "s/^[ ]*restoredb2=.*/#&\\ # [] consists of one SPACE & one TAB character.
restoredb2=$restoredb2_value # $comment/" restore_$(basename $PWD).sh
sed "s/^[[:space:]]*restoredb2=.*/#&\\
restoredb2=$restoredb2_value # $comment/" restore_$(basename $PWD).sh
It would be really thankful if you can share the significance of [[:space:]]
character.
The character class space
contains the following characters in the POSIX and C locales: <space>, <tab>, <form-feed>, <carriage-return>, <newline>, and <vertical-tab>. For what you are doing here, I would tend to use the character class blank
which only contains the <space> and <tab> characters rather than the character class space
:
sed "s/^[[:blank:]]*restoredb2=.*/#&\\
restoredb2=$restoredb2_value # $comment/" restore_$(basename $PWD).sh
1 Like
With awk, taking space into account wouldn't be needed because awk does this implicitly with the default field separator:
awk '$1~/^line=/{$0="# "$0 RS s}1' s="$restoreufs # This comment line" file