LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 08-05-2010, 05:49 AM   #1
keenboy
Member
 
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 36

Rep: Reputation: 15
How to extract lines from file using AWK


Hi,

I have a file similar to this:

david,1,1,1,1,50,7
david,1,1,1,1,65,8
david,1,1,1,1,89,12
thomas,1,1,1,1,40,2
bill,1,1,1,1,39,3
bill,1,1,1,1,41,4

I need to print the last line for each user into a file. The resulting file should look like this:

david,1,1,1,1,89,12
thomas,1,1,1,1,40,2
bill,1,1,1,1,41,4

Is there a way that AWK can match lines from $1 and then print the last line into a file?

Or is there a better way?

Many Thanks,
 
Old 08-05-2010, 05:52 AM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
This looks like it might possibly be a homework program. Could you explain how you will use this, or show what you have tried so far. Then we will be able provide hints. If this is something you can use, you could tools such as cut, sort & tail to achieve the same thing.

Last edited by jschiwal; 08-05-2010 at 06:01 AM.
 
Old 08-05-2010, 06:07 AM   #3
keenboy
Member
 
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
The file is a csv file which is exported from a website. The file contains delivery information and the last line for each name is the line that is required to print delivery labels.

The final file is imported into an application which prints the labels.

I have been able to print the required columns from an original csv file into a new file and delete repeated lines by using the uniq command. This wont work here though because the lines are all different.

I want to be able to use the awk match function to match lines based on a pattern once the lines have been matched I should be able to print the last line into a new file.

The patterns will always be different so I will have to match the lines based on the contents of column 1 being the same.

I hope this helps.

Thanks,
 
Old 08-05-2010, 06:17 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
I'm not as handy with AWK as I am with SED.

However, tweaking what you did before should work. Use cut to obtain a list of names to match. Then use each name for grep or awk to match against.

cut -d, -f1 test | sort -u >names
for name in $(cat names); do
grep $name test | tail -1; done
 
Old 08-05-2010, 06:51 AM   #5
keenboy
Member
 
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks for that. The result isn't what was expected. I'm getting all the lines as output instead of the last line for each name.

I think I need to keep looking at AWK functions, match in particular.
 
Old 08-05-2010, 06:53 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Is this what you are looking for:
Code:
#!/bin/bash

sort infile | \
awk 'BEGIN { FS = "," ; prevUser = "" }
{ if ( prevUser != $1 ) {
      print $0
  }
  prevUser = $1
}
'
Example run:
Code:
$ cat infile
david,1,1,1,1,50,7
bill,1,1,1,1,39,3
david,1,1,1,1,89,12
thomas,1,1,1,1,40,2
david,1,1,1,1,65,8
bill,1,1,1,1,41,4

$ ./one.only.sh 
bill,1,1,1,1,39,3
david,1,1,1,1,50,7
thomas,1,1,1,1,40,2
Hope this helps.
 
1 members found this post helpful.
Old 08-05-2010, 07:19 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
No order though
Code:
awk -F"," '{a[$1]=$0}END{for(i in a) print i,a[i]}' file
 
1 members found this post helpful.
Old 08-05-2010, 08:29 AM   #8
keenboy
Member
 
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
Thank you both. They work well. ghostdog74 perfect order, thank you again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Extract multiple lines of data from a text file. shawnamiller Programming 8 04-30-2010 11:46 AM
Extract uncommented lines from a file aarav2306 Linux - Newbie 6 07-11-2009 11:27 AM
Extract lines NOT on a block of text from a file Renan_S2 Programming 3 10-05-2008 04:14 PM
how to extract certain lines from a log file Avatar Linux - Newbie 3 02-11-2005 09:51 AM
Getting awk to extract scripts from a file jspaceman Programming 5 11-24-2002 06:37 PM


All times are GMT -5. The time now is 12:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration