How can I use grep
to search only one column within a CSV file, but after a match is found, to output the entire line? E.g.:
fish @ eats worms
bear @ eats fish
Searching for fish
in column 2 will output: bear @ eats fish
.
...Use awk...
awk -F@ '{if ($2 ~ /fish/) { print $0; }}' <input file>
To use a shell variable change single quotes to doubles and escape the awk variables (soo awk still sees $2 etc and not the shell's expansion of them.
awk -F@ "{if (\$2 ~ /$find_me/ ) { print \$0; } }" <input_file>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With