Visit Our UNIX and Linux User Community


Help to get correct data using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to get correct data using awk
# 1  
Old 11-04-2012
Help to get correct data using awk

I have this input.
Code:
|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
Code:
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
Code:
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:
Code:
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.
# 2  
Old 11-04-2012
Hi

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

Guru.
# 3  
Old 11-04-2012
Thanks. I needed only the value, so modified it some.
Code:
$ awk -F"|" -v x="test" '{sub(/ *$/,"",$2); sub(/ \(.*/,"",$6);}$2==x{print $6;}' file

Edit:
Found another solution, remove all spaces Smilie
Code:
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


Last edited by Jotne; 11-04-2012 at 08:54 AM..
# 4  
Old 11-04-2012
Code:
$ 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
$


Last edited by ctsgnb; 11-04-2012 at 09:06 AM..
# 5  
Old 11-04-2012
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.
Code:
awk '$1=="test" {gsub("[|]"," ");print $1,$6}

# 6  
Old 11-04-2012
I've updated my previous post, check the line :
Code:
awk '{gsub("[|]"," ");if($1~/^test$/) print $1,$5}' input

# 7  
Old 11-04-2012
I need to have the user as a variable.
This does not work:
Code:
user="test"
awk -v inp="$user" '{gsub("[|]"," ");if($1~/^inp$/) print $1,$5}' input


Previous Thread | Next Thread
Test Your Knowledge in Computers #397
Difficulty: Medium
HP-UX 11i offers a common shared disks for its clustered file system.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk output is not the correct count

The awk below runs and produces the following output on the file2. This is just an example of the format as the file is ~14MB. file1.txt is attached. I am trying to count the ids that match between the two files and out the ids that are missing. Thank you :). file2 970 NM_213590 ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Programming

Correct way to read data of different formats into same struct

I was wondering what is the correct way to read in data "one-part-per-line" as compared with "one-record-per-line" formats into the same structure in C? format1.dat: Zacker 244.00 244.00 542.00 Lee 265.00 265.00 456.00 Walter 235.00 235.00 212.00 Zena 323.00 215.45 ... (12 Replies)
Discussion started by: yifangt
12 Replies

3. Shell Programming and Scripting

Shell script to correct the data

Hi, I have below data in my flat file.I would like to remove the quotes and comma necessary from the data.Below is the details I would like to have in my output. Could anybody help me providing the Unix shell script for this. Input : ABC,ABC,10/15/2012,"47,936,164.567 ","1,036,997.453... (2 Replies)
Discussion started by: sonu_pal
2 Replies

4. Shell Programming and Scripting

How to correct this awk code without eval?

Hi everyone, The following piece of awk code works fine if I use eval builtin var='$1,$2' ps | eval "awk '{print $var}'" But when I try to knock off eval and use awk variable as substitute then I am not getting the expected result ps | awk -v v1=$var '{print v1}' # output is $1,$2 ps |... (4 Replies)
Discussion started by: royalibrahim
4 Replies

5. Shell Programming and Scripting

Cannot get the correct ans. Using awk in taking average

Hi all, I think so I’m getting the result is wrong, while using following awk commend, colval=$(awk 'FNR>1 && NR==FNR{a=$4;next;} FNR>1 {a+=$4; print $2"\t"a/3}' filename_f.tsv filename_f2.tsv filename_f3.tsv) echo $colval >> Result.tsv it’s doing the condition 2 times, first result... (5 Replies)
Discussion started by: Shenbaga.d
5 Replies

6. Shell Programming and Scripting

Using AWK to Calculate Correct Responses

Hello, I am trying to count how many times a subject makes a correct switch or a correct stay response in a simple task. I have data on which condition they were in (here, labeled "IMAGINE" and "RECALL"), as well as whether they made a left or right button response, and whether the outcome was... (5 Replies)
Discussion started by: Jahn
5 Replies

7. Shell Programming and Scripting

AWK or SED for correct columns

Hello, I have the following file, but one of his columns is not in place, and tried with SED and AWK, how I can correct format? In the second line break is wrong, and puts it after the first column of next line I would appreciate if you could guide me on the subject. (4 Replies)
Discussion started by: nitwh
4 Replies

8. Shell Programming and Scripting

Awk error -- awk: 0602-562 Field $() is not correct.

typeset -i i=1 while read -r filename; do Splitfile=`$Targetfile_$i.txt` awk 'substr($0,1,5) == substr($filename,1,5) && substr($0,526,2) == substr($filename,6,2) && substr($0,750,12) == substr($filename,8,12)' $SourceFilename >> $Splitfile i=i+1 done < /tmp/list.out I am using this logic... (1 Reply)
Discussion started by: pukars4u
1 Replies

9. Shell Programming and Scripting

AWK not giving me correct output.

i have a line like this in my script IP=`get_IP <hostname> | awk '{ print $1 }' echo $IP the problem is get_IP <hostname> returns data formated as follows: ip 1.1.1.1 name server_name the code above returns 1.1.1.1 server_name and i just need the 1.1.1.1 I have tried to add "|... (5 Replies)
Discussion started by: mcdef
5 Replies

10. Shell Programming and Scripting

Conversion of Exponential to numeric in awk- not correct output

Hi All, I have 1 million records file. Using awk, I am counting the number of records. But as the number is huge, after crossing a number, awk is displaying it in exponential format. At the end, I need to verify this count given by awk with expected count. But as it is in exponential format,... (3 Replies)
Discussion started by: ssunda6
3 Replies

Featured Tech Videos