0

I'm having problems applying my gawk command to an array of strings.

The gawk command in itself works fine:

$ gawk '$1 == "name" {print $0}' Data1.txt >> Data2.txt 

with this I am able to find anything that resembles the word 'name' in column 1 of my first data file, and copypaste the whole line to my second data file.

However, I have to do this procedure a couple of times, and when trying the following, it doesn't seem to retrieve anything:

$ array=("name1" "name2")
$ for i in "${array[$@]}"; do gawk '$1 == $i {print $0}' Data1.txt >> Data2.txt; done

the array itself seems fine, as it works when I replace the gawk command with an echo command. I've also tried to replace $i for "$i", ${i}, "${i}", etc, but this didn't help.

Any idea what I'm doing wrong?? I'm kinda new to Linux, so sorry in advance for my noob question!

msw
  • 42,753
  • 9
  • 87
  • 112
RMulder
  • 21
  • 1
  • 4
  • Unfortunately `' "$i" '` (or `'$i'`) doesn't paste anything in my Data2.txt file either. I do see now why gawk isn't reacting to my `$i` however, thanks for that! – RMulder Jul 05 '15 at 16:38
  • Yes I noticed the spaces weren't necessary, but unfortunatly I don't get anything either without them – RMulder Jul 05 '15 at 16:59
  • gah, please see http://stackoverflow.com/questions/8880603/loop-through-array-of-strings-in-bash-script my recollection of bash array syntax was horrible. – msw Jul 05 '15 at 17:30
  • Its extremely unlikely that you are approaching this the right way since any time you write a loop in shell to manipulate text you have the wrong approach. The right approach is probably to do it all in awk but we'd need to know more about what you are doing with sample input and expected output to help you with that. – Ed Morton Jul 06 '15 at 05:44
  • That's what I am discovering, however, I have no idea how to make an array in (g)awk. Data1 is a text file with over 400 000 variables listed in it. The first name of each column starts with a variable name and the other columns provide more info on this variable. I want to extract certain variables by their name, and copy paste their info into a new text file, Data2. Since I have a lot of variable names of which I want to retrieve information, looping through an array of them seems much faster to me. – RMulder Jul 06 '15 at 14:41

1 Answers1

0

The correct way to do this is like this:

for i in "${array[@]}"
do
  awk '$1 == i' i="$i" Data1.txt
done > Data2.txt

If you want to avoid creating an awk variable you can do this, but I do not advise it because it changes the scope of the variable:

export i
for i in "${array[@]}"
do
  awk '$1 == ENVIRON["i"]' Data1.txt
done > Data2.txt
Zombo
  • 1
  • 62
  • 391
  • 407