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

2 Answers 2

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.

2
  • but if FILE start after TAB or space then what need to add in your syntax?
    – yael
    Feb 20, 2013 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, 2013 at 11:53
3

Try doing this :

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

or

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .