grep and awk parse line

16,182

Solution 1

This will work, although you could probably simplify it further in a single awk script if you wanted:

awk  '{print $7}' <your file> | awk -F= '{print $2}' | sort -u

Solution 2

grep -o 'SRC=\([^ ]\+\)' | cut -d= -f2 | sort -u

Share:
16,182
Dan
Author by

Dan

Updated on June 04, 2022

Comments

  • Dan
    Dan about 2 years

    I have e a line that looks like:

    Feb 21 1:05:14 host kernel: [112.33000] SRC=192.168.0.1 DST=90.90.90.90 PREC=0x40 TTL=51 ....
    

    I would like to the a list of uniq IPs from SRC=

    How can I do this? Thanks

    • Dr. belisarius
      Dr. belisarius over 13 years
      Allow me to welcome you to StackOverflow and remind three things we usually do here: 1) As you receive help, try to give it too answering questions in your area of expertise 2) Read the FAQs 3) When you see good Q&A, vote them upusing the gray triangles, as the credibility of the system is based on the reputation that users gain by sharing their knowledge. Also remember to accept the answer that better solves your problem, if any, by pressing the checkmark sign
  • glenn jackman
    glenn jackman over 13 years
    I'd write awk '{split($7, a, /=/); print a[2]}' file | sort -u to avoid calling awk twice
  • glenn jackman
    glenn jackman over 13 years
    that will emit the prefix SRC= for each line
  • Dr. belisarius
    Dr. belisarius over 13 years
    @glenn I think that is OK. As the OP commented "Thanks everybody!" before I answered, so I think my answer was left unread. In any case, a substr(i,5) solves the problem, if there is one.
  • kvista
    kvista over 13 years
    ... and there you go! (I have tremendous faith in awk :-) Thanks glenn.