Jotne
November 4, 2012, 6:42am
1
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.
Jotne
November 4, 2012, 7:44am
3
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
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
ctsgnb
November 4, 2012, 7:57am
4
$ 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
$
Jotne
November 4, 2012, 8:07am
5
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}
ctsgnb
November 4, 2012, 8:10am
6
I've updated my previous post, check the line :
awk '{gsub("[|]"," ");if($1~/^test$/) print $1,$5}' input
Jotne
November 4, 2012, 8:41am
7
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
ctsgnb
November 4, 2012, 8:59am
8
awk -vU="$user" '{s="^" U "$";gsub("[|]"," ");if($1~s) print $1,$5}' input
ctsgnb
November 4, 2012, 9:23am
10
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
$
Jotne
November 4, 2012, 11:19am
11
Thank you all. Works great. And I have learn some more tweaks in awk.
pamu
November 5, 2012, 1:31am
12
one more with awk..
$ 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