-1

I am trying to print the line numbers of each line with Fail in it. I am using echo grep -n FAIL ${filename} to print the result to the screen. However, it prints every line to teh same line. How do I breakup the result so that each line iss on it's own?

EDITED: Added input

Input would be :

FAIL

PASS

FAIL

PASSAGE

This gets a PASS

I take a pass on this

Output to the command line would be

1 FAIL

3 FAIL

Ehedges
  • 1
  • 1
  • 4
    why are you wrapping grep in echo? – jhnc Sep 24 '19 at 00:39
  • 1
    please add example input and expected output in your question. – Sufiyan Ghori Sep 24 '19 at 00:42
  • It's for a lab, 2) If a file contains "FAIL(s)" then add logic to display the line number in the file that "FAIL(s)" occurred on to the screen. – Ehedges Sep 24 '19 at 00:45
  • Don't use `echo` on a command in backticks; that's probably causing the problem, and there are several others it can cause as well. Just run the command directly. Also, you should double-quote the variable reference. [shellcheck.net](https://www.shellcheck.net) is good at pointing out common mistakes like these. – Gordon Davisson Sep 24 '19 at 01:54
  • See also [useless use of `echo`](http://www.iki.fi/era/unix/award.html#echo) – tripleee Sep 24 '19 at 05:15

1 Answers1

0

This print the line number for each file with FAIL line

awk '/FAIL/ {print FNR}' file*

If you like file name as well

awk '/FAIL/ {print FNR,FILENAME}' file*

Or with line info

awk '/FAIL/ {print FNR,$0,FILENAME}' file*
Jotne
  • 40,548
  • 12
  • 51
  • 55