3

I am trying to loop through a file named file.txt that contains a bunch of junk and one column (column #4) I am interested in. I want this loop to run from 0 to eof. For each value in column 4, I want to call another script.

Kusalananda
  • 333,661
ilikecake
  • 31
  • 1
  • 1
  • 2
  • Can you provide a copy of file.txt? So we understand what you are working with. – Whitecat Nov 12 '15 at 16:43
  • Do you want to call the same script once per line? Presumably with the value of column 4 as an argument to it? If so, please [edit] your question to say so. – Chris Davies Nov 12 '15 at 16:58
  • Just in case @roaima was right, you can think something like ./myscript $( awk '{print $4} file.txt' ) – Hastur Nov 12 '15 at 17:30

5 Answers5

4

To call a script for each value in colum #4, you can use something like this:

awk '{system("./your_script.sh " $4)}' inputfile
Kira
  • 4,807
  • 1
    I'd suggest full path to the script instead of ./your_script.sh as it refers to the script being present in the current directory, which may not be the case – Sergiy Kolodyazhnyy Nov 14 '15 at 23:17
2

Yet another version how it could be done, this time with shell builtins only:

while read line ; do
    set $line
    echo $4
done <filename

Replace echo with your script.

ott--
  • 856
1

I'm assuming that your columns are separated by whitespace.

for value in $(cat file.txt | tr -s ' ' | cut -d ' ' -f4); do
  ./my_script.sh $value
done

Explanation:

  • tr -s ' ' squelches consecutive spaces so that columns are separated by single space
  • cut -d ' ' -f4 uses single space as delimiter to choose 4th column
muru
  • 72,889
twink_ml
  • 111
1

I'm surprised no one has mentioned xargs, because that's precisely the purpose of xargs to provide values outputed by previous command to something else. We can combine that property with awk's ability to print columns. Bellow is sample demo. You can replace printf "Hello %s\n" part with your script

xieerqi@eagle:~$ df > testFile.txt
xieerqi@eagle:~$ awk '{print $4}' testFile.txt | xargs -I {} printf "Hello %s\n" {}
Hello Available
Hello 26269816
Hello 4
Hello 2914488
Hello 584064
Hello 5120
Hello 2827976
Hello 102324
-2
sed 1d $outfile1 | while read "column on file" do

done
muru
  • 72,889
Xhuk
  • 1