Search for & edit rows & columns in data file and pipe


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search for & edit rows & columns in data file and pipe
# 1  
Old 03-26-2009
Data Search for & edit rows & columns in data file and pipe

Dear unix gurus,

I have a data file with header information about a subject and also 3 columns of n rows of data on various items he owns. The data file looks something like this:

adam peter
blah blah blah
blah blah blah

car

01 30 200
02 31 400
03 57 121
.. .. ..
.. .. ..
n y z

bicycle

01 21 888
02 25 213
03 35 414
.. .. ...
n y z

van

... ... ...
... ... ...
n x y

What I would like to do is write a script to loop over all the items i.e. car, bicycle and van, create a new file named after the item and pipe in all the 3 columns X n rows of data associated with each item into the file. So I should end up with 3 files called car, bicycle, van, each with its associated data only. Every item has exactly 3 columns and n rows of data.
I'm struggling...can someone help? thanks a lot.
# 2  
Old 03-26-2009
Code:
awk 'NF == 1 {n=$1} NF > 1 {print $0 >> n}' infile

# 3  
Old 03-26-2009
thanks. but your suggestion did not help. The first thing is that I get the error message "( FILENAME=xxx.dat FNR=1) fatal: expression for >> has null string value"
The second this is that it seems that you are trying to pipe the contents of $0 into n. In which case we would end up with only a single file. What I want is 3 separate files e.g

file 1: car

01 30 200
02 31 400
03 57 121
.. .. ..
.. .. ..
n y z

file 2 :car

01 30 200
02 31 400
03 57 121
.. .. ..
.. .. ..
n y z

and so on. thanks.
# 4  
Old 03-27-2009
No, sometimes I even know what I do Smilie Usually I test a solution out before I post it. I am usually trying not to guess by the description the posters give.

The thing is that you might use another version of awk than I do. I use gawk.
A quote I always have to search which might help you:
Quote:
Use nawk or /usr/xpg4/bin/awk on Solaris
To explain what it does:
If the line has only 1 field it will assing field 1 to n. If the line has more than 1 field, it will write the line to the file named n. It does that for every line.

Here is what I get with the solution I posted - I altered your example a bit but it stays the same like what you posted as input:
Code:
$> cat infile2
car

 1 2 3
 4 5 6
 7 8 9

bicycle

 10 11 12
 13 14 15
 16 17 18

van

 21 22 23
 24 25 26
 27 28 29
$> awk 'NF == 1 {n=$1} NF > 1 {print $0 >> n}' infile2
$> ll car bicycle van
-rw-r--r-- 1 root root 30 2009-03-27 09:30 bicycle
-rw-r--r-- 1 root root 21 2009-03-27 09:30 car
-rw-r--r-- 1 root root 30 2009-03-27 09:30 van
$> cat bicycle
 10 11 12
 13 14 15
 16 17 18
$> cat car
 1 2 3
 4 5 6
 7 8 9
$> cat van
 21 22 23
 24 25 26
 27 28 29

The output is 3 different files each containing the rows that are contained under the "filename" in the single original file.

Also please use code tags next time posting code, logs or data etc.

Last edited by zaxxon; 03-27-2009 at 05:53 AM..
# 5  
Old 03-27-2009
yes, that is exactly what I want. I think the problem is that we are using different platforms. the quote use nawk doesn't work in my shell.

My shell type is bash-3.2. my GNU awk version is 3.1.6. Same applies for gawk. Would you know what the equivalent command of your script is for bash shell?
# 6  
Old 03-27-2009
The solution of zaxxon should work with gawk. Post a part of your input file within code tags, select the text with your mouse and click on the # above the edit box.

Regards
# 7  
Old 03-27-2009
I'm not sure what you mean Franklin52, I'm new in the forum. Smilie How will wrapping my input file within code tags help me?


Code:
 
peter adams
40 the for five
car
01 1.1 1.96
02 31 55.2
03 29 23.4
04 27 24.7
05 25 54
06 23 55
 
bicycle
 
01 42 100
02 32.1 44.66
03 33 44.1
04 45.8 54.2
05 66 53.1
06 48 22

what do I do next?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove new line characters from data rows in a Pipe delimited file?

I have a file as below Emp1|FirstName|MiddleName|LastName|Address|Pincode|PhoneNumber 1234|FirstName1|MiddleName2|LastName3| Add1 || ADD2|123|000000000 2345|FirstName2|MiddleName3|LastName4| Add1 || ADD2| 234|000000000 OUTPUT : ... (1 Reply)
Discussion started by: styris
1 Replies

2. Shell Programming and Scripting

search & merg data from 3 files

i have 3 files which contains as below (example): yy-mm-dd hh:mm:sec lat lon depth mag 2006-01-01 23:17:26.80 39.8405 41.8795 2.0 3.3 2006-01-06 00:10:26.80 39.9570 41.2130 5.0 3.3 2006-01-06 06:59:02.10 39.4099 44.6065 10.0 3.7 2006-01-06 13:49:52.70... (4 Replies)
Discussion started by: oreka18
4 Replies

3. Shell Programming and Scripting

Perl script to get info from specific rows & columns (.xls file)

Hi all, I want to read some specific rows & columns in the .xls file with my script to get the data to be manipulated. Now, I can read the .xls file correctly & i can go to the specific sheet that I want but i have a problem to specify the specific rows & columns. I mean, I want to get the info... (0 Replies)
Discussion started by: Yohannita
0 Replies

4. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

5. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

6. Shell Programming and Scripting

Script to Check & Edit Content of a file (Addition of comma in each lines of code)

Hi all, I need to write an automated bash shell script which performs such operations: 1. Grep the header of everyline with the initial of "T" in "FILE_A" 2. Perform a for loop, Count the numbers of comma in the line of code, if (no. of comma < 17) ADD the comma until 17; ... (2 Replies)
Discussion started by: big_nutz
2 Replies

7. Shell Programming and Scripting

replace & with &amp; xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

8. UNIX for Dummies Questions & Answers

selective concatenation of rows & columns

Dear unix gurus, I have a data file that looks something like this ... x y x y x y x y x y 0 3836 30 3915 60 5984 90 7388 120 8385 150 9038 180 9453 210 9745 240 9906 270 9962 300 9953 330 9915 350 9887 ... (22 Replies)
Discussion started by: tintin72
22 Replies

9. Shell Programming and Scripting

How do I search first&second string & copy all content between them to other file?

Hi All, How do I search first string & second string and copy all content between them from one file to another file? Please help me.. Thanks In Advance. Regards, Pankaj (12 Replies)
Discussion started by: pankajp
12 Replies

10. Shell Programming and Scripting

deleting rows & columns form a csv file

Hi , I want to delete some rows & columns from file. can someone please help me on this? Regards. (2 Replies)
Discussion started by: code19
2 Replies
Login or Register to Ask a Question