rebca
rebca

Reputation: 1189

deleting lines from a text file with bash

I have two sets of text files. First set is in AA folder. Second set is in BB folder. The content of ff.txt file from first set(AA folder) is shown below.

Name        number     marks
john            1         60
maria           2         54
samuel          3         62
ben             4         63

I would like to print the second column(number) from this file if marks>60. The output will be 3,4. Next, read the ff.txt file in BB folder and delete the lines containing numbers 3,4. How can I do this with bash?

files in BB folder looks like this. second column is the number.

   marks       1      11.824  24.015  41.220  1.00 13.65 
   marks       1      13.058  24.521  40.718  1.00 11.82
   marks       3      12.120  13.472  46.317  1.00 10.62 
   marks       4      10.343  24.731  47.771  1.00  8.18

Upvotes: 2

Views: 336

Answers (2)

Eran Ben-Natan
Eran Ben-Natan

Reputation: 2615

This works, but is not efficient (is that matter?)

gawk 'BEGIN {getline} $3>60{print $2}' AA/ff.txt | while read number; do gawk -v number=$number '$2 != number' BB/ff.txt > /tmp/ff.txt; mv /tmp/ff.txt BB/ff.txt; done

Of course, the second awk can be replaced with sed -i


For multi files:

ls -1 AA/*.txt | while read file
do
    bn=`basename $file`
    gawk 'BEGIN {getline} $3>60{print $2}' AA/$bn | while read number
    do
        gawk -v number=$number '$2 != number' BB/$bn > /tmp/$bn
        mv /tmp/$bn BB/$bn
    done
done

I didn't test it, so if there is a problem, please comment.

Upvotes: 0

Dennis Williamson
Dennis Williamson

Reputation: 360095

awk 'FNR == NR && $3 > 60 {array[$2] = 1; next} {if ($2 in array) next; print}' AA/ff.txt BB/filename

Upvotes: 1

Related Questions