Help to get correct data using awk

I have this input.

|user1    |10.10.10.10   |23|046|1726 (212)  |0
|user2    |10.10.10.11   |23|046|43 (17)     |0
|test     |10.10.10.12   |23|046|45 (10)     |0
|test1    |10.10.10.13   |23|046|89 (32)     |0

I need to get the data for a user like this

user1 1726
user2 43
test 45
test1 89

I thought my awk line did work fine, but then I see that username that is part of other username makes problems.

Here is what I have done

echo "|user1    |10.10.10.10   |23|046|1726 (212)  |0
|user2    |10.10.10.11   |23|046|43 (17)     |0
|test     |10.10.10.12   |23|046|45 (10)     |0
|test1    |10.10.10.13   |23|046|89 (32)     |0" | awk  -F"|" '$0 ~ search {print $6}' search="user1"| awk '{print $1}'
1726

This works fine except user test that gives this:

echo "|user1    |10.10.10.10   |23|046|1726 (212)  |0
|user2    |10.10.10.11   |23|046|43 (17)     |0
|test     |10.10.10.12   |23|046|45 (10)     |0
|test1    |10.10.10.13   |23|046|89 (32)     |0" | awk  -F"|" '$0 ~ search {print $6}' search="test"| awk '{print $1}'
45
89

I do understand why. Since my search is not en exact match, it goes wrong.

I have tried some with multiple field separators, but can not get it to work
awk -F"|| " or awk -F"\|| " does not work.
How to get space and | as separators at the same time, or another solution.

If I use awk -F"|" then a search like this: $2 == search does not work, since the space after the name needs to be removed. test ok test not ok.

Hi

$ awk -F"|" -v x="test" '{sub(/ *$/,"",$2); sub(/ \(.*/,"",$6);}$2==x{print $2,$6;}' file
test 45

Guru.

Thanks. I needed only the value, so modified it some.

$ awk -F"|" -v x="test" '{sub(/ *$/,"",$2); sub(/ \(.*/,"",$6);}$2==x{print $6;}' file

Edit:
Found another solution, remove all spaces :slight_smile:

echo "|user1    |10.10.10.10   |23|046|1726 (212)  |0
|user2    |10.10.10.11   |23|046|43 (17)     |0
|test     |10.10.10.12   |23|046|45 (10)     |0
|test1    |10.10.10.13   |23|046|89 (32)     |0" |sed -e 's/ //g' | awk  -F"|" '$2 == search {print $6}' search="test"| awk -F"(" '{print $1}'
45
$ cat input
|user1    |10.10.10.10   |23|046|1726 (212)  |0
|user2    |10.10.10.11   |23|046|43 (17)     |0
|test     |10.10.10.12   |23|046|45 (10)     |0
|test1    |10.10.10.13   |23|046|89 (32)     |0
$ awk '{gsub("[|]"," ");print $1,$5}' input
user1 1726
user2 43
test 45
test1 89
$ awk '{gsub("[|]"," ");if($1~/^test$/) print $1,$5}' input
test 45
$ awk '{gsub("[|]"," ");if($1~/^test/) print $1,$5}' input
test 45
test1 89
$

I need to search for a user like test and then get only the value 45 . And the search must hit only test and not test1

This gives all users..

Edit gsub seems to run after search, so this does not work.

awk '$1=="test" {gsub("[|]"," ");print $1,$6}

I've updated my previous post, check the line :

awk '{gsub("[|]"," ");if($1~/^test$/) print $1,$5}' input

I need to have the user as a variable.
This does not work:

user="test"
awk -v inp="$user" '{gsub("[|]"," ");if($1~/^inp$/) print $1,$5}' input
awk -vU="$user" '{s="^" U "$";gsub("[|]"," ");if($1~s) print $1,$5}' input
$1~("^" inp "$")

I've just tested (ubuntu), it looks it even works without the parenthesis :

awk -v inp="$user" '{gsub("[|]"," ");if($1~"^"inp"$") print $1,$5}' input
$ user="test"
$ awk -v inp="$user" '{gsub("[|]"," ");if($1~"^"inp"$") print $1,$5}' input
test 45
$ user="test1"
$ awk -v inp="$user" '{gsub("[|]"," ");if($1~"^"inp"$") print $1,$5}' input
test1 89
$ user="tes"
$ awk -v inp="$user" '{gsub("[|]"," ");if($1~"^"inp"$") print $1,$5}' input
$

Thank you all. Works great. And I have learn some more tweaks in awk.

one more with awk..:slight_smile:

$ user="test"
$ awk -v inp="$user" -F "[|(]" '{gsub(" ","",$0);if($2 == inp) print $2,$6}' file
test 45

$ user="user2"
$ awk -v inp="$user" -F "[|(]" '{gsub(" ","",$0);if($2 == inp) print $2,$6}' file
user2 43