![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Sorting your data with msort | iBot | UNIX and Linux RSS News | 0 | 05-19-2008 12:20 PM |
| sorting data using array in ksh | ali560045 | Shell Programming and Scripting | 4 | 12-04-2007 04:26 AM |
| Sorting blocks of data | alfredo123 | Shell Programming and Scripting | 8 | 07-05-2007 11:53 AM |
| Newbie Awk data sorting | i_am_a_robot | Shell Programming and Scripting | 5 | 05-04-2007 08:33 AM |
| Recovering lost folders/files data | Yorgy | UNIX for Dummies Questions & Answers | 0 | 03-15-2007 05:46 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Syntax errors notwithstanding, that obviously reads the file multiple times. But yes, if you have trouble with sed, then awk is also an option. Code:
awk -F '|' '$1 == "AAA" { print >"/path/to/AAA.dat"; next }
$1 == "BBB" { print > "/path/to/BBB.dat"; next }' numbers.dat
awk doesn't particularly mind if you fail to enter that as two separate lines. Take out the "next" commands if you have lines which should end up in both files somehow. Like sed, awk reads the input file once only, so by putting the commands in a single script we manage to meet your requirement. |
|
||||
|
Hello Shiva,
Its giving an error : awk: syntax error near line 1 awk: bailing out near line 1 Also as you have said, awk '$1=="AAA" print { $0 ;}' Number.dat > AAA.dat awk '$1=="BBB" print { $0 ;}' Number.dat > BBB.dat If I am correct, need to give these scripts seperately. I have some concerns, I dont want to run thro' number.dat repeatedly. I am planning to make a script that allows to sort and move data to respective files without traversing thro' it again and again. Kindly assist if possible, Thanks and regards, Vinay |
|
||||
|
hi era, shiva,unix gurus,
I tried the awk present in comment #17. awk -F '|' '$1 == "AAA" { print >"/path/to/AAA.dat"; next } $1 == "BBB" { print > "/path/to/BBB.dat"; next }' numbers.dat Its giving an error. awk: syntax error near line 1 awk: bailing out near line 1 I am not able to figure out what the error is... You guys have made my day... Now I have started going thro' awd basics and sed basics. Truly great concepts... Please assist me if possible Thanks and Regards, Vinay |
|
||||
|
Perhaps at this point it would make sense to step back and tell us what platform you're on and whether you can figure out the versions of sed and awk you have at your disposal. If you're on something like HP-UX or Irix and you have very old versions of both awk and sed, maybe a Perl script would be the best solution. If you're on HP-UX or Solaris then there may be more recent versions of these commands if you look around for a bit. Search these forums for xpg4 and the name of your platform for some pointers. Maybe you have a command nawk or mawk or gawk which would work better than just awk. Here's a Perl script, for the heck of it. Code:
perl -ne 'if (m/^(AAA|BBB)\|/) { open (H, ">>$1.dat"); print H; close H; }' numbers.dat
This isn't very efficient because it will open a new file handle for each match. It could be improved but it's just a proof of concept anyway. |
![]() |
| Bookmarks |
| Tags |
| regex, regular expressions, solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|