awk with or condition

Viewed 82

I have a file with a word Sweden in different variations.

I am trying to get if 34th column has Sweden there

awk -F\" '$34 ~ /Sweden/  {print $0}' $ipp >> sweden.csv &
awk -F\" '$34 ~ /sweden/  {print $0}' $ipp >> sweden.csv &
awk -F\" '$34 ~ /SWEDEN/  {print $0}' $ipp >> sweden.csv &
awk -F\" '$34 ~ /^se$/  {print $0}' $ipp >> sweden.csv &
awk -F\" '$34 ~ /^Se$/  {print $0}' $ipp >> sweden.csv &
awk -F\" '$34 ~ /^SE$/  {print $0}' $ipp >> sweden.csv &

As far as i know it gonna be so slow, as I have 650 million rows.

Is there any way I can get all variation in 1 awk command?

6 Answers

You can use this awk:

awk -F\" 'tolower($34) ~ /sweden|^se$/' "$ipp" >> sweden.csv 

With your shown samples, attempts please try following awk code. Simply making field separator as " and in main block checking if field 34th is either containing sweden(including upper and lower cases to match any kind of combinations of it) OR it starts from se9with both lower and upper case for letters) if any of the condition passes then print that line.

awk -F\" '$34 ~ /[Ss][Ww][Ee][Dd][Ee][Nn]|^[Ss][Ee]$/' "$ipp" >> sweden.csv

If you're using GNU awk, you can use IGNORECASE option:

awk -F\" 'BEGIN{IGNORECASE=1} $34 ~ /sweden|^se$/' "$ipp" >> sweden.csv

Your code might be ameloriated as already explained, more generally you might put 6 pattern-action pairs in single awk call rather than 6 separate that is

awk -F\" '$34 ~ /Sweden/  {print $0}' $ipp >> sweden.csv &
awk -F\" '$34 ~ /sweden/  {print $0}' $ipp >> sweden.csv &
awk -F\" '$34 ~ /SWEDEN/  {print $0}' $ipp >> sweden.csv &
awk -F\" '$34 ~ /^se$/  {print $0}' $ipp >> sweden.csv &
awk -F\" '$34 ~ /^Se$/  {print $0}' $ipp >> sweden.csv &
awk -F\" '$34 ~ /^SE$/  {print $0}' $ipp >> sweden.csv &

might be written more concisely as

awk -F\" '$34 ~ /Sweden/  {print $0}$34 ~ /sweden/  {print $0}$34 ~ /SWEDEN/  {print $0}$34 ~ /^se$/  {print $0}$34 ~ /^Se$/  {print $0}$34 ~ /^SE$/  {print $0}' $ipp >> sweden.csv &

Note that if line does contain both Sweden and SWEDEN it will appear twice (in 6 x awk and 1 x awk solution) and also order of lines in output might be different between these 2 approaches.

awk 'sub(/^[sS][eE]$|^[Ss]weden$|^SWEDEN$/,$4,$4)' "$ipp" >> sweden.csv 
mawk '$34~__'                           FS='[\"]'\
          __="^[Se][Ee]|[Ss][Ww][Ee][Dd][Ee][Nn]|"\
          "([Kk][Oo][Nn][Uu][Nn][Gg][Aa][Rr][Ii]"  \
           "[Kk][Ee][Tt] +)?[Ss][Vv][Ee][Rr][Ii][Gg][Ee]" "$ipp" >> sweden.csv  

Between the 2-letter country code and its name in 2 languages this should be at least somewhat comprehensive.

Related