Starbucks
Starbucks

Reputation: 135

Removing rows that contains "(null)" value from a text file

I would like to remove any row within a .txt file that contains "(null)". The (null) value is always in the 3rd column. I would like to add this to a script that I already have.

Txt file example:

39|1411|XXYZ
40|1416|XXX
41|1420|(null)

In this example I would like to remove the third row. Im guessing its an awk -F but not sure from there.

Upvotes: 1

Views: 1103

Answers (5)

Daweo
Daweo

Reputation: 36510

I would like to remove any row within a .txt file that contains "(null)"

If you wish to do that using AWK let file.txt content be

39|1411|XXYZ
40|1416|XXX
41|1420|(null)

then

awk '!index($0,"(null)")' file.txt

will output

39|1411|XXYZ
40|1416|XXX

Explanation: index return position of first occurence of substring ((null) in this case) or 0 if none will find, I negate what is return thus getting truth for 0 and false for anything else and AWK does print where result was truth.

Upvotes: 1

Timur Shtatland
Timur Shtatland

Reputation: 12347

Use grep:

grep -v '|.*|(null)' in_file

Here, grep uses option -v : print lines that do not match.

Or use Perl:

perl -F'[|]' -lane 'print if $F[2] ne "(null)";' in_file

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-n : Loop over the input one line at a time, assigning it to $_ by default.
-l : Strip the input line separator ("\n" on *NIX by default) before executing the code in-line, and append it when printing.
-a : Split $_ into array @F on whitespace or on the regex specified in -F option.
-F'[|]' : Split into @F on literal |, rather than on whitespace.

SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches

Upvotes: 1

dawg
dawg

Reputation: 103864

Here is a sed:

$ sed '/(null)$/d' file
39|1411|XXYZ
40|1416|XXX

The $ assures that the (null) is at the end of the line. If you want to assure that (null) is the final column:

$ sed '/\|(null)$/d' file

And if you want to be extra sure that it is the third column:

$ sed '/^[^|]*\|[^|]*\|(null)$/d' file

Or with grep:

$ grep -v '^[^|]*|[^|]*|(null)$'

(But instead of this last one, just use awk...)

Upvotes: 2

Jens
Jens

Reputation: 72657

You are on the right track with using -F.

$ awk -F '|' '$3 != "(null)"' file.txt
39|1411|XXYZ
40|1416|XXX

You set the field separator to |, then print all lines where the third field is not equal to (null). This uses awk's default of "print the line" if there's no action associated with a pattern.

If you relax the requirement to specifically test the third field, and there is no other place for the "(null)" substring to occur, you can get the same result with

grep -vF '(null)' file.txt

Upvotes: 3

dash-o
dash-o

Reputation: 14452

With awk:

awk '-F|' '$3 != "(null)"' <  input-file

Upvotes: 2

Related Questions