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 12: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 01:46 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: May 2008
Posts: 13
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Sorting data and place them in different folders

Hello Unix gurus,
I am new to Unix. I am working on some dummy project which involves unix scripting.

I have a query :

There is a file Number.dat which is of form say

AAA|123|4563|animal
AAA|1234|45634|animal2
BBB|123444|456312|bird
BBB|123445|456313|bird2

Here AAA,BBB are headers.
Now I want to sort Number.dat based on header and place them in different header.dat file.
How do I do that? I would be really greatful if some one helps me.


Thanks in advance
Vinay
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-13-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,387
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
What have you done to attempt to solve this problem yourself?
Post your sample script, and we'll see how we can assist.

Regards
Reply With Quote
  #3 (permalink)  
Old 05-13-2008
psiva_arul's Avatar
Registered User
 

Join Date: Jul 2007
Location: Bangalore, India
Posts: 75
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
using awk we can achive your solution.

using awk we can get the solution for your problem


if $1 ='AAA' we can redirect it into new file AAA.dat
and if $1 ='BBB' we can redirect it into new file BBB.dat


please use this way to get the solutions

Regards,
Siva.P
Bangalore
Reply With Quote
  #4 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: May 2008
Posts: 13
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hello Franklin / Unix gurus,

My script is as follows:


#!/bin/ksh
sys=/export/home/vinay/Number.data
mod1=AAA
mod2=BBB

grep -h 'AAA' $sys > /export/home/vinay/AAA_$mod1.data
grep -h 'BBB' $sys > /export/home/vinay/BBB_$mod2.data


But I am not happy with the script since it has to traverse twice imto the Number.dat file


Also I some questions,

I want the process to use the Number.dat once rather than traversing twice.
Also, say if memory is full while script is running, then how should I handle it.
Kindly reply back if any one has a suggestion/script/solution

Thanks in advance.

Regards,
Vinay

Last edited by Vinaykumar1; 05-13-2008 at 10:18 PM.
Reply With Quote
  #5 (permalink)  
Old 05-13-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Both awk and sed have facilities for writing into multiple files as they read in a single pass down the file. psiva_arul is hinting in the same direction in the posting above.
Reply With Quote
  #6 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: May 2008
Posts: 13
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
hello era /Unix gurus,
If you don't mind, can you assist in which fashion I can make use of sed into my script.

Thanks and Regards,
Vinay

Last edited by Vinaykumar1; 05-13-2008 at 10:43 PM.
Reply With Quote
  #7 (permalink)  
Old 05-13-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Googling is not an option?

Code:
sed -n '/^AAA/wAAA.dat
/^BBB/wBBB.dat' file
Notice that writing doesn't have any side effect (such as, for example, finish this line and fetch the next) so if you have patterns which overlap, you can get the same line written to multiple files (or need to write a slightly more complex script).
Reply With Quote
  #8 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: May 2008
Posts: 13
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hello era / unix gurus,

I tried the following:
sed -e "x;/^AAA/b" -e "/^BBB/b" -e d number.dat

It works. But I need to divert it to different files .How do I do it. Kindly assist if possible

Thanks and Regards,
Vinay
Reply With Quote
  #9 (permalink)  
Old 05-14-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Did you try the script I posted above? If so, what's wrong with it? It writes lines matching "^AAA" to AAA.dat and lines matching the regular expression "^BBB" to BBB.dat, is that not what you want?
Reply With Quote
  #10 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: May 2008
Posts: 13
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hello era,

The script u had sent did not work.
Its giving an error
sed: command garbled: /^AAA/wAAA.dat/^BBB/wBBB.dat

That is why I tried
sed -e "x;/^AAA/b" -e "/^BBB/b" -e d number.dat
I am getting answer properly, but I dont know how to route it to different files.
Reply With Quote
  #11 (permalink)  
Old 05-14-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
The script should be on two different lines. You could also try

Code:
sed -e '/^AAA/wAAA.dat' -e '/^BBB/wBBB.dat' number.dat
The sed w command takes a file name to write the line to.
Reply With Quote
  #12 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: May 2008
Posts: 13
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hi era,
sed -e '/^AAA/w AAA.dat' -e '/^BBB/w BBB.dat' number.dat works..
Thanks a lot.
Can I specify the path where it should be stored:
sed -e '/^AAA/w /export/home/vinay/AAA1.dat' -e '/^BBB/w /export/home/vinay/BBB1.dat' a.dat
I hope there is some error

Also does sed provides a way to search specific fields, like the -f1 (1st field) option

Also does the sed -e '/^AAA/w AAA.dat' -e '/^BBB/w BBB.dat' number.dat traverses through the number.dat once or twice.

.. Kindly assist me if possible
Thanks and Regards,
Vinay

Last edited by Vinaykumar1; 05-14-2008 at 01:47 AM.
Reply With Quote
  #13 (permalink)  
Old 05-14-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
sed uses regular expressions only, you can create a regex to look at the first field only with regex constructs; the ^ is already halfway there, as it forces the match to happen at beginning of line. Suppose the field separator is a vertical bar; then you can just add that after the string you want to search for, to anchor it properly.

Code:
sed -e '/^AAA|/w/export/home/vinay/AAA1.dat' -e '/^BBB|/w/export/home/vinay/BBB1.dat' a.dat
This finds "AAA" followed by vertical bar, but only if it is at beginning of line (because of the ^) and ditto for "BBB".

The vertical bar has special meaning to some regular expression engines, just like the ^ -- if you get erratic behavior (all lines matching all the time) then you need to backslash-quote it, like \|. It is unfortunate that there are different dialects of sed so that we can't know for sure whether or not this is an issue in your case.

Last edited by era; 05-14-2008 at 01:52 AM. Reason: Separator is vertical bar, as per above
Reply With Quote
  #14 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: May 2008
Posts: 13
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hello era / unix gurus,

I tried the above script but its giving me
sed: command garbled: /^AAA \|/w/export/home/cmohanku/vinay/AAA1.dat

"Also does the sed -e '/^AAA/w AAA.dat' -e '/^BBB/w BBB.dat' number.dat traverses through the number.dat once or twice."


Please help me if possible

Thanks and Regards,
Vinay
Reply With Quote
  #15 (permalink)  
Old 05-14-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Google UNIX.COM
Reply

Tags
solaris

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash for loop boot: cannot open kernel/sparcv9/unix command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix export command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device