remove blank lines in awk display

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
ramesh
ramesh_venkat
ramesh3_venkat3
ramesh4
ramesh5
venkat
venkat3
venkat4

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,

venkat
venkat3

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

EDIT

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

Question1

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

Question2

$ awk -F _ '
    NR == FNR {a[$1];next}
    ($2 in a) {print $2}
' file file
venkat
venkat3

Method 3

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

cut -s -f2 -d'_' file
venkat
venkat3

Method 4

question 1

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

question 2

awk -F "_" '{values[$1]=1;}; END {for (val in values) print val;}' file


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x