help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to make `read` fail if there is not enough fields in the input?


From: Assaf Gordon
Subject: Re: How to make `read` fail if there is not enough fields in the input?
Date: Fri, 6 Dec 2019 13:15:07 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0

On 2019-12-04 8:01 p.m., Eli Schwartz wrote:
On 12/4/19 9:12 PM, Peng Yu wrote:
I’d like to know whether there is enough field in a TSV row.

Are you building a TSV validator in bash? o_O


Somewhat related:

GNU Datamash ( https://www.gnu.org/software/datamash/ )
has a "check" command that performs exactly that:
verifies that an input file for number of fields.

Examples:

  $ printf "A\tB\t\C\n" | datamash check
  1 line, 3 fields


  $ printf "A\tB\tC\nD\tE\n" | datamash check
  line 1 (3 fields):
    A   B       C
  line 2 (2 fields):
    D   E
  datamash: check failed: line 2 has 2 fields (previous line had 3)

Use "-W" if the delimiters are whitespace (and not a single tab):

  $ printf "A\tB \t C\nD    E F\n" | datamash -W check
  2 lines, 3 fields

The exit code will be zero (success) if the file has
same number of fields in all lines.

More details and examples of incorporating it in scripts here:
  https://www.gnu.org/software/datamash//manual/html_node/Check.html


regards,
 - assaf


disclaimer: I'm datamash's developer :)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]