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.

  • Can you provide a copy of file.txt? So we understand what you are working with.
    – Whitecat
    Nov 12, 2015 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. Nov 12, 2015 at 16:58
  • Just in case @roaima was right, you can think something like ./myscript $( awk '{print $4} file.txt' )
    – Hastur
    Nov 12, 2015 at 17:30

5 Answers 5


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

awk '{system("./your_script.sh " $4)}' inputfile
  • 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 Nov 14, 2015 at 23:17

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.


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


  • 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

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
sed 1d $outfile1 | while read "column on file" do


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .