1

I am looking for ways getting line number when using especially 'cat'.

I tried this command on the ubuntu 16.04 like this

cat flux.log |grep "statistic"

And This is the results when finding out information.

 Using fit statistic: chi
 Using test statistic: chi
Fit statistic : Chi-Squared =         175.92 using 16 PHA bins.
Test statistic : Chi-Squared =         175.92 using 16 PHA bins.
Fit statistic : Chi-Squared =         175.92 using 16 PHA bins.
Test statistic : Chi-Squared =         175.92 using 16 PHA bins.
 Fit statistic in use: Chi-Squared
 Using fit statistic: chi
 Using test statistic: chi
Fit statistic : Chi-Squared =         175.92 using 16 PHA bins.
Test statistic : Chi-Squared =         175.92 using 16 PHA bins.
Fit statistic : Chi-Squared =           6.05 using 16 PHA bins.
Test statistic : Chi-Squared =           6.05 using 16 PHA bins.
Fit statistic : Chi-Squared =           6.05 using 16 PHA bins.
Test statistic : Chi-Squared =           6.05 using 16 PHA bins.
Fit statistic : Chi-Squared =           6.05 using 16 PHA bins.
Test statistic : Chi-Squared =           6.05 using 16 PHA bins.
Fit statistic : Chi-Squared =           6.05 using 16 PHA bins.
Test statistic : Chi-Squared =           6.05 using 16 PHA bins.
Fit statistic : Chi-Squared =           6.05 using 16 PHA bins.
Test statistic : Chi-Squared =           6.05 using 16 PHA bins.
Fit statistic : Chi-Squared =          66.15 using 16 PHA bins.
Test statistic : Chi-Squared =          66.15 using 16 PHA bins.
 Fit statistic in use: Chi-Squared
 Using fit statistic: chi
 Using test statistic: chi
Fit statistic : Chi-Squared =          66.15 using 16 PHA bins.
Test statistic : Chi-Squared =          66.15 using 16 PHA bins.

In this results, i want to last lines for catching information.

Fit statistic : Chi-Squared =          66.15 using 16 PHA bins.
Test statistic : Chi-Squared =          66.15 using 16 PHA bins.

the problem is there are many lines and these numbers that are written are random. So i just have information such as "Fit","Test","statistic" or "Chi-Squared" to finding.

If these results have line number and could get distinguished, i can be find the line i want to. Is there somebody help me?

ps, i have tried this command

<cat -n flux.log |grep "statistic">

but all of lines are different each file.

  • 3
    Can't you use the `tail` command? `grep "statistic" flux.log | tail -2`. – codeforester Jan 25 '18 at 06:14
  • Please add sample input and your desired output for that sample input to your question. – Cyrus Jan 25 '18 at 06:24
  • 1
    `cat -n` and `grep -n` can both print line numbers; but often, requiring line numbers for the things you want to find is indicative of a more fundamental problem. A common antipattern is finding the matching lines so you can then delete them ... but you can delete them *when you find the match* just as well (`grep -v` or whatever), and the line numbers per se are completely unimportant and beside the point. – tripleee Jan 25 '18 at 07:44
  • 1
    Also, as usual, avoid the abominable [useless use of `cat`](https://stackoverflow.com/questions/11710552/useless-use-of-cat) – tripleee Jan 25 '18 at 07:45

2 Answers2

1

Try man nl. Also awk '{print NR,$0}' myfile

Mischa
  • 2,240
  • 20
  • 18
1

The combination cat/grep can usually be replaced with a singe awk.

In your case, you don't know when the last two occurrences would be as your file can span random number of lines. Try something like below:

$ awk '/statistic/{first= last;last="Line" NR " : " $0}END{print first RS last}'  casefile_48436615
Line27 : Fit statistic : Chi-Squared =          66.15 using 16 PHA bins.
Line28 : Test statistic : Chi-Squared =          66.15 using 16 PHA bins.

Info : The awk built-in NR gives the record number which indeed is the line number you're looking for. Also RS is the awk built-in for default record separator which is a newline. Using RS can help workaround hard-coding newlines in our script.

sjsam
  • 21,411
  • 5
  • 55
  • 102