0

I run a awk script. But, it is here. This is the command awk -f awk_scr ERR.txt.

BEGIN {
FS=" " 
target="missing" 
}
{
for (i=1; i <= NR; i++) {
  for(j=1; j <= NF; j++) {
    if ($j == target) {
      do { 
        printf $j > "final.txt" 
      } while (j == NF)
    }           
    if (j == NF) {
      printf "\n"
    }
  }
}
}

The ERR.txt content is here. This awk script is for trimming what that is matched by "missing" and then print to a file, final.txt.

npm ERR! peer dep missing: react@^15.0.0, required by [email protected]
npm ERR! peer dep missing: [email protected] - 3, required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]

But, when I execute the command, there is only a blank stdout.

================ADD MORE CONTENT================

This is what I expect to be, albeit I just spend few minutes to do. Anyway, the awk script is worth to use in the future.

    missing: react@^15.0.0, required by [email protected]
    missing: [email protected] - 3, required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
7
  • 3
    Please clarify what you mean by "trimming". Note that the code block that you have in your awk script will be executed once for each line in the input. Having a loop over NR is therefore not needed, unless you'd like to do the inner loop once for the first line, twice for the second line, thrice for the third, etc.
    – Kusalananda
    Commented Jan 15, 2019 at 6:44
  • You do not redirect your LF (\n) to the file.
    – kvantour
    Commented Jan 15, 2019 at 8:25
  • 1
    Please edit the question and add the expected output, otherwise we can just guess what you want to achieve as your awk script is not clear.
    – pLumo
    Commented Jan 15, 2019 at 9:53
  • 2
    You probably see nothing because you get stuck in an infinite loop: the do while loop does not alter j, so if j == NF, you're stuck. Did you check the file size of the final.txt file? Commented Jan 15, 2019 at 19:26
  • What's in final.txt for the sample input? Commented Jan 15, 2019 at 22:48

2 Answers 2

1
# awk_scr

BEGIN {
    # clear the output file if it already exists
    # you can do this using your shell instead
    ORS = ""
    print "" > "final.txt"
    ORS = "\n"
}

/missing/ {
    # delete all characters prior to the last occurrence of "missing:"
    # this assumes that there is only one sub-string "missing:" per line
    sub(/.*missing:/, "missing:", $0)

    # append the updated line to the output file
    print $0 >> "final.txt"
}

Then run awk -f awk_scr ERR.txt && cat final.txt.

Another solution is to use sed, which is shorter:

sed 's/^.*missing:/missing:/' ERR.txt | tee final.txt

However, it has the same downside as the awk solution: it assumes that there is only one occurrence of "missing:" per line. This is due to the fact that both awk and sed only match greedily.

To simulate lazy matching, you can do the following, instead (which is better):

sed -E 's/(missing)(.*)|./\1\2/g' ERR.txt | tee final.txt

1

I believe your script is a bit convoluted and shows a misunderstanding of what 'awk' is all about. Awk programs consist out of a list of

pattern { actions }

statement. Every time a new record/line is read, awk will process that line by executing the pattern-action statement one by one. From what I see, it looks like you imagine that awk reads the full file in memory and you can then process it using a for loop over the number of records. This is not how it works.

The following awk will probably do what you request:

awk '/missing/' inputfile > "missing.txt"

Here, the patter is /missing/ and the action is the default action 1 which is equal to {print $0}, or print the full record. The pattern /missing/ states, select all lines which contain the substring "missing"

2
  • 1
    Or just grep -F 'missing:' <inputfile >missing.txt.
    – Kusalananda
    Commented Jan 15, 2019 at 9:46
  • 1
    I suspect awk '/missing/ {print $NF}' inputfile > "missing.txt" is what is wanted. Commented Jan 15, 2019 at 19:28

Not the answer you're looking for? Browse other questions tagged .