Looping through a file using a column
Solution 1
To call a script for each value in colum #4, you can use something like this:
awk '{system("./your_script.sh " $4)}' inputfile
Solution 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.
Solution 3
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
Solution 4
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
Related videos on Youtube
ilikecake
Updated on September 18, 2022Comments
-
ilikecake over 1 year
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.
-
Admin over 8 yearsCan you provide a copy of file.txt? So we understand what you are working with.
-
-
Sergiy Kolodyazhnyy over 8 yearsI'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