Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show lines which does not contain specific string on Linux

Tags:

linux

grep

sed

awk

I have a text file on my Linux server with these characters:

  ID              DATA
MF00034657,12435464^DRogan^DPUM-DT_MAX_1234;PUM-DT_MAX_1234;PUM-DT_MAX_1234;PUM-DT_MAX_1234;PUM-DT_MAX_1234;M-DT_MAX_1;
MF00056578,12435464^DRogan^DPUM-DT_MAX_1234;PUM-DT_MAX_1234;PUM-DT_MAX_1234;PUM-DT_MAX_1234;PUM-DT_MAX_1234;UM-DT_MAX_123;

Now I need to filter the lines which do not contain "PUM-DT_MAX_1234" and save them in another file with the ID.

Like this:

MF00034657,M-DT_MAX_1
MF00056578,UM-DT_MAX_123

I use:

grep -v 'PUM-DT_MAX_1234' file > file.out
awk '!/PUM-DT_MAX_1234/' file > file.out

But it doesn’t work.

How can I fix it?

like image 478
Dorian Avatar asked Jun 05 '15 13:06

Dorian


People also ask

How do I find all files not containing specific text on Linux?

When searching for certain file types that does not contain certain text or strings, you can use this command: >> find /home/example -iname "*. txt" -exec grep -Li "mystring" {} \+ This will list all text files that do NOT contain the term "mystring".

How do you print the lines from a file which does not contain the Searchstring?

You want to use the "-L" option of grep : -L, --files-without-match Only the names of files not containing selected lines are written to standard output. Path- names are listed once per file searched.

How do I find a particular string in Linux?

Use the grep command to search the specified file for the pattern specified by the Pattern parameter and writes each matching line to standard output. This displays all lines in the pgm. s file that begin with a letter.

How do you check if a string does not contains a substring in bash?

To check if a string contains a substring in Bash, use comparison operator == with the substring surrounded by * wildcards.


1 Answers

Use:

awk '$0 !~ /your_pattern/'

As found in the (probably) greatest AWK documentation.

like image 101
voiger Avatar answered Oct 11 '22 02:10

voiger