0

I search through a HUGE file with cat and grep. I need to know on which line is cat all the time.

If i want to search ball:

cat file.txt | grep ball

FILE:

football
tennis
hockey
basketball
...

WHAT I NEED:

1. row - football [1/1000]
2. row - [2/1000]
2. row - [3/1000]
3. row - basketball [4/1000]
  • Would getting the line number (and not the total lines in the file) in front of each line be enough for you? – ojs Nov 11 '17 at 21:31
  • Of course it would be enough. Total lines i do with "wc -l file" – Pavel Meduna Nov 11 '17 at 21:48
  • awk '/ball/{print $0 " ["NR"]" }' file.txt – Arpit Agarwal Nov 11 '17 at 21:49
  • This is not exactly what i want. I want to see all the time the number of line where awk or cat is. Like printing percentage example: print("{}% Complete".format(percent), end="\r"). That current line of awk or cat will be printed on same line, updating every "tick". – Pavel Meduna Nov 11 '17 at 22:08
1

Complete awk + wc approach:

Sample file.txt:

football
tennis
hockey
basketball
boxing

awk -v t=$(wc -l file.txt | cut -d' ' -f1) '{ printf "%s[%d/%d]\n",(/ball/? $0" ":""),NR,t }' file.txt

The output:

football [1/4]
[2/4]
[3/4]
basketball [4/4]
[5/5]
  • What i didn't mention is if isn't found anything on line, the line numbers will be printed on same line, so it will not be so messy in the output. – Pavel Meduna Nov 11 '17 at 22:31
  • 1
    @PavelMeduna, then update your WHAT I NEED section with actual expected result – RomanPerekhrest Nov 12 '17 at 7:44
  • I made it. Little edit of your command. awk -v t=$(wc -l file.txt | cut -d' ' -f1) '{ printf "%s[%d/%d]\r", (/admin/? $0"\n":""), NR, t }' file.txt – Pavel Meduna Nov 12 '17 at 11:46
1

Doing grep -n ball file.txt will output

1:football
4:basketball

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.