Uncommenting the file

hi i have a file in the below fomat

######################################
# some data
# .....
######################################
file value 
# param1 
# param2

I want to remove the comment of the parameters but not the header...i want the file in the below format

######################################
# some data
# .....
######################################
file value 
param1 
param2

I have used

sed -e 's/^#[ ]*//g' <file name>

but this uncommenting the header also and giving me

######################################
 some data
.....
 ######################################
 file value 
 param1 
param2

is there any way to start replace only after the line after "file value" ie ,if i get the file value line number (in this case 5), can we ask the sed to start replacing only after 5 th line till the end of the file

sed '6,$s/<pattern>/<replace_string>/g' inputfile
1 Like

Hi midhun19,

One way using perl assuming the file has several headers:

$ cat infile
######################################
# some data
# .....
######################################
file value
# param1
# param2
######################################
# some data
# .....
######################################
file value
# param1
# param2
######################################
# some data
# .....
######################################
file value
# param1
# param2
######################################
# some data
# .....
######################################
file value
# param1
# param2
$ perl -lpe 'if ( m/\A#+\z/ ) { ++$header; next } if ( $header % 2 == 0 ) { s/\A[#\s]*// }' infile
######################################
# some data
# .....
######################################
file value
param1
param2
######################################
# some data
# .....
######################################
file value
param1
param2
######################################
# some data
# .....
######################################
file value
param1
param2
######################################
# some data
# .....
######################################
file value
param1
param2

i do not have perl or else perl command was a good solution here

Thanks balajesuri...the sed is working

but for getting the line number of the pattern "file value" ...im using

grep -n "file value" <file name>

but this giving me a line with

5:file value

from that i need to cut the line number, is there any way i can get line number using single command

---------- Post updated at 06:37 AM ---------- Previous update was at 06:07 AM ----------

is there a way to parameterise the number to be passed in the sed, like in

sed '6,$s/^#[ ]*//g'

when i give

sed'$number,$s/^#[ ]*//g' ..is failing

Try...

awk '/^[^#]/{f=1}f{sub(/^# */,"")};1' file

I know this is not a sed solution but in vim you can do a range based search replace. That way you can see what you have done and then undo if you don't want to keep that change..

open file in vim and do the following:

:6,$s/^#//

Then if you don't like what it did:

u

To explain...

The ":" places you in command mode in vi/vim. The "6,$" is telling vi/vim to start at line "6" and end at "$" (end of file). Then the "s/^#//" tells vi/vim that we are doing a search/replace operation. Searching for "#" at the beginning of the line (^) and then in the replace side we leave blank to remove the "#" if found.

Just like sed, "s/<search>/<replace>/" uses regex. You can use regex patterns as normal in the search side. It even supports keeping part of the found string as in sed. Use "(" and ")" in the search and "\1" in the replace.

The undo is easy... simply make sure you are not in command mode by hitting the "ESC" key and then hit "u" to undo your changes (Ctrl+r for redo).

Cheers