Uncommenting the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Uncommenting the file
# 1  
Old 06-14-2012
Uncommenting the file

hi i have a file in the below fomat
Code:
######################################
# 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
Code:
######################################
# some data
# .....
######################################
file value 
param1 
param2

I have used
Code:
sed -e 's/^#[ ]*//g' <file name>

but this uncommenting the header also and giving me

Code:
######################################
 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

Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by zaxxon; 06-14-2012 at 07:52 AM.. Reason: code tags, see PM
# 2  
Old 06-14-2012
Quote:
Originally Posted by midhun19
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
Code:
sed '6,$s/<pattern>/<replace_string>/g' inputfile

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 06-14-2012
Hi midhun19,

One way using perl assuming the file has several headers:
Code:
$ 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

# 4  
Old 06-14-2012
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
Code:
grep -n "file value" <file name>

but this giving me a line with
Code:
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
Code:
sed '6,$s/^#[ ]*//g'

when i give
Code:
sed'$number,$s/^#[ ]*//g' ..is failing


Last edited by Franklin52; 06-14-2012 at 08:44 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 06-14-2012
Try...
Code:
awk '/^[^#]/{f=1}f{sub(/^# */,"")};1' file

# 6  
Old 06-14-2012
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:

Code:
:6,$s/^#//

Then if you don't like what it did:
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

3. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies
Login or Register to Ask a Question