The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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 08:20 AM
sorting data using array in ksh ali560045 Shell Programming and Scripting 4 12-04-2007 01:26 AM
Sorting blocks of data alfredo123 Shell Programming and Scripting 8 07-05-2007 07:53 AM
Newbie Awk data sorting i_am_a_robot Shell Programming and Scripting 5 05-04-2007 04:33 AM
Recovering lost folders/files data Yorgy UNIX for Dummies Questions & Answers 0 03-15-2007 02:46 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #15  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
It reads number.dat once, examining each line in turn.

Can't help you with the "command garbled" error; it works for me here. Is it the addition of the field separator or the full path name which is causing it confusion?

You seem to have a space between AAA and \| -- this will only match on a sequence where there really is a space between "AAA" and the vertical bar.
Reply With Quote
Forum Sponsor
  #16  
Old 05-14-2008
psiva_arul's Avatar
Registered User
 

Join Date: Jul 2007
Location: Bangalore, India
Posts: 78
Reg: split the data file based on the file header...

Hi Vinay,

Try with this script and it may solve your problem

awk '$1=="AAA" print { $0 ;}' Number.dat > AAA.dat

awk '$1=="BBB" print { $0 ;}' Number.dat > BBB.dat

Let me know if you need more details

Regards,
Siva.P
Bangalore
Reply With Quote
  #17  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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.
Reply With Quote
  #18  
Old 05-14-2008
Registered User
 

Join Date: May 2008
Posts: 13
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
Reply With Quote
  #19  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Scroll back up to comment #17 if you missed that.
Reply With Quote
  #20  
Old 05-14-2008
Registered User
 

Join Date: May 2008
Posts: 13
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
Reply With Quote
  #21  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:38 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0