Reputation: 7387
So, let's say I have got this data file:
apples asd 45 321 7000
oranges gl 78 102 850
some ltd 83 15 500
other nova 80 50 3500
stuff 600 65 115 450
And I want to loop through each line, get the last word (which is the big numbers), which is supposedly the price. Then check if it's smaller than 1000
, and if so - output it to another file.
Here's what I've got:
touch items_cheaper_than_1k.txt
while read LINE
do
PRICE="$(cut -d. -f3 $LINE)"
if [$PRICE < 1000]; then
$LINE >> items_cheaper_than_1k.txt
fi
done < cars.txt
cat items_cheaper_than_1k.txt
The problem is that I get the error
cut: apples: No such file or directory
cut: asd: No such file or directory
cut: 45: No such file or directory
cut: 321: No such file or directory
cut: 7000: No such file or directory
(and sequentially for each single word of each line). Am I doing the cut wrong?
After the list of errors per line, I get line 5: 1000]: No such file or directory
Upvotes: 1
Views: 50
Reputation: 41987
With awk
:
awk '$NF<1000' file.txt
$NF
is the value of the last field and $NF<1000
checks if the value is less than 1000, if so then the line is printed.
To save the output in another file e.g. out.txt
:
awk '$NF<1000' file.txt >out.txt
Example:
% cat file.txt
apples asd 45 321 7000
oranges gl 78 102 850
some ltd 83 15 500
other nova 80 50 3500
stuff 600 65 115 450
% awk '$NF<1000' file.txt
oranges gl 78 102 850
some ltd 83 15 500
stuff 600 65 115 450
Upvotes: 2