Deleting comments from fields

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting comments from fields
# 8  
Old 04-21-2014
i have about 1000 files, in which first 5 lines are comments, rest are simple assignments.
i have to remove comments in rest of the file , leaving first 5 lines as same.
// name: xyz
// age : b
// sex :male
color=black      //not confirmed
profession=farmer //more specification needed

i have to remove comments in rest of the file, keeping first 5 comments as it is, as those are desired comments. same thing i have to do in 1000 more files, how can i do this using shell scripts or sed/awk command , or any way possible.

Please help.
# 9  
Old 04-21-2014

This should do the job;
sed '6,$s;//.*;;' < input_file > output_file



Last edited by gull04; 04-21-2014 at 08:22 AM.. Reason: Typo
# 10  
Old 04-21-2014
Thanx a lot.

i sat whole day and wrote below script

echo cfg_name=$cfg
sed 5q $cfg > cfg_header 
sed -i 5d $cfg
sed -i 's/ *\/\/.*//;s/:/ /g' $cfg
sed -i '/^$/d' $cfg
cat $cfg >> cfg_header
rm -rf $cfg
mv cfg_header $cfg

i shifted first line, in header file, then used sed command to remove rest of the comments and blank lines.
but ur command does that in a single line.
sed '6,$s;//.*;;' < input_file > output_file

# 11  
Old 04-21-2014

You are welcome, every little contribution helps.


# 12  
Old 09-03-2014
Posted by tsu3000:



I have this sample data set as follows (called file.txt):

hostname1:user1Smilieasswordhostname2:user1Smilieassword #comments commentshostname3:user1Smilieassword

I wish to produce a report as follows:

hostname1 user1 passwordhostname2 user1 passwordhostname3 user1 password

ie remove all trailing whitespace and comments.

So far I have tried this and it works:

awk -F":" '{print $1,$2,$3}' file.txt | awk '{print $1,$2,$3}'

I was wondering if there is a more elegant way of programming this (without the pipe). Does awk allow you to change the FS midway for example?

Thanks in advance.


One more approach for same.

awk 'NF>1 {for(i=1;i<=(NF-i);i++){$NF=$(NF-i)=""}} {gsub(/[[:punct:]]/," ",$0)}1'  filename

Output will be as follows.

hostname1 user1 password
hostname2 user1 password
hostname3 user1 password

R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

3. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

4. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

5. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

6. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

7. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

8. Shell Programming and Scripting

Deleting comments from c-file

hii all, i am writing a shell script to remove comments from a .c/.cpp file. i have written script as the above script file deletes line between /* and */ also lines starting with //. but the problems are : 1) i dont want to delete the content between /** and */. 2)sed -i... (16 Replies)
Discussion started by: yashwantkumar
16 Replies

9. Shell Programming and Scripting

Sed script, changing all C-comments to C++-comments

I must write a script to change all C++ like comments: // this is a comment to this one /* this is a comment */ How to do it by sed? With file: #include <cstdio> using namespace std; //one // two int main() { printf("Example"); // three }//four the result should be: (2 Replies)
Discussion started by: black_hawk
2 Replies

10. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies
Login or Register to Ask a Question