3

I have a file (newline.txt) that contains the following line

Footer - Count: 00034300, Facility: TRACE, File Created: 20160506155539

I am trying to get the value after Count: up to the comma (in the example 00034300) from this line.

I tried this but I get is all the numbers concatenated into one large string with that command:

grep -i "Count:" newfile.txt | sed 's/[^0-9]//g'

output:0003430020160506155539

how do I get just the digits after Count: up to to the first non-digit character?

I just need 00034300.

John1024
  • 109,961
  • 14
  • 137
  • 171
sboga
  • 51
  • 1
  • 4

1 Answers1

6

Using sed

$ sed '/[Cc]ount/ s/[^:]*: *//; s/,.*//' newline.txt 
00034300

How it works:

  • /[Cc]ount/ selects lines containing Count or count. This eliminates the need for grep.

  • s/[^:]*: *// removes everything up to the first colon including any spaces after the colon.

  • In what remains, s/,.*// removes everything after the first comma.

Using awk

$ awk -F'[[:blank:],]' '/[Cc]ount/ {print $4}' newline.txt 
00034300

How it works:

  • -F'[[:blank:],]' tells awk to treat spaces, tabs, and commas as field separators.

  • /[Cc]ount/ selects lines that contain Count or count.

  • print $4 prints the fourth field on the selected lines.

Using grep

$ grep -oiP '(?<=Count: )[[:digit:]]+' newline.txt 
00034300

This looks for any numbers following Count: and prints them.

John1024
  • 109,961
  • 14
  • 137
  • 171