Modifying the output using AWK

Viewed 66

Having a data set like below shown with (:)colon delimeted:

##L1
Advc:start:TA_Wk;TA_Wk:rstb:ver:trstb.techind_C3_D[0].RESETH:LK

##L2
ner:ted:Unused:C1500:ver:sync3rmsfd0nt_t;tape_out:SOCK0

I am using the column number's and I am attaching some strings to it using AWK. And I am trying to ignore the COmments which encounter's with # and appending them at last.

#!/bin/bash
awk -F: '/^#/ {c=$0; sub(/#+/,"", c); next}
{print $1, "-f" " "$4, "-t" " "$6, "-d" " ""{"$3"}", "-t" " ""{"$7"}","-", "\"" c "\""}' s1

Output:

Advc -f rstb -t trstb.techind_C3_D[0].RESETH -d {TA_Wk;TA_Wk} -t {LK} - "L1"
 -f  -t  -c {} -c {} - "L1"
ner -f C1500 -t sync3rmsfd0nt_t;tape_out -d {Unused} -t {SOCK0} - "L2"

Desired Output:

Advc -f rstb -t trstb.techind_C3_D[0].RESETH -d {TA_Wk;TA_Wk} -t {LK} - "L1"
ner -f C1500 -t sync3rmsfd0nt_t;tape_out -d {Unused} -t {SOCK0} - "L2"

1.In column 3 and column 7 if words like are "Unused", "Unspecified" then it should not print that particular column it should ignore along with the string attached to it as shown above in desired output.

2.HOw to remove empty lines.

How to fix above two conditions in my code as shown above?

1 Answers

OP's attempt fix: Fixed OP's attempt itself here. You need to skip empty lines to print your output correctly which you could do with simple if condition + I did minor changes in your attempt like changing sub to gsub in removing # part, setting : within BEGIN section + give variables well defined name so in place of c I changed it to val for better understanding.

awk 'BEGIN{FS=":"} /^$/{next} /^#/ {val=$0; gsub(/#/,"", val); next}
{print $1, "-f" " "$4, "-t" " "$6, \
($3!="Unused"?"-d" " ""{"$3"}":""),\
 ($7!="Unspecified"?"-t" " ""{"$7"}":""),"-", "\"" val "\""
}' s1="\""  Input_file


My attempt to solve this: Written and tested with shown samples in GNU awk.

awk '
BEGIN{
 FS=":"
}
/^$/{
  next
}
/^#/{
  val=$0
  gsub(/#/,"", val)
  next
}
{
  ($3=$3!="Unused"?"-d" " ""{"$3"}":"")
  ($7=$7!="Unspecified"?"-t" " ""{"$7"}":"")
  print $1, "-f" " "$4, "-t" " "$6, \
  $3,\
  $7,"-", "\"" val "\""
}' s1="\""  Input_file
Related