How to Format My Grep Output to Show Line Numbers at the End of the Line, and Also the Hit Count

How can I format my grep output to show line numbers at the end of the line, and also the hit count?

-n returns line number.

-i is for ignore-case. Only to be used if case matching is not necessary

$ grep -in null myfile.txt

2:example two null,
4:example four null,

Combine with awk to print out the line number after the match:

$ grep -in null myfile.txt | awk -F: '{print $2" - Line number : "$1}'

example two null, - Line number : 2
example four null, - Line number : 4

Use command substitution to print out the total null count:

$ echo "Total null count :" $(grep -ic null myfile.txt)

Total null count : 2

Use grep to report back only line numbers

try:

grep -n "text to find" file.ext | cut -f1 -d:

How to fetch and display just the count of grep matches against each file that has the matching string?

As hinted by @root in the comments grep -cR 'stringtomatch' ./* will do the task.

Get line number while using grep

grep -n SEARCHTERM file1 file2 ...

grep results: how to format output file with one result for each line

Using awk for fixing the problem is an idea, after some adjustments.

You need \r (the Windows linefeed) and use double quotes.

grep 'ACCESSION' chrom_CDS_2.txt | awk '{print $0 "\r"}' > accession_out.txt

When you use awk, you do not need grep:

awk '/ACCESSION/ {print $0 "\r"}' chrom_CDS_2.txt > accession_out.txt

Another possibility is using sed: By default don't print lines. When ACCESSION is part of the line, replace the complete line with the complete line (&, matched part), followed by \r and use /p for printing it.

sed -n 's/.*ACCESSION.*/&\r/p' chrom_CDS_2.txt > accession_out.txt

Github Actions: Why an intermediate command failure in shell script would cause the whole step to fail?

By default Github Actions enables set -e to run the step's commands. This is why an intermediate command failure may cause the whole step to fail. To take full control of your step's commands you can do like this:

  - name: Check for changes
shell: bash {0}
run: |
diff=$( git diff --name-only 'origin/main' )
changed_files=$( echo $diff | grep -c src/my_folder )

# ...
# ...

Or you can just disable the default set -e at the beginning of the step's script:

  - name: Check for changes
run: |
set +x

diff=$( git diff --name-only 'origin/main' )
changed_files=$( echo $diff | grep -c src/my_folder )

# ...
# ...


Related Topics



Leave a reply



Submit