3

i know it is very basic question but im total new in shell scripting

i a txt file called 'berkay' and content of it is like

03:05:16 debug blablabla1
03:05:18 error blablablablabla2
05:42:14 degub blabblablablabal
06:21:24 debug balbalbal1

I want to print the lines whose second column is error so the output will be

03:05:18 error blablablablabla2

I am thinking about something like " if nawk { $2}" but i need help.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
abidinberkay
  • 1,857
  • 4
  • 36
  • 68

1 Answers1

3

With this for example:

$ awk '$2=="error"' file
03:05:18 error blablablablabla2

Why is this working? Because when the condition is true, awk automatically performs its default behaviour: {print $0}. So there is no need to explicitly write it.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • No, the `$` just shows the prompt. You just need to copy from `awk` – fedorqui Feb 11 '14 at 13:24
  • 1
    Why not just `$2=="error"`? I don't see why an RE comparison would be useful here. – Ed Morton Feb 11 '14 at 13:27
  • 1
    You are totally right, @EdMorton, I just messed up. Updated with a proper solution. Thanks! – fedorqui Feb 11 '14 at 13:30
  • 1
    thanks dude i did "awk '{ if($2 == "error") print $0;}' berkay" and it worked. im loving shell scripting gradually – abidinberkay Feb 11 '14 at 13:55
  • Nice, @abidinberkay Note that my solution is more "idiomatic". That is, it takes some `awk` default ways of handling to skip some parts like `if ()` --> `()` or `{print $0}`. – fedorqui Feb 11 '14 at 13:57
  • 1
    @abidinberkay You could add 50 more redundant lines of code and 10 more spurious semi-colons to it and still get the output you want and it still wouldn't be the solution. See what fedorqui posted - that is how to solve this problem in awk. If that didn't work for you then you should spend the time now to figure out what you are doing wrong or what is broken about your awk. Maybe you're using old, broken awk (/usr/bin/awk on Solaris). – Ed Morton Feb 11 '14 at 13:58