Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pair lines together

Status
Not open for further replies.

viadisky

Technical User
Jun 19, 2003
110
GB
Hi,

I have this raw data:
maria olan 4
olan maria 5
maria danie 9
danie maria 10

Based from this data, I can say that "maria olan 4" and "olan maria 5" lines are pair. I did create a simple awk that would display either of these lines showing the one with the maximum value in the last column. In this case, it would be "olan maria 5".

This is the awk command I created:
Code:
cat file | awk '/olan maria|maria olan/ {print $0}' | \
awk '$3 > max {max=$3; maxline=$0}; END{ print maxline}'

Ii just need an easy way to feed a variable in my regex, example in this case is /olan maria|maria olan/.

Hope somebody can help me.

Many thanks!

 
Hi

I would do it this way :
Code:
awk -vname='[green][i]maria olan[/i][/green]' 'function can(n){split(n,a);asort(a);s=a[1];for(i=2;i in a;i++)s=s" "a[i];return s}BEGIN{n=can(name)}n==can($1 FS $2) && [gray]$3>max{max=$3;maxline=$0}END{print maxline}[/gray]' /input/file
Advantages :
[ul]
[li]enter the name only once[/li]
[li]easy to use with names like Jean Claude Van Damme[/li]
[/ul]
Modifying your code, the keyword is && :
Code:
awk '/olan maria|maria olan/ && $3 > max {max=$3; maxline=$0}; END{ print maxline}' file
( Please not that your code is subject of UUOC police prosecution. )

Feherke.
 
Thanks Feherke,

I'm looking at code you provided ... still trying to undertsand line by line.

By the way, what do you mean by this line:
( Please not that your code is subject of UUOC police prosecution. )

Best regards!
 
feherke said:
( Please not that your code is subject of UUOC police prosecution. )
Yes, I am still lurkin' around......

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi

In your code
[ul]
[li][tt]cat[/tt] reads the data from the file and output it to the standard output[/li]
[li]the shell captures its output and pass it to [tt]awk[/tt] as input[/li]
[li][tt]awk[/tt] reads the data from the standard input[/li]
[/ul]
This is waste of processor time and memory. And implicitly runs slower. So is better to
[ul]
[li][tt]awk[/tt] reads the data from the file[/li]
[/ul]
UUOC = Useless Use Of [tt]cat[/tt]

Regarding the involvement of police, is just a saying.

And a typo : "( Please not[red]e[/red] that your code is subject of UUOC police prosecution. )".

Feherke.
 
Hi Feherke,

I just typed the code you provided and this is what I can see in my screen:

I made sure when I do the vi script, everything is one line.

Code:
zen:/u/mviado$ more script
awk -vname='maria olan' 'function can(n){split(n,a);asort(a);s=a[1];for(i=2;i in a;i++)s
=s" "a[i];return s}BEGIN{n=can(name)}n==can($1 FS $2) && $3>max{max=$3;maxline=$0}END{print maxline}' file

zen:/u/mviado$ sh -x script
+ awk -vname=maria olan function can(n){split(n,a);asort(a);s=a[1];for(i=2;i in a;i++)s=s" "a[i];return s}BEGIN{n=can(name)}n==can($1 FS $2) && $3>max{max=$3;maxline=$0}END{print maxline} file 
awk: syntax error near line 1
awk: bailing out near line 1
zen:/u/mviado$

I will look into that awk option -v command

Thanks again!
 
How about this alternative:

Code:
awk '
$2 > $1 { t=$2; $2=$1; $1=t }
{ if ($3 > a[$1 OFS $2]) a[$1 OFS $2]=$3 }
END { for (i in a) { print i,a[i] } }'

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top