I was trying to figure out a solution for this question. I wanted to use awk for the solution.

My input file is something like below.

-bash-3.2$ cat file

I used awk command to extract the second values after _ as below.

awk -F "_" '{print $2}' file

However, though the above command prints the correct values I am getting blank lines in my output. I have 2 questions.

Question 1

How can I remove the blank lines in output so that I get only venkat and venkat3 in the output?

If I use printf instead of print in my awk, I get venkatvenkat3 as output which is not I wanted to achieve. I want the output like,


Question 2

Using those values as an associative array or something, how can I find if the values actually occur in $1 column?

I wanted to achieve something like,

awk -F "_" '$2==1{print $1}' file


I did not notice the awk solution of Stephane. Is it doing the same thing that I had mentioned?

  • 1
    Stephane's awk is not doing the same thing. Your approach assumes that a word can only be contained in another if it is separated by _. While that is true for the OP's example, all of the posted answers also deal with cases like doglion and not only dog_lion. – terdon May 7 '14 at 16:52
  • For non-awk, see: How to remove blank lines from a file in shell? – kenorb May 5 '15 at 16:05


$ awk -F _ 'NF > 1 {print $2}' file


$ awk -F _ '
    NR == FNR {a[$1];next}
    ($2 in a) {print $2}
' file file
  • I need to display venkat and venkat3 as per the OP asked in the other question. I am trying to find if the key after _ is present in my $1 column. – Ramesh May 7 '14 at 15:43
  • Oh, I have updated my answer! – cuonglm May 7 '14 at 15:46
  • Nice solution. I think you should add this one also to your solution in the other question :) – Ramesh May 7 '14 at 15:48

for Question 1, you could use the --only-delimited (-s) option of cut

cut -s -f2 -d'_' file

Another approach:

Question 1

awk -F_ '$2{print $2}' file

This will only print if $2 is defined. It is a shorter way of writing:

awk -F_ '{if($2){print $2}}' file

Question 2

Don't have anything to add that has not already been addressed.

  • 1
    Good solution for question1. Short and crisp :) – Ramesh May 7 '14 at 17:16

question 1

awk -F "_" '/_/ {print $2}' file

question 2

awk -F "_" '{values[$1]=1;}; END {for (val in values) print val;}' file
  • Nice solution. I like it :) – Ramesh May 7 '14 at 15:30
  • For question2, I intend to get only venkat and venkat3 as output as they are present in $1. However, I get all the $1 values as per your command. – Ramesh May 7 '14 at 15:37
  • @Ramesh: As your describtion, I think you want to get $2 of entry that have $2 occurs in 1st column. Is this right? – cuonglm May 7 '14 at 15:43
  • @Gnouc, yes you are right. – Ramesh May 7 '14 at 15:44

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.