Deleting comments from fields

Hi

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

hostname1:user1:password
hostname2:user1:password   #comments comments
hostname3:user1:password

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.

Sed is a more appropriate tool for this task in my opinion:

sed 's/ *#.*//;s/:/ /g' file.txt

Or:

awk -F"[: ]" '{print $1,$2,$3}' file

OR

awk -F'[: ]' '{NF=3}1' file

OR

$ awk -F':|#.*' '{$1=$1}1' file
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

Try also

awk -F: '{gsub(/#.*$|:/," ")}1' file 

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
//
height=156cm
color=black      //not confirmed
chest=77cm
profession=farmer //more specification needed
weight=70kg

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.
Thanks

Hi,

This should do the job;

sed '6,$s;//.*;;' < input_file > output_file

Regards

Gull04

Thanx a lot.

i sat whole day and wrote below script

#!/bin/sh

cfg=$1
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

Hi,

You are welcome, every little contribution helps.

Regards

Gull04

Posted by tsu3000:

Hello,

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

Thanks,
R. Singh