Vonton
Vonton

Reputation: 3334

Find same name of text file like in the first column and print text if condition is true

I have one text file and folder with another text files, delimited by tab. I would like to use condition if value smaller than 0 print specific text to another file with another condition.

For example:

  1. file:
112   0.6
116  -0.3
198  -0.7
205   0.4
  1. folder, where is 4 files with same name like in 1. file in first column.
112.tsv
116.tsv -here i would like to add last row with "This is the row what I want"
198.tsv -here i would like to add last row with "This is the row what I want"
205.tsv

I would like to use awk code:

awk '{if ($2<=0); print "This is the row what I want" "?? to file with same name like $1 ??"}' 1.file

Please could you help me?

Upvotes: 0

Views: 31

Answers (1)

Ed Morton
Ed Morton

Reputation: 203209

Something like this (untested) will do it:

awk '$2<0 { print "This is the row what I want" >> ("folder/"$1".tsv") }' file

You MIGHT need to add a ;close("folder/"$1".tsv") after the print if you aren't using GNU awk and can have a lot of files open simultaneously .

Upvotes: 1

Related Questions