1

My file, aaa,has 5 lines.

01_AAA-11-ID22
02_BBB-33-ID44
03_CCC-55-ID66
.
.

I tried to grep anything after underline from a file bigfile.txt

for i in $(cat aaa)
do
A= $(awk -F '_' '{print $1}' $i)
B= $(awk -F '_' '{print $2}' $i)
grep $B bigfile.txt > $A
done

I tried to do

grep AAA-11-22 bigfile.txt > 01

But, it seems not working. My error code is

awk: cmd. line:1: fatal: cannot open file `01_AAA-11-22' for reading (No such file or directory)

Need some advise. Thanks,

jlliagre
  • 61,204
TJ Wu
  • 113

3 Answers3

3

echo $i instead of trying to open it as a file:

for i in $(cat aaa)
do
  A= $(echo $1 | awk -F '_' '{print $1}')
  B= $(echo $i | awk -F '_' '{print $2}')
  grep $B bigfile.txt > $A
done

However, if you are interested, you could replace this for-loop entirely with an awk one-liner:

awk -F '_' '{system("grep "$2" bigfile.txt > "$1)}' aaa
Kira
  • 4,807
  • I tried the one-liner but it does not work. But, the first one works good. Thanks. – TJ Wu Jan 14 '16 at 21:35
  • I've tested the one-liner myself, probably it happened due to different awk versions (I'm using mawk). I'm glad the answer helped you anyway. – Kira Jan 14 '16 at 21:54
1

You need not to use awk at all

while read i
do
    grep ${i#*_} bigfile.txt > ${i%%_*}
done <aaa

For GNU sed

sed 's/\([^_]\+\)_\(.*\)/sed "\/\2\/!d" bigfile.txt >"\1"/e' aaa

For awk (if you'd like)

awk -F_ '
    NR == FNR{
        A[$2] = $1
        next
    }
    {
        for(a in A)
            if($0 ~ a)
                print > A[a]
    }
    ' aaa bigfile.txt

For big files you will have to use

                print >> A[a]
                close(A[a])
Costas
  • 14,916
  • I really liked your trick to process the two files separately but in the same script. – Kira Jan 15 '16 at 15:43
0

You'll have to use echo to pass the $i to awk. Otherwise it looks for a file.

A=$(echo $i | awk -F_ '{print $1}')

Same with B

Munir
  • 3,332