I have the following 0.txt
file below separating content by columns (fields):
'Disinfectants', 'Brand A', 'Brand B', 'Brand C'
'brand A', 'below brand C', 'greater than brand B'
'brand B', 'greater than brand D', 'below brand A'
I would like to find (from the second column) every time a pattern (say "brand A") occurs, and print out the contents of the first column belonging to the line where this pattern is found. For both the content of the resulting file is like:
Disinfectants
brand B
I have seen other similar issues but only printing the column itself where the pattern was found, usually using grep
.
EDIT UPDATE: from @jubilatious1 suggestion , I found a question (https://stackoverflow.com/a/9153113) on the OS as part of my search for a solution.
awk '/brand A/{ print substr( $1, RLENGTH )}' 0.txt > 1.txt
but my 1.txt
output has been different than expected as it prints only part of the content of the first field (column):
'brand
'brand
Besides, just using awk '/brand A/{ print substr( $1, RLENGTH )}'
I can't specify that the search only works from the second field (column) onwards for each line.
EDIT UPDATE 1: maybe just fix the output of awk '/brand A/{ print substr( $1, RLENGTH )}'
so that correctly printing the contents of the fields in the first column is a first step.
Hackish pipeline:
cut -d, -f2- 0.txt | grep -ni 'brand a' | sed 's/:.*/p/' | sed -nf- 0.txt | cut -d, -f1
{linenumber}p
-- a sed command to print that linesed -nf-
... this will print only when instructed to from stdin ... so you get only the lines you wantOr perl:
perl -lanF, -e 'print $F[0] if grep /brand a/i, @F[1..$#F]' 0.txt
@F
on commas, and print the first field if 'brand a' (case insensitive) is found in any of the other fields.Both output this:
'Disinfectants'
'brand B'
You can strip the single quotes however you'd like, or, you can change the split
regex for perl autosplit:
perl -lanF"/[',]+/" -e 'print $F[1] if grep /brand a/i, @F[2..$#F]' brand.txt
To get this:
Disinfectants
brand B
... note that once the line starts with a split delimiter, $F[0]
is an empty string.