Deleting comments from fields

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting comments from fields
# 1  
Old 02-13-2014
Deleting comments from fields


I have this sample data set as follows (called file.txt):
hostname2:user1:password   #comments comments

I wish to produce a report as follows:
hostname1 user1 password
hostname2 user1 password
hostname3 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.
# 2  
Old 02-13-2014
Sed is a more appropriate tool for this task in my opinion:
sed 's/ *#.*//;s/:/ /g' file.txt

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-13-2014
awk -F"[: ]" '{print $1,$2,$3}' file

# 4  
Old 02-13-2014
awk -F'[: ]' '{NF=3}1' file

# 5  
Old 02-13-2014

$ awk -F':|#.*' '{$1=$1}1' file

This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 02-14-2014
awk -F" " '/#comments comments/ {gsub("#comments comments", "    ")}1' file

---------- Post updated at 02:42 AM ---------- Previous update was at 02:38 AM ----------

awk -F"[: ]" ' {gsub("#comments comments", " ")} { print $1,$2,$3}' file

Last edited by Franklin52; 02-14-2014 at 05:17 AM.. Reason: Please use code tags
# 7  
Old 02-14-2014
Try also
awk -F: '{gsub(/#.*$|:/," ")}1' file

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