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.
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
No, sometimes I even know what I do 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:
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.
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?
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.
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)
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)
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)
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)
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&data3</tag3>
Now i can grep & and replace with & for whole file but it will replace all... (4 Replies)
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)
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)