Quantcast
Channel: Parse file with Awk only when first row matches pattern - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 4

Answer by Barefoot IO for Parse file with Awk only when first row matches pattern

$
0
0

Assuming a simple, comma-delimited file where every comma is a delimiter (some csv files may have quoted commas that ought not to be treated as field seperators), the following prints every line except the header when a column in the header is "SOMESTRING":

awk -F, '    FNR==1 {        for (i=1; i<=NF; i++)             if ($i == "SOMESTRING")                next        nextfile    }    1' file1 file2 file3 file4

The string comparison can be replaced with a substring test or a regular expression match operation, if appropriate.

nextfile is not part of POSIX AWK, but it is widespread; it is available in at least gawk, nawk (used on *BSD systems), mawk, and busybox.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>