0

How to get 1st field of a file only when 2nd field matches a given string?

#cat temp.txt

Ankit   pass
amit    pass
aman    fail
abhay   pass
asha    fail
ashu    fail

cat temp.txt | awk -F"\t" '$2 == "fail" { print $1 }'*

gives no output

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ankit Sharma
  • 413
  • 1
  • 4
  • 9
  • Welcome to SO, thank you for showing your efforts in form of code. Your subject says 5th field you want to print and check 7th field but your code says its 2nd field you are checking. Could you please do add more clear description in your post and let us know then. – RavinderSingh13 Jul 10 '20 at 13:16
  • 1
    are you sure that your second field is exactly equal to "fail"? There are no extra spaces involved, maybe there is a hidden carriage return (CR) from a windows file (check with `cat -vET file`. Maybe due to the tab as a field separator, your field is actually something like `"fail___"` (underscore represents space)`. Hence there are many possibilities here of what might have gone wrong. – kvantour Jul 10 '20 at 13:27
  • 3
    Remove the `-F"\t"` and the stray `*` at the end of the command. For the data shown, it should work with tabs or spaces in the input. – Jonathan Leffler Jul 10 '20 at 13:36

3 Answers3

0

Your code seems to work fine when I remove the * at the end

cat temp.txt | awk -F"\t" '$2 == "fail" { print $1 }'

The other thing to check is if your file is using tab or spaces. My copy/paste of your data file copied spaces, so I needed this line:

cat temp.txt | awk '$2 == "fail" { print $1 }'

The other way of doing this is with grep:

cat temp.txt | grep fail$ | awk '{ print $1 }'
James McGuigan
  • 7,542
  • 4
  • 26
  • 29
  • Yes, there are thousand other ways to do it, But it is bugging me, why i am not getting any result with "cat temp.txt | awk -F"\t" '$2 == "fail" { print $1 }'" still – Ankit Sharma Jul 10 '20 at 18:49
  • Ankit did you try the suggestion you got [in a comment](https://stackoverflow.com/questions/62835076/how-to-get-1st-field-of-a-file-only-when-2nd-field-matches-a-string/62842830#comment111116677_62835076) from @kvantour 5 hours before you posted that comment? I think he was on the right track. – Ed Morton Jul 10 '20 at 21:59
  • Check to see if your file is using tabs or spaces. If using spaces try `cat temp.txt | awk '$2 == "fail" { print $1 }'`. (example now fixed), or else remove the `*` from the end of your command. – James McGuigan Jul 11 '20 at 10:21
  • 1
    Awk can open files just fine by itself, see http://porkmail.org/era/unix/award.html. – Ed Morton Jul 12 '20 at 04:42
  • I have used an alternate @james the recommended one still is giving any output – Ankit Sharma Jul 15 '20 at 21:47
0

Another syntax with awk:

awk '$2 ~ /^faild$/{print $1}' input_file

A deleted 'cat' command.

  • ^ start string
  • $ end string

It's the best way to match patten.

James McGuigan
  • 7,542
  • 4
  • 26
  • 29
mchelabi
  • 154
  • 5
  • 1
    of course equivalent to `$2=="fail"` here (once you fix the typo), but may be useful if it wasn't a literal match. – karakfa Jul 10 '20 at 15:24
  • ...'faild' or 'fail', this is just an example! – mchelabi Jul 10 '20 at 15:54
  • 2
    Since you're new there, I gave some feedback but cannot force you to take it. People usually copy/paste the scripts to test, so I think it make sense to at least try to make sure it works; but again it's up to you. – karakfa Jul 10 '20 at 16:48
  • There are no patterns when matching text, there are only strings and regexps. Using a regexp comparison to match a string is inefficient, fragile, and obfuscates your code so `$2 == "fail"` is better in every way than `$2 ~ /^fail$/`. – Ed Morton Jul 10 '20 at 22:03
  • and what if we have to search for a variable rather than a literal. suppose NAMESPACE="test".. awk '$2 ~ /$NAMESPACE/{print $1}' input_file will not work ? – Ankit Sharma Jul 18 '20 at 01:22
  • You can use the variable like this for example: awk '$2 ~ /^'"$NAMESPACE"'$/{print $1}' input_file – mchelabi Jul 19 '20 at 08:26
0

Either:

  1. Your fields are not tab-separated or
  2. You have blanks at the end of the relevant lines or
  3. You have DOS line-endings and so there are CRs at the end of every line and so also at the end of every $2 in every line (see Why does my tool output overwrite itself and how do I fix it?)

With GNU cat you can run cat -Tev temp.txt to see tabs (^I), CRs (^M) and line endings ($).

Ed Morton
  • 188,023
  • 17
  • 78
  • 185