I have a file named test which has two columns one having ID and other having status.
I want to loop through the file and print IDs where status have one particular value (e.g. 'ACTIVE').
I tried
cat test | while read line; do templine= $($line | cut -d ' ' -f 2);echo $templine; if [ $templine = 'ACCEPTED' ]; then echo "$templine"; fi done
and some variation of above which obviously did not work.
Any help would be appreciated.
cat test | while read line; do templine= $($line | cut -d ' ' -f 2);echo $templine; if [[ $templine = 'ACCEPTED' ]]; then echo "$templine"; fi done
above command treadted IDs as commands and started executing them
– Abhijeet Shukla Feb 22 '16 at 09:36grep
ping the lines and then getting the column of the IDs only? – FelixJN Feb 22 '16 at 09:39awk '$2 == "ACTIVE" { print $1 }'
;-) – Stephen Kitt Feb 22 '16 at 09:50any idea why my query above did not work ?
– Abhijeet Shukla Feb 22 '16 at 09:54templine=$($line | cut ...)
rather than$(echo "$line" | cut ...)
or, better yet:$(printf '%s' "$line" | cut ...)
. your version of the command attempted to execute the command contained in$line
- which almost certainly doesn't exist. you probably also wantedcat test | while IFS= read line ; do ...
so thatread
assigns the entire input line to$line
rather than just the first field. but as has been mentioned awk is a better tool for this job. – cas Feb 22 '16 at 10:00cat test | while read line; do templine= $(echo "$line" | cut -f 2) ; done;
I made the changes as suggested but now it's executing the command contained in templine – Abhijeet Shukla Feb 22 '16 at 10:49