I have a text file with the following structure:
text1;text2;text3;text4
...
I need to write a script that gets 2 arguments: the column we want to search in and the content we want to find.
So the script should output only the lines (WHOLE LINES!) that match content(arg2) found in column x(arg1).
I tried with egrep and sed, but I’m not experienced enough to finish it. I would appreciate some guidance…
Given your added information of needing to output the entire line,
awkis easiest:Explaining the above, the
-voptions setawkvariables without needing to worry about quoting issues in the body of theawkscript. Pre-POSIX versions ofawkwon’t understand the-voption, but will recognize the variable assignment without it. The-Foption sets the field separator. In the body, we are using a pattern with the default action (which isprint); the pattern uses the variables we set with-vfor both the column ($there isawk‘s “field index” operator, not a shell variable) and the pattern (andpatcan indeed hold anawk-style regex).