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 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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-14-2008
psiva_arul's Avatar
psiva_arul psiva_arul is offline
Registered User
  
 

Join Date: Jul 2007
Location: Bangalore, India
Posts: 97
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
  #2 (permalink)  
Old 05-14-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
  #3 (permalink)  
Old 05-14-2008
Vinaykumar1 Vinaykumar1 is offline
Registered User
  
 

Join Date: May 2008
Posts: 16
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
  #4 (permalink)  
Old 05-14-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Scroll back up to comment #17 if you missed that.
  #5 (permalink)  
Old 05-14-2008
Vinaykumar1 Vinaykumar1 is offline
Registered User
  
 

Join Date: May 2008
Posts: 16
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
  #6 (permalink)  
Old 05-14-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
Closed Thread

Bookmarks

Tags
regex, regular expressions, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0