2

If I have a file that looks something like this eg called myfile.log

1  entry1      # Blah blah 
2  entry2      # Blah blah
3  entry3      # Blah blah
4  entry4      # Blah blah
5  myentry5      # Blah blah
6  myentry6      # Blah blah
7  yourentry7      # Blah blah
8  yourentry8      # Blah blah
9  ourentry9      # Blah blah
10 ourentry10      # Blah blah

How would I extract only the words containing "entry" so output should look something like

entry1
entry2
entry3
entry4
myentry5
myentry6
yourentry7
yourentry8
ourentry9
ourentry10

Tried various grep, sed, awk commands already but just can get exactly what I want, it either outputs everything on teh line, or I get it just to say entry instead of everything containing entry

Edit - file looks like this in reality(sanitized) - Need to extract all entries with RTR

10.0.0.1     MYRTR001      # Router in Raleigh
10.0.0.2     MYSW100      # Switch in Raleigh
10.0.0.3     MYRTR002      # Router in Houston
10.0.0.4     MYSW001      # Switch in Houton
1
  • In your sample input/output you should include lines that contain entry (or RTR - pick one and stick with it, don't muddy your question by initially saying 1 thing and then later changing) in the comments at the end of the line but not in the 2nd field, e.g. 1 foobar # wish there was an entry as all existing answers would match on entry in the comments which I suspect you don't want to happen.
    – Ed Morton
    May 21 at 14:33

4 Answers 4

2

Using sed

$ sed '/entry/s/[^ ]* *\([^ ]*\).*/\1/' input_file
entry1
entry2
entry3
entry4
myentry5
myentry6
yourentry7
yourentry8
ourentry9
ourentry10

Using awk

awk '/entry/{print $2}' input_file
entry1
entry2
entry3
entry4
myentry5
myentry6
yourentry7
yourentry8
ourentry9
ourentry10
3
  • 1
    Apologies - thanks awk '/entry/{print $2}' input_file does exactly what I'm looking for
    – laurencew
    May 20 at 9:25
  • @laurencew awk '/RTR/{print $2}' file . Same solution as provided earlier.
    – HatLess
    May 20 at 9:38
  • 1
    @HatLess yes thanks that work perfectly, I tried way too complicated solutions for a simple task
    – laurencew
    May 20 at 9:40
2

Using GNU grep iwth the -o option to return only the matched string:

grep -o '[^ ]*entry[^ ]*'

That will print only words containing the string entry no matter where they are in file or what comes before or after them. Even if the numbers at the beginning of each line aren't actually there and you only included them for descriptive purposes, it will return what is wanted.

Output:

entry1
entry2
entry3
entry4
myentry5
myentry6
yourentry7
yourentry8
ourentry9
ourentry10
1

When field separator is space character, then:

awk -v RS=' ' 'index($0, "entry")' infile
1

Assuming you only want to test the values in the 2nd column, you should be doing this:

awk '$2 ~ /entry/{print $2}' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.