tsu3000
February 13, 2014, 7:13am
1
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
Yoda
February 13, 2014, 8:20am
4
OR
awk -F'[: ]' '{NF=3}1' file
OR
$ awk -F':|#.*' '{$1=$1}1' file
24ajay
February 14, 2014, 2:42am
6
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
RudiC
February 14, 2014, 4:55pm
7
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
gull04
April 21, 2014, 7:21am
9
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
gull04
April 21, 2014, 11:27am
11
Hi,
You are welcome, every little contribution helps.
Regards
Gull04
Posted by tsu3000:
Hi
I have this sample data set as follows (called file.txt):
Code:
hostname1:user1:passwordhostname2:user1:password #comments commentshostname3:user1:password
I wish to produce a report as follows:
Code:
hostname1 user1 passwordhostname2 user1 passwordhostname3 user1 password
ie remove all trailing whitespace and comments.
So far I have tried this and it works:
Code:
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.
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