2

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
4
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.

  • 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
3

Try doing this :

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

or

awk '/^[[:blank:]]*FILE=/{print $1, $5}'
  • 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 – Gilles Quenot Feb 20 '13 at 11:52
  • This still misses the line with FILE#. – Chris Down Feb 21 '13 at 2:31

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.