Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk: Using invert match to a string and then substitute characters

Tags:

awk

I want to extract lines that don't contain # and delete ", ; in the output.

My input FILE looks like this:

# ;string"1"
# string"2"; 
string"3";

Can use grep and tr to get wanted output:

grep -v '#' FILE | tr -d ';"'  
string3

However I want to use awk.

I can extract invert match awk '!/#/' FILE, but how can I use sub to delete ", ; in the same awk command?

like image 797
pogibas Avatar asked Sep 22 '13 07:09

pogibas


2 Answers

You can use gsub for global substitution:

awk '!/#/{gsub(/[";]/,"",$0);print}'

The following transcript shows this in action, it delivers the same results as your grep/tr pipeline:

pax> echo '# ;string"1"
# string"2"; 
string"3";' | awk '!/#/{gsub(/[";]/,"",$0);print}{}'

string3

Note that the final {} may not be necessary in some implementations of awk but it's there to stop output of non-matching lines in those implementations (usually older ones) that do it automatically for lines matching none of the rules.

like image 161
paxdiablo Avatar answered Oct 07 '22 22:10

paxdiablo


Use gsub instead which would replace all matches not just one:

awk  '/#/{next}{gsub(/[";]/,"")}1' file

Output:

string3
  • Skipping the third parameter to gsub makes it process $0 by default.
  • /#/{next} makes it skip lines containing #
  • 1 makes it print $0
like image 29
konsolebox Avatar answered Oct 07 '22 21:10

konsolebox