0

I have many files with different names that end with txt.

rtfgtq56.txt
fgutr567.txt
..

So I am running this command

for i in *txt
do
  awk -F "\t" '{print $2}' $i | grep "K" | awk '{print}' ORS=';' | awk -F "\t" '{OFS="\t"; print $i, $1}' > ${i%.txt*}.k
done

My problem is that I want to add the name of every file in the first column, so I run this part:

awk -F "\t" '{OFS="\t"; print $i, $1}' > ${i%.txt*}

$i means the file that are in the for loop, but it did not work because awk can't read the $i in the for loop. Do you know how I can solve it?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    Does this answer your question? [How do I use shell variables in an awk script?](https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script) – rethab Dec 14 '21 at 12:06
  • Could you please [edit] to clarify what the xpected output should look like? You want the filename, a tab, and all the values from the second column from lines which contain `K` in a single column with semicolons between them? – tripleee Dec 14 '21 at 12:28
  • Using the `-v` option of `awk`, you can create an awk Variable based on a shell variable. See the awk man-page. – user1934428 Dec 14 '21 at 13:37

2 Answers2

3

You want to refactor eveything into a single Awk script anyway, and take care to quote your shell variables.

for i in *.txt
do
    awk -F "\t" '/K/{a = a ";" $2}
      END { print FILENAME, substr(a, 1) }' "$i" > "${i%.txt*}.k"
done

... assuming I untangled your logic correctly. The FILENAME Awk variable contains the current input file name.

More generally, if you genuinely want to pass a variable from a shell script to Awk, you can use

awk -v awkvar="$shellvar" ' .... # your awk script here
    # Use awkwar to refer to the Awk variable'

Perhaps see also useless use of grep.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • ... and you don't need the variable anyway because you don't need the loop, you could do the whole thing in 1 call to awk. But that'd be a different question. – Ed Morton Dec 14 '21 at 12:41
  • Yeah, but then you'd need to write to the output file from Awk instead, which I didn't want to tackle in what's obviously a beginner question. – tripleee Dec 14 '21 at 12:53
  • 1
    Understood, hence `But that'd be a different question`, just figured the OP should be made aware. – Ed Morton Dec 14 '21 at 12:56
1

Using the -v option of awk, you can create an awk Variable based on a shell variable.

awk -v i="$i" ....

Another possibility would be to make i an environment variable, which means that awk can access it via the predefined ENVIRON array, i.e. as ENVIRON["i"].

user1934428
  • 19,864
  • 7
  • 42
  • 87