grep abc student.csv | cut -d, -f 3 >text.txt
This would first extract every line from student.csv
that contains the substring abc
anywhere, and then cut out the 3rd comma-delimited column from those lines and store them in text.txt
.
With the data given in the question, text.txt
would end up containing
1/1/18
1/1/17
This relies on the original file being a simple CSV file, i.e. without fields containing embedded commas or newlines.
Would you want to search for abc
as a complete word, then use grep -w abc
in place of just grep abc
. This would avoid matching lines containing strings like abcde
, but would still match a field containing abc xyz
.
If you are looking for fields that contain an arbitrary complex string (still in a simple CSV file) and if you need the field to be exactly equal to that string, then you would have to iterate over the fields on each line:
string='some string' awk -F, '{ for (i = 1; i <= NF; ++i) if ($i == ENVIRON["string"]) { print $3 ; next } }' student.csv
This awk
code iterates over all fields on each line looking for a comma-delimited field whose value is exactly the same as the value of the environment variable string
. Once such a field is found, the 3rd field is printed.
abcd
exists in a row does that satisfy yourabc
exists criteria or not? In other words are you looking for full matches on a field or partial matches?