Legends,
I need to append the file. please help me out.
Current contents are
/tmp/a.txt
#
e11.Mer.out.host1 "fun.tp" read
e11.Mer.out.host2 "fun1.tp" read
e11.Mer.out.host3 "fun2.tp" read
e11.Mer.out.host4 "fun3.tp" read
e11.Mer.out.host5 "fun3.tp" read
#
Now i want to add additional line as below
#
e11.Mer.out.host1 "fun.tp" read
e11.Mer.out.host2 "fun1.tp" read
e11.Mer.out.host3 "fun2.tp" read
e11.Mer.out.host4 "fun3.tp" read
e11.Mer.out.host5 "fun4.tp" read
e11.Mer.out.host6 "fun5.tp" read
#
e12.Mer.out.host1 "fun.tp" read
e12.Mer.out.host2 "fun1.tp" read
e12.Mer.out.host3 "fun2.tp" read
e12.Mer.out.host4 "fun3.tp" read
e12.Mer.out.host5 "fun4.tp" read
e12.Mer.out.host6 "fun5.tp" read
#
gull04
September 23, 2014, 4:10am
2
Hi,
To do something like this you just have to edit the file, not sure what editor you'll have but you can use Vi, Vim, Emacs, Gedit or any number of editors to do that.
Regards
Dave
sdosanjh:
Legends,
I need to append the file. please help me out.
Current contents are
/tmp/a.txt
#
e11.Mer.out.host1 "fun.tp" read
e11.Mer.out.host2 "fun1.tp" read
e11.Mer.out.host3 "fun2.tp" read
e11.Mer.out.host4 "fun3.tp" read
e11.Mer.out.host5 "fun3.tp" read
#
Now i want to add additional line as below
#
e11.Mer.out.host1 "fun.tp" read
e11.Mer.out.host2 "fun1.tp" read
e11.Mer.out.host3 "fun2.tp" read
e11.Mer.out.host4 "fun3.tp" read
e11.Mer.out.host5 "fun4.tp" read
e11.Mer.out.host6 "fun5.tp" read
#
e12.Mer.out.host1 "fun.tp" read
e12.Mer.out.host2 "fun1.tp" read
e12.Mer.out.host3 "fun2.tp" read
e12.Mer.out.host4 "fun3.tp" read
e12.Mer.out.host5 "fun4.tp" read
e12.Mer.out.host6 "fun5.tp" read
#
Hello,
Following may help in same.
awk '/^#/ {a++;print $0;getline;u=$0;match($1,/.*host/);c=substr($1,RSTART,RLENGTH);match($2,/.*\./);e=substr($2,RSTART,RLENGTH-1);match($2,/\..*/);f=substr($2,RSTART,RLENGTH);$1=$2="";V=$0;print u} !/^# && a/ {b++} {if(b>1 &&a==1){print $0}} {if(b==5){a=1;b;c=c b;e=e b f;print c OFS e OFS V;c=V=b=e=""}}' FS=" " OFS="\t" Input_file
Output will be as follows.
#
e11.Mer.out.host1 "fun.tp" read
e11.Mer.out.host2 "fun1.tp" read
e11.Mer.out.host3 "fun2.tp" read
e11.Mer.out.host4 "fun3.tp" read
e11.Mer.out.host5 "fun3.tp" read
e11.Mer.out.host5 "fun5.tp" read
#
Thanks
R. Singh
If you want to add the same Mer.out.host6 "fun5.tp" read
everytime with first value different, you can use below
awk 'NF > 1 {p=$1; print; next} NR > 1{print p, "Mer.out.host6 \"fun5.tp\" read"}1' FS=. OFS=. /tmp/a.txt
awk 'FNR==1{gsub(/,/,OFS,latest)}/^#/ && p==3{print latest }{p=NF; $1=$1}1' OFS='\t' latest='e12.Mer.out.host6,"fun5.tp",read' file
srinishoo:
If you want to add the same Mer.out.host6 "fun5.tp" read
everytime with first value different, you can use below
awk 'NF > 1 {p=$1; print; next} NR > 1{print p, "Mer.out.host6 \"fun5.tp\" read"}1' FS=. OFS=. /tmp/a.txt
Thx Srini,
but it is giving the syntax error
awk: syntax error near line 1
awk: bailing out near line 1
awk 'FNR==1{gsub(/,/,OFS,latest)}/^#/ && p==3{print latest }{p=NF; $1=$1}1' OFS='\t' latest='e12.Mer.out.host6,"fun5.tp",read' file
Thx Akshay,
but i am getting syntax error
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
If you are on Solaris/SunOS
system, change awk
at the start of the script to /usr/xpg4/bin/awk
or /usr/xpg6/bin/awk
or nawk