unix - How To Run Multiple "awk" commands: -
would run multiple "awk" commands in single script .. example master.csv.gz located @ /cygdrive/e/test/master.csv.gz , input files located in different sub directories /cygdrive/f/jan/input_jan.csv.gz & /cygdrive/f/feb/input_feb.csv.gz , on .. input files *.gz extension files.
below commands working fine while executing command 1 one:
command#1
awk ' begin {fs = ofs = ","} fnr==nr {a[$2] = $0; next} ($2 in a) {print $0}' <(gzip -dc /cygdrive/e/test/master.csv.gz) <(gzip -dc /cygdrive/f/jan/input_jan.csv.gz) >>output.txt
output#1:
name,age,location abc,20,xxx
command#2
awk ' begin {fs = ofs = ","} fnr==nr {a[$2] = $0; next} ($2 in a) {print $0}' <(gzip -dc /cygdrive/e/test/master.csv.gz) <(gzip -dc /cygdrive/f/feb/input_feb.csv.gz) >>output.txt
output#2:
name,age,location def,40,yyy
cat output.txt
name,age,location abc,20,xxx def,40,yyy
have tried below commands run in via single script , got error:
attempt#1: awk -f test.awk
cat test.awk
awk ' begin {fs = ofs = ","} fnr==nr {a[$2] = $0; next} ($2 in a) {print $0}' <(gzip -dc /cygdrive/e/test/master.csv.gz) <(gzip -dc /cygdrive/f/jan/input_jan.csv.gz) >>output.txt awk ' begin {fs = ofs = ","} fnr==nr {a[$2] = $0; next} ($2 in a) {print $0}' <(gzip -dc /cygdrive/e/test/master.csv.gz) <(gzip -dc /cygdrive/f/feb/input_feb.csv.gz) >>output.txt
error : attempt#1: awk -f test.awk
awk: test.awk:1: ^ invalid char ''' in expression awk: test.awk:1: ^ syntax error
attempt#2: sh test.sh
cat test.sh
#!/bin/sh awk ' begin {fs = ofs = ","} fnr==nr {a[$2] = $0; next} ($2 in a) {print $0}' <(gzip -dc /cygdrive/e/test/master.csv.gz) <(gzip -dc /cygdrive/f/jan/input_jan.csv.gz) >>output.txt awk ' begin {fs = ofs = ","} fnr==nr {a[$2] = $0; next} ($2 in a) {print $0}' <(gzip -dc /cygdrive/e/test/master.csv.gz) <(gzip -dc /cygdrive/f/feb/input_feb.csv.gz) >>output.txt
error : attempt#2: sh test.sh
test.sh: line 2: syntax error near unexpected token `('
desired output:
name,age,location abc,20,xxx def,40,yyy
looking suggestions ..
update#2-month name ed morton, inputs, output order not proper , "jan2014" print on next line , please suggest
cat output.txt:
name,age,location abc,20,xxx jan2014 def,40,yyy feb2014
expected output
name,age,location abc,20,xxx,jan2014 def,40,yyy,feb2014
all need is:
#!/bin/bash awk -f, 'fnr==nr{a[$2]; next} $2 in a' \ <(gzip -dc /cygdrive/e/test/master.csv.gz) \ <(gzip -dc /cygdrive/f/jan/input_jan.csv.gz) \ <(gzip -dc /cygdrive/f/feb/input_feb.csv.gz) \ >> output.txt
if want print month name simplest thing be:
#!/bin/bash awk -f, 'fnr==nr{a[$2]; next} $2 in a{print $0, mth}' \ <(gzip -dc /cygdrive/e/test/master.csv.gz) \ mth="jan" <(gzip -dc /cygdrive/f/jan/input_jan.csv.gz) \ mth="feb" <(gzip -dc /cygdrive/f/feb/input_feb.csv.gz) \ >> output.txt
but remove redundant specifying of month name 3 times on each line with:
#!/bin/bash mths=(jan feb) awk -f, 'fnr==nr{a[$2]; next} $2 in a{print $0, mth}' \ <(gzip -dc /cygdrive/e/test/master.csv.gz) \ mth="${mths[$((i++))]}" <(gzip -dc "/cygdrive/f/${mths[$i]}/input_${mths[$i]}.csv.gz") \ mth="${mths[$((i++))]}" <(gzip -dc "/cygdrive/f/${mths[$i]}/input_${mths[$i]}.csv.gz") \ >> output.txt
Comments
Post a Comment