Home > OS >  Linux Bash, get only single word with grep
Linux Bash, get only single word with grep

Time:07-06

I have a large textfile and I have to find all of the "KNR"-alias. I removed all comments and empty lines with this:

cat file | grep -v ^# | grep -v ^$ >> Test.txt

How can I only get this one word, that I need to write out of the "Test.txt" file? I really only need one word, which has the structure of: "KNR00000000000000000_THING" I don´t know if this is possible, but after that structure. there is always a space.

I also need some help with how to make that first line of code work in a script.

Any help would be very appreciated. Kind regards Elias

CodePudding user response:

If you only need this word and noting else, you could do a

grep -vE '^#|^$' file | grep -owE KNR0 _THING

Of course if there are several words matching this pattern, you get all of them.

Explanation:

-o : Output only the part matching the pattern -w : Match only words -E : Turn on extended regex (so that can be used).

I also modified your test for lines to remove.My pattern removes all lines starting with a #, and those which contain zero characters.

CodePudding user response:

As per comments:

grep -ohE 'KNR[^ ]*' file

will use grep to match your pattern as I understand it. [^ ] means anything but a space.

Other possibilities are sed:

sed -nE "s/.*(KNR.*) .*/\1/p

or @F.Hauri's

tr < file \  \\n | grep ^KNR

This assumes that the KNR is either after a space or at the start of a line.

  •  Tags:  
  • bash
  • Related