1

I've a grep command

grep -o 'ITEM+[^ ]*' "file.txt"

And the returned string is like this

ITEM+12345678::hashtext_20191030::ITEM+87654321::hashmap_20191030::ITEM+12348765::hashvalue_20191001

So when i add the cut command,

grep -o 'ITEM+[^ ]*' "file.txt" | cut -c 5-12

I only get one result:

12345678

How do i make the result to be something like this:

12345678
87654321
12348765
1
  • it's better if you post a testable fragment of your file.txt Commented Oct 31, 2019 at 5:39

1 Answer 1

0

Your grep only returns one line, the [^ ]* pattern matches 12345678::hashtext_20191030::ITEM+… and the rest of the line until a space character is found. If you only want to match ITEM+ and the following digits, use

grep -o 'ITEM+[^:]*' file.txt

or

grep -o 'ITEM+[0-9]*' file.txt

and you will get multiple results.

You should get all the numbers with

grep -o 'ITEM+[^:]*' file.txt | cut -c 6-

which returns the 6th character until the end of the line.

1
  • If using GNU grep, the comand: grep -oP 'ITEM\+\K[0-9]{8}' will extract the numbers directly, no cut required.
    – user232326
    Commented Oct 31, 2019 at 6:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .