Remove field to get the value using awk or sed Remove field to get the value using awk or sed unix unix

Remove field to get the value using awk or sed


try following awk and let me know if this helps you.

awk '/Name/{gsub(/\"|\,/,"",$2);val=$2;next} /count/{print val " , " $2}'  Input_file

OR

awk -F'[",]' '/Name/{val=$2;next} /count/{split($0, a," ");print val,a[2]}' OFS=" , "  Input_file


If you are using an awk that supports regular expression RS (at least gawk and mawk), you can do it like this:

awk '!(NR%2) { print $3 " , " $5  }' RS='\\ *{|}' FS='[\n," ]+' infile

Output:

ABC , 378DEF , 5283BCD , 152244XYZ , 56881A2B , 1749132


Here's another awk thought you may apply,

$ awk '$2~/[0-9A-Z]/ {printf gsub(/"/,"",$2)?$2:" "$2"\n"}' file

Brief explanation,

  • $2~/[0-9A-Z]/: find the record matched regex [0-9A-Z]
  • gsub(/"/,"",$2): remove " in the $2, and then print it