Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have the following file:

…
   LINK=dummy 172.17.100 => 10.218.11 [PATH=/etc]
      FILE=hosts 172.17.100 => 10.218.11 [PATH=/etc]
       FILE=network 172.17.100 => 10.218.11 [PATH=/etc/sysconfig]
     LINK=empty 172.17.100 => 10.218.11 [PATH=/etc]
   FILE#TEST 172.17.100 => 10.218.11 [PATH=/etc/sysconfig]

How to print the first field and the fifth field only if line start with the word FILE?

The word FILE could be located immediately in the begging of line or after space or TAB in the line.

Example of what I expected to get

  FILE=hosts   [PATH=/etc]
  FILE=network [PATH=/etc/sysconfig]
  FILE#TEST    [PATH=/etc/sysconfig]

I try this awk but doesn't work

awk '$1 == "^[[:blank:]]*FILE*" && '{print $1" "$5}'  file
share|improve this question

2 Answers 2

up vote 3 down vote accepted
awk '$1 ~ /^FILE/ { print $1 " " $5 }'

Your method doesn't work because == checks for literal equality, not a regex; you need to use ~ for that.

share|improve this answer
    
but if FILE start after TAB or space then what need to add in your syntax? –  yael Feb 20 '13 at 11:52
    
@yael - You don't have to, as they are default field separators in awk. It should just work. –  Chris Down Feb 20 '13 at 11:53

Try doing this :

awk '/^ *FILE=/{print $1, $5}'

or

awk '/^[[:blank:]]*FILE=/{print $1, $5}'
share|improve this answer
    
This will not work, as there is whitespace before FILE. It also doesn't match the comments as asked in the question. –  Chris Down Feb 20 '13 at 11:52
    
Post edited accordingly –  sputnick Feb 20 '13 at 11:52
    
This still misses the line with FILE#. –  Chris Down Feb 21 '13 at 2:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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