Sorting data and place them in different folders


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sorting data and place them in different folders
# 15  
Old 05-14-2008
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.
# 16  
Old 05-14-2008
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
# 17  
Old 05-14-2008
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.
# 18  
Old 05-14-2008
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
# 19  
Old 05-14-2008
Scroll back up to comment #17 if you missed that.
# 20  
Old 05-14-2008
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
# 21  
Old 05-14-2008
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying text file records, find data in one place in the record and print it elsewhere

Hello, I have some text data that is in the form of multi-line records. Each record ends with the string $$$$ and the next record starts on the next line. RDKit 2D 15 14 0 0 0 0 0 0 0 0999 V2000 5.4596 2.1267 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

How to merge variable data from another file into specific place?

Hello, I'm trying to create multiple commands using a variable input from another file but am not getting any successful results. Basically, file1.txt contains multiple lines with single words: <file1.txt> yellow blue black white I want to create multiple echo commands with these... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

search data from text file in different folders

I am fairly new to unix scripting, the problem is i can understand the unix script. but i fail to write. I do not know where to start and how to end. I am sure this forum will help to achive my dream scriptings in unix. Thanks in adv for your help. Here I need.. I have list of columns in one... (2 Replies)
Discussion started by: dsnrhdy
2 Replies

4. Shell Programming and Scripting

Sorting the Data

My actual data looks like below i have given only format. i can't give exact data format of my requirement due to some reasons. I this set of data lines about 5000 I need to come up with information in below exact format of my data set : Line<space>Number1<space>"somedata":... (1 Reply)
Discussion started by: ckaramsetty
1 Replies

5. UNIX for Dummies Questions & Answers

Help with Data Sorting

Hi All, I have a long list made of 4 columns containing entries such as the following example: a b c d 0 0 0 0 1 2 1 2 2 5 3 4 3 8 4 6 4 10 9 8 5 15 8 10So the top row is the header and I need to arrange the data in a way as to... (11 Replies)
Discussion started by: pawannoel
11 Replies

6. Shell Programming and Scripting

sorting files or folders

I have the small script to arrange files of a descending way. ls -l |sort -r -k4 i wanted for example if I place -d one arranges only the folders or -a to arranges only the files. Cheers (7 Replies)
Discussion started by: krlos07
7 Replies

7. UNIX for Dummies Questions & Answers

Sorting data

Hello guys. I need help figuring this one out. It's probably really easy. Thanks in advance! I have a file say for example containing this: Rice Food Carrots Food Beans Food Plates Kitchen Fork Kitchen Knives Kitchen I need: Food Rice, Carrots, Beans Kitchen Plates, Fork,... (7 Replies)
Discussion started by: visuelz
7 Replies

8. Solaris

What is the best way to copy data from place to another place?

Dear Gurus, I need you to advice or suggestion about the best solution to copy data around 200-300G from serverA(location A) to serverB(location B). Normally, I will share folder and then copy but it takes too long time(about 2 days). Do you have any suggestion or which way should be... (9 Replies)
Discussion started by: unitipon
9 Replies

9. UNIX for Dummies Questions & Answers

Sorting data from a to z

Hi, Let's say I have these 3 columns; NGC1234 6 9 SL899 4 1 NGC1075 8 3 SL709 5 2 And I want to sort the data according to the first column (from a to z) like having them as: NGC1075 8 3 NGC1234 6 9 SL709 5 2 SL899 4 1 Can that be done... (2 Replies)
Discussion started by: cosmologist
2 Replies

10. UNIX for Dummies Questions & Answers

sorting data from who by IP

Hello. I have an RS/6000 running AIX 4 and I need to be able to see if there are any users that are logged on more than once from the same terminal so I can kick them off to make room for other terminals. 64 connections is the limit. Currently I am doing this: who | more and then manually... (11 Replies)
Discussion started by: raidzero
11 Replies
Login or Register to Ask a Question