0

I'm trying to output lines that contain a specific string

example of lines:

Column1:Column2:Column3 + ID
Column1:Column2:Column3 - ID
Column1:Column2:Column3

desired output:

Column1:Column2:Column3 + ID

Attempts -

awk -F: '$3 == + ID' < file.txt > out.txt

which returns no output as it's searching for EXACTLY " + ID " without any other input within column3

Now I know I could do

grep -f " + ID" <file.txt >out.txt

but if + ID isn't within column3 then it will output any line containing + ID, example of this would be

output:

Column1:Column2 + ID:Column3

notice how + ID is within column2 and not 3

now I would also like to do this within a directory searching through all files and not just one specific .txt

3

You can use a regular expression match, anchored to the end of the field, in place of a string match:

awk -F: '$3 ~ /+ ID$/' < file.txt
Column1:Column2:Column3 + ID

or anchored to the end of the line, with grep:

grep '+ ID$' < file.txt
Column1:Column2:Column3 + ID
  • how can i use awk to search a folder of text files instead of one individual text file? – user3255841 Mar 9 '18 at 1:40
  • 1
    never mind i just used find and then added awk to the command :) thank you for your help! – user3255841 Mar 9 '18 at 1:49
0
grep -Pie '(Column\d:?){3} + ID$' file.txt > out.txt

if, for instance Column1: pattern repetition more than 3 then modify {3,} above

0

This may be done through

find dir -type f -exec grep -H '+ ID$' {} +

This will find all regular files in or beneath the directory dir and then run grep -H '+ ID$' in batches of as many as possible of these. The result will contain both the filename of the file matching the regular expression and the lines from the file that matched.

To only get the filenames, use -l in place of -H with grep, and to only get the lines that match, use -L instead.

The regular expression + ID$ will match any line in a file that contains the exact string + ID at the very end of the line.

Or, without find but with a grep that is able to recurse into subdirectories:

grep -R '+ ID$' dir

On systems that have a grep that does not do -H (this is a non-standard option), you may instead use

find dir -type f -exec grep '+ ID$' /dev/null {} +

If you just want the matching lines (and no file names) on a system like this, cat the files together and grep that:

find dir -type f -exec cat {} + | grep '+ ID$'
-1

You can use substr like this:

awk '{ if (substr($0,position,length)=="+  ID" { print $0 } }' myfile.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.