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.
5 Answers
To call a script for each value in colum #4, you can use something like this:
awk '{system("./your_script.sh " $4)}' inputfile
-
1I'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 Commented 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
done
Explanation:
tr -s ' '
squelches consecutive spaces so that columns are separated by single spacecut -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
./myscript $( awk '{print $4} file.txt' )