[SOLVED] re-parse duplicate horizontally


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [SOLVED] re-parse duplicate horizontally
# 1  
Old 10-04-2012
[SOLVED] re-parse duplicate horizontally

I have a 2 column file.
Column 1 is a road ID number.
Column 2 is the date that the road surface condition was inspected.
Some roads have been inspected only once, while some roads have been inspected multiple times.

I would like to re-arrange the following example list:

Code:
1001 2001_01_01
1002 2001_01_01
1002 2002_02_02
1003 2001_01_01
1003 2002_02_02
1004 2001_01_01
1005 2001_01_01
1006 2001_01_01
1006 2002_02_02
1006 2003_03_03
1007 2001_01_01

to look like:

Code:
1001 2001_01_01
1002 2001_01_01 2002_02_02
1003 2001_01_01 2002_02_02
1004 2001_01_01
1005 2001_01_01
1006 2001_01_01 2002_02_02 2003_03_03
1007 2001_01_01

The input file may not be in a nice neat order as shown in the example.

Thanks,
Kenny.

Last edited by Scott; 10-04-2012 at 03:13 PM.. Reason: Code tags
# 2  
Old 10-04-2012
Code:
awk '{x[$1] = x[$1] ? x[$1]""$2 : $2} END {for(i in x) print i, x[i]}' file

# 3  
Old 10-04-2012
Thanks Shamrock,

I am not getting spaces between all of the dates.
I get:

Code:
1001 2001_01_01
1002 2001_01_012002_02_02
1003 2001_01_012002_02_02
1004 2001_01_01
1005 2001_01_01
1006 2001_01_012002_02_022003_03_03
1007 2001_01_01

Note that it did not work at all when I used awk.
The output above is from mawk.

And I should add that my OS is not UNIX, it is Windows Smilie

Thanks,
Kenny.

Last edited by Scott; 10-04-2012 at 03:33 PM.. Reason: Code tags
# 4  
Old 10-04-2012
How are you using awk? Are you running it in a proper shell, or are you using it in windows cmd?
# 5  
Old 10-04-2012
I got it to work with mawk.

Code:
mawk "{x[$1] = x[$1] ? x[$1]""\" \"$2 : $2} END {for(i in x) print i, x[i]}" example_01.txt

I introduced \" \" before the first $2

Thanks again for the starter code.

Kenny.

---------- Post updated at 11:41 AM ---------- Previous update was at 11:39 AM ----------

I am in a Windows command window.
# 6  
Old 10-04-2012
Windows CMD does not parse shell syntax, it passes the commandline onto the program raw, quotes and all, for it to process as it sees fit. As such, programs can process quotes on the commandline in quite different ways from each other. It can be hard to guess how many backslashes you need, or whether backslashes even matter, and sometimes impossible to prevent strings from splitting when they shouldn't.

I'd reccomend busybox for windows if you want to casually write small UNIX scripts in windows. It amounts to a standalone Bourne shell with built-in awk, sed, cp, mv, and all sorts of other commands.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[solved] Awk/shell question to parse hour minute from text

Hi, I have a quick question on parsing the hour/minute and value from a text file and remove the seconds portion. For example in the below text file: 20:26:01 95.83 20:27:01 96.06 20:28:01 95.99 20:29:01 7.11 20:30:01 5.16 20:31:01 8.27 20:32:02 9.79 20:33:01 11.27 20:34:01 7.83... (2 Replies)
Discussion started by: satishrao
2 Replies

2. UNIX for Dummies Questions & Answers

[Solved] How to extract single and duplicate lines from file?

Hi, I need help! I have two files, one containing a list of codes and the other a list of codes and their meaning. I need to extract from file 2 all the codes from file 1 into a new file. These are my files: File1: Metbo Metbo Memar Mth Metbo File2: Metbo Methanoculleus... (3 Replies)
Discussion started by: Lokaps
3 Replies

3. Shell Programming and Scripting

How do i add horizontally??

1.04/1.05:ELA=20000,POLLK=35000,RH=5000,MH=7000,WH=4359 1.7:ELA=2000,POLLK=2000,RH=2000,MH=2000,WH=607 1.9:ELA=2000,POLLK=2000,RH=2000,MH=2000,WH=396 2.0:POLLK=6000,WENS=10000,ELA=7789 I have this above content where in i wan the total of the every row i.e. ... (9 Replies)
Discussion started by: nikhil jain
9 Replies

4. UNIX for Dummies Questions & Answers

[SOLVED] remove lines that have duplicate values in column two

Hi, I've got a file that I'd like to uniquely sort based on column 2 (values in column 2 begin with "comp"). I tried sort -t -nuk2,3 file.txtBut got: sort: multi-character tab `-nuk2,3' "man sort" did not help me out Any pointers? Input: Output: (5 Replies)
Discussion started by: pathunkathunk
5 Replies

5. Shell Programming and Scripting

[Solved] Find duplicate and add pattern in sed/awk

<Update> I have the solution: sed 's/\{3\}/&;&;---;4/' The thread can be marked as solved! </Update> Hi There, I'm working on a script processing some data from a website into cvs format. There is only one final problem left I can't find a solution. I've processed my file... (0 Replies)
Discussion started by: lolworlds
0 Replies

6. Shell Programming and Scripting

[Solved] I need to parse an username in parameters

Hi! Thank you ina advance for your Help. Sorry my english, Im from Argentina! Great Place!!! So, here si my problem I have a variable, lets say PARAMETERS that could be set like this: PARAMETROS="-Usistemas string1 -Astring2 -G -Y -Kcoco -Y string4 -Z" or PARAMETROS="string1... (1 Reply)
Discussion started by: Hercraft
1 Replies

7. Programming

[Solved] duplicate records

Hi I have a table which has 2 columns - id and amount. If there duplicate rows , as in id and amount are same , then i have to update the table in such away that only one row should contain amount and all rows should become zero for that id. eg id amount 1 100 1 100 2 200 1... (5 Replies)
Discussion started by: megha2525
5 Replies

8. Emergency UNIX and Linux Support

[Solved] AWK to parse adjacent matching lines

Hi, I have an input file like F : 0.1 : 0.002 P : 0.3 : 0.004 P : 0.5 : 0.008 P : 0.1 : 0.005 L : 0.05 : 0.02 P: 0.1 : 0.006 P : 0.01 : 0.08 F : 0.02 : 0.08 Expected output: (2 Replies)
Discussion started by: vasanth.vadalur
2 Replies

9. Shell Programming and Scripting

merge two files horizontally

Hi all, I have two files -- one with 1024 lines and another with 2048 lines. I want to merge them to create another file in such way that it takes 2 lines from file with 1024 lines and 4 from 2048 lines. I wrote a sample script and curious to see if there is a better way; i am sure there is,... (4 Replies)
Discussion started by: jakSun8
4 Replies

10. UNIX for Advanced & Expert Users

Merging Two File Horizontally

I am trying to merge two large file horizontally using paste command. Every thing is working fine except for time. Its taking lot of time. Is there any effiecient way of doing the same thing or is there anyway by which I can improve its perfomance programatically? Thanks, Yeheya (1 Reply)
Discussion started by: yeheyaansari
1 Replies
Login or Register to Ask a Question