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?
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 likedoglion
and not onlydog_lion
. – terdon May 07 '14 at 16:52