Skip to content
Advertisement

Grep a line then print awk until a certain substring

My code is

JavaScript

But, I want to have $9-$20 be stopped when it hits a value like (0) or (1). This will make my output format and look a lot nicer because anything after (0) or (1) is garbage.

Does anyone have an idea on a way to implement that?

Input:

JavaScript

Output:

JavaScript

EDIT: THANK YOU TO ALL THE PEOPLE THAT COMMENTED ON THIS THREAD ESPECIALLY ED AND GLENN

Advertisement

Answer

Update:

JavaScript

Should meet your requirements exactly

Notes:

  • NF is an awk variable containing the Number of Fields in the current record.
  • when we see a record that contains the pattern:
    • store the first 4 fields in a variable called rec, separated by the Output Field Separator.
    • loop over the fields from 9 to the last, appending to the rec variable
      • when we see one that matches the regular expression (a zero or a one in parentheses), then we break out of the for loop
    • and print the accumulated rec string.

First of all, be aware that awk can do what cat and grep do, so we can simplify the pipeline immediately

JavaScript

Next, it sounds like you want to

JavaScript

This changes the “number of fields in this record” variable, so that subsequent fields are ignored.

Testing

JavaScript

Outputs

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement