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]
4
  • Would getting the line number (and not the total lines in the file) in front of each line be enough for you? Commented Nov 11, 2017 at 21:31
  • Of course it would be enough. Total lines i do with "wc -l file" Commented Nov 11, 2017 at 21:48
  • awk '/ball/{print $0 " ["NR"]" }' file.txt Commented Nov 11, 2017 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". Commented Nov 11, 2017 at 22:08

2 Answers 2

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]
3
  • 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. Commented Nov 11, 2017 at 22:31
  • 1
    @PavelMeduna, then update your WHAT I NEED section with actual expected result Commented Nov 12, 2017 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 Commented Nov 12, 2017 at 11:46
1

Doing grep -n ball file.txt will output

1:football
4:basketball

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.