I have a file containing several numbers in a column: (numbers.txt)

2
5
126
3005
65

there is another text file which is somehow like this: (input.txt)

#    126    2    0
bla    mjnb     kjh    ojj
#    5    65    0
kjh    jhgg    kjhkjh    juh
hgj    ikaw    esd     cdqw
#    100    3005    0
jhgjh    jh    jhjhg    pol

the firs lines are important. Both of two numbers that are written after # should be in number.txt,Ihave written following code but it takes weeks for my huge file.my numbers.txt contains about 2500 number.

    #!/bin/bash
    cat numbers.txt | while read first
    do
    for second in $(cat numbers.txt)
    do
    awk -v RS="#" "/ $first    $second / {sub(/^ /,RS);print}"input.txt >> output1.txt
    done
    done

output should be:

#    126    2    0
bla    mjnb     kjh    ojj
#    5    65    0
kjh    jhgg    kjhkjh    juh
hgj    ikaw    esd     cdqw

can someone please offer a faster way to reach the output?

有帮助吗?

解决方案

awk '
    # read the numbers file into the array "num"
    NR == FNR {num[$1]; next} 

    # if this is a "#" line and the first 2 numbers are in "num" set a flag to "true"
    $1 == "#" {p = (($2 in num) && ($3 in num))} 

    # print the current line if the flag is true
    p
' numbers.txt input.txt 

其他提示

Using awk you can do without creating nested loops:

awk 'FNR==NR{a[$0];next} $1=="#" && ($2 in a) && ($3 in a) {p=1}
           $1=="#" && (!($2 in a) || !($3 in a)) {p=0} p' file1 file2
#    126    2    0
bla    mjnb     kjh    ojj
#    5    65    0
kjh    jhgg    kjhkjh    juh
hgj    ikaw    esd     cdqw
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top