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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-05-2010, 05:49 AM
|
#1
|
|
Member
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 34
Rep:
|
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,
|
|
|
|
08-05-2010, 05:52 AM
|
#2
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
08-05-2010, 06:07 AM
|
#3
|
|
Member
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 34
Original Poster
Rep:
|
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,
|
|
|
|
08-05-2010, 06:17 AM
|
#4
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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
|
|
|
|
08-05-2010, 06:51 AM
|
#5
|
|
Member
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 34
Original Poster
Rep:
|
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.
|
|
|
|
08-05-2010, 06:53 AM
|
#6
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,713
|
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.
|
08-05-2010, 07:19 AM
|
#7
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
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.
|
08-05-2010, 08:29 AM
|
#8
|
|
Member
Registered: Jan 2008
Location: Cullompton
Distribution: Kubuntu
Posts: 34
Original Poster
Rep:
|
Thank you both. They work well. ghostdog74 perfect order, thank you again.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:16 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|