![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pulling a file off a backup tape | rocker40 | UNIX for Dummies Questions & Answers | 14 | 10-12-2007 12:37 PM |
| Pulling data and following lines from file | MizzGail | Shell Programming and Scripting | 2 | 01-31-2006 03:13 PM |
| How to add fields in a file | bjorb | Shell Programming and Scripting | 2 | 10-13-2005 09:11 AM |
| pulling a column from a file in ksh | dangral | Shell Programming and Scripting | 8 | 01-13-2003 04:10 PM |
| pulling the following line from a file | peter.herlihy | UNIX for Dummies Questions & Answers | 4 | 08-29-2002 10:09 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Hi,
I have a file that contains 1400 lines similar to the one shown below: NAME=sara, TOWN=southampton, POSTCODE=SO18777, EMAIL=sara@hotmail.com, PASSWORD=asjdflkjds etc etc (note: this is one line). Each line has the same fields, but on each line they are in a different order. Eg. the line beneath the one shown above is: TOWN=southampton, PASSWORD=asjdflkjds, NAME=sara, EMAIL=sara@hotmail.com, POSTCODE=SO18777 I want to be able to pull out only the POSTCODE and NAME fields (including everything up to the comma) from this file and place it into another. However, using sed/awk I have not been able to do this. Any ideas? Last edited by Saz; 09-28-2001 at 01:20 PM.. |
|
||||
|
Another method, 3 commands, a little bit longer, but easier to understand. Here I assume there are 5 fields in the source file,
(awk -F "," '{print FNR, $1}' filename ; awk -F "," '{print FNR, $2}' filename ; awk -F "," '{print FNR, $3}' filename ; awk -F "," '{print FNR, $4}' filename ; awk -F "," '{print FNR, $5}' filename) | grep "NAME=" | sort > tab1 (awk -F "," '{print FNR, $1}' filename ; awk -F "," '{print FNR, $2}' filename ; awk -F "," '{print FNR, $3}' filename ; awk -F "," '{print FNR, $4}' filename ; awk -F "," '{print FNR, $5}' filename) | grep "POSTCODE=" | sort > tab2 join tab1 tab2 "awk" outputs the fields with line number (record number), then we "join" them together by line numebr. Just like we make a query / joint between 2 tables, "select tab1.NAME, tab2.POSTCODE from tab1, tab2 where tab1.linenumber=tab2.linenumber" ![]() Last edited by eddie; 09-30-2001 at 05:22 PM.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|