awk questions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk questions
# 8  
Old 03-25-2013
Code:
sort -t"|" -k1nr -k4nr filename | awk -F"|" '$1 != index1 { print; index1=$1}'


Last edited by mstafreshi; 03-25-2013 at 04:59 PM.. Reason: change tmp to filename
# 9  
Old 03-25-2013
Quote:
Originally Posted by mstafreshi
Code:
sort -t"|" -k1nr -k4nr filename | awk -F"|" '$1 != index1 { print; index1=$1}'

Won't work : give a test with the example provided and check :

you will get 2 entries whose $1 is -10.5

This is because of the key used ... check my previous post, i have meanwhile updated it.
# 10  
Old 03-25-2013
With a single awk you could also try this which reads the input file twice to preserve the order in the input file and would have modest memory requirements:
Code:
awk -F\| 'NR==FNR{if($4>M[$1] || !($1 in M)) M[$1]=$4; next} $4==M[$1]' file file

# 11  
Old 03-26-2013
Quote:
Originally Posted by ctsgnb
Won't work : give a test with the example provided and check :

you will get 2 entries whose $1 is -10.5

This is because of the key used ... check my previous post, i have meanwhile updated it.
Code:
sort -t"|" -k1nr -k4nr filename | awk -F"|" '$1 != index1 { print; index1=$1}'

And this is oputput
Code:
-10.5|6|L|56|MR||R||012009|E
-10.75|6|L|18|||U||012008|E
-11|6|L|17|||R||012008|E
-11.3|6|L|51|MR|V|P||012009|E
-11.4|6|L|35|MR|B|R||IN2010|E
-11.85|6|L|52|MR|B|P||012009|E
-11.95|6|L|53|MR||P||012009|E
-12.4|6|L|50|MR|V|R||IN2013|E
-12.7|6|L|23|MR|B|R||IN2011|E
-13.67|6|L|18|||U||012007|E
-14|6|L|51|MR||R||IN2013|E
-14.9|6|L|22|MR|V|R||IN2011|E
-14.92|7|P|09|MPP||P|I|012009|E
-14.93|5|L|25|MPP||P||012009|E
-15.14|6|L|18|||U||012006|E
-15.18|6|L|57|MR|V|U||012009|E

test is done on freeBSD .
# 12  
Old 03-26-2013
Quote:
Originally Posted by mstafreshi
Code:
sort -t"|" -k1nr -k4nr filename | awk -F"|" '$1 != index1 { print; index1=$1}'

test is done on freeBSD .
The test i did with your code was on a linux ubuntu but a reliable code should work independently of the unix distrib.

Last edited by ctsgnb; 03-26-2013 at 05:20 AM..
This User Gave Thanks to ctsgnb For This Post:
# 13  
Old 03-26-2013
The correct way is to use -k1,1 and -k4,4 instead of -k1and -k4.
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

awk questions using sort and grep

1. The problem statement, all variables and given/known data: So i'll probably get told off for this but I have a few problems and rather than clog up the whole forum I'll post them here. Please bare in mind I am a complete novice when it comes to all this and so if you help please treat me like a... (4 Replies)
Discussion started by: jamesb18
4 Replies

2. Shell Programming and Scripting

Couple of easy questions for experts on awk/sed

Hello Experts.. I have 3-4 C codes with Oracle SQL statements embedded. All the SQL statements starts with EXEC SQL keyword and ends with ;. I want to extract all the SQL statements out of these codes. I did awk '/^EXEC SQL/,/\;/' inputFile (I use this on all of the codes individually). That... (2 Replies)
Discussion started by: juzz4fun
2 Replies

3. AIX

Simple AWK cleanup/questions AIX

I have an unfortunate need to redo a bunch of disk settings on a VIOS on AIX, so I was putting together a quick script to scrub everything it has, make the changes, and then put the mappings back. It works, I just am trying to get my awk a bit more up-to-snuff and wanted to know the proper way to... (2 Replies)
Discussion started by: Vryali
2 Replies

4. Shell Programming and Scripting

Awk/Nawk Questions

Hi Guys, This is the Input: <xn:MeContext id="XXX012"> <xn:ManagedElement id="1"> <xn:attributes> <xn:userLabel>XXX012</xn:userLabel> <xn:swVersion>R58E68</xn:swVersion> </xn:attributes> </xn:ManagedElement> </xn:MeContext>... (4 Replies)
Discussion started by: smarones
4 Replies

5. Shell Programming and Scripting

basic awk questions

I find an script with awk sitting around. I went through some online manuals, but I can't figure out exactly how it works. I can't post the whole program. Not allowed. This is the line that is confusing me. I get when else is in the script grep -v "^REM " $1| grep -v "JUNK;" | awk -F" "... (2 Replies)
Discussion started by: guessingo
2 Replies

6. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

7. Shell Programming and Scripting

Some questions about grep/awk

Hi guys. I need to filter some values from a number of log files. One of the files is: Interconnect Utilisation Results: Achieved Maximum Number of Concurrent Connections: 17 Statistics for Average Number of Concurrent Connections: Point Estimation: Confidence Interval: ... (2 Replies)
Discussion started by: Faaz0
2 Replies

8. Shell Programming and Scripting

...yet another string of awk/sed questions from a RegExp-Challenged luser %-\

Greetings all, ...here is yet another string of awk/sed questions from a RegExp-Challenged luser :eek: I'm looking to have sed/awk do some clean-up on routing tables and to that end, I would like to do the following: 1.) If a line contains the word "masks" or "subnets" prepend CR/LF to... (16 Replies)
Discussion started by: SteveB-in-LV
16 Replies

9. UNIX for Dummies Questions & Answers

Questions on AWK

Hi, Could someone tell me how to use a varible in the one of the sections of the awk command below which adjusts the spacing (i.e. "%-5s%-10s%-15s")? echo "1 2 3" | awk ' { printf("%-5s%-10s%-15s",$1,$2,$3); printf("\n"); } ' e.g. I have a variable named LENGTH which has the value 8 and I... (2 Replies)
Discussion started by: stevefox
2 Replies

10. UNIX for Dummies Questions & Answers

awk questions

I have the following script: set DAY=`date +%y%m%d` set H=`date +%H` set M=`date +%M` awk 'NR==1 {printf "MY HEADER WITH TIME STAMP $H \n"} {print $0}' data1> data2 my result is: MY HEADER WITH TIME STAMP $H ......data information.... ......data information.... How can I get... (1 Reply)
Discussion started by: bobo
1 Replies
Login or Register to Ask a Question