Another tricky sed or awk question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Another tricky sed or awk question
# 1  
Old 06-14-2010
Another tricky sed or awk question

This post is in reference to https://www.unix.com/shell-programmin...#post302428154

I am trying to go the opposite direction now:

I have the following file:
Code:
a,b,C,f,g
a,b,D,f,g
a,b,E,f,g
h,i,J,k,l
m,n,O,t,u
m,n,P,t,u
m,n,Q,t,u
m,n,R,t,u
m,n,S,t,u
v,w,X,z,a
v,w,Y,z,a

I want to use sed or awk (perl also welcome), to "condense" these lines into this format:
Code:
a,b,C-D-E,f,g
h,i,J,k,l
m,n,O-P-Q-R-S,t,u
v,w,X-Y,z,a

Who is up for it? You would again save me tons of time!

Thanks a lot in advance!

Last edited by vgersh99; 06-14-2010 at 12:28 PM.. Reason: code tags, please!
# 2  
Old 06-14-2010
Code:
nawk '
   BEGIN {
     FS=OFS=","
     SEP="-"
     stub="<stub>"
   }
   {
     idx=$1 OFS $2 OFS stub OFS $4 OFS $5
     a[idx]=(idx in a)?a[idx] SEP $3:$3
   }
   END {
     for(i in a) {
       sub(stub, a[i], i)
       print i
     }
   }' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 06-14-2010
perfect, thanks!
# 4  
Old 06-15-2010
Code:
$ perl -F, -ane '{
                  if ( @A[0] eq "" ) { push @A, @F ; }
                  elsif ( $A[0] ne "" && $A[0] eq $F[0] ) { $A[2]="$A[2]-$F[2]"; }
                  elsif ( $A[0] ne "" && $A[0] ne $F[0] ) { print "$A[0],$A[1],$A[2],$A[3],$A[4]" ; @A=(); push @A, @F; }
                }
                 END
                {
                  print "$A[0],$A[1],$A[2],$A[3],$A[4]"; 
                }' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tricky sed required

Hi All I need to put some sed together for a task and its a bit advanced for me, so I thought I'd ask if anyone here could help. I have a csv file with content like this - "","abcde","","" "'","abcde","","" "","","","1234" "'e'","","","" I need to remove any single quotes that fall... (17 Replies)
Discussion started by: steadyonabix
17 Replies

2. Shell Programming and Scripting

Tricky BASH quoting question

I have some data files that I can identify by a certain pattern in the names using find. Every one of those data files has an XML file associated with it (can be multiple data files per XML file). The XML file is always up one directory from the data file(s) in a folder calledRun##### -... (12 Replies)
Discussion started by: Michael Stora
12 Replies

3. UNIX for Dummies Questions & Answers

Tricky GREP question..

I have some large login files that I need to extract (user)@(server) from. Where it gets tricky is that there is usually more than one '@' sign on each line(although it does have a leading space if it's not part of the (user)@(server) string), I need only the (user)@(server) section, I need only... (6 Replies)
Discussion started by: Mordaris
6 Replies

4. Shell Programming and Scripting

sed tricky problem

Hi, I have a file which contains two strings: AAAAA and BBBBB I have two variables in my script: DATE="03/21/2010" aDate="20100321" I need to replace string AAAAA with variable $DATE and BBBBB with $aDate. Here is what I do sed "s/AAAAA/$DATE/" $BASIC_TMPLT | sed "s/BBBBB/$aDate/" >... (4 Replies)
Discussion started by: axed
4 Replies

5. Shell Programming and Scripting

Tricky sed or awk question

Hello everyone, unfortunately I am no unix nor scripting guru, which is why I am asking for help here. I am trying to reformat a .csv file using sed or awk which has the following format: a,b,C-D-E,f,g h,i,J,k,l m,n,O-P-Q-R-S,t,u v,w,X-Y,z,a It's basically a 5-field text file which has an... (7 Replies)
Discussion started by: awayand
7 Replies

6. Shell Programming and Scripting

sed or awk question

Hello expert, I have an output file with few thousand lines similar like below : &quot;Future Netmgmt&quot; &quot;10.99.16.0&quot; &quot;N&quot; &quot;10&quot; &quot;10.0.0.0&quot; &quot;Circuitless-IP&quot; &quot; &quot; &quot;255.255.254.0&quot; &quot;Future Netmgmt&quot; &quot;10.99.18.0&quot; &quot;N&quot; &quot;10&quot; &quot;10.0.0.0&quot; &quot;Circuitless-IP&quot; &quot; &quot; &quot;255.255.254.0&quot; &quot;WAAS loopbacks&quot; &quot;10.99.20.0&quot;... (6 Replies)
Discussion started by: dannytrinh
6 Replies

7. UNIX for Dummies Questions & Answers

Tricky Quotation Question

Hi, I am at a point in my script where I defined the number of the command line parameter I would like to set a variable equal to: parameter_number=14 I would then like to set a variable equal to the correct parameter: variable=$parameter_number The issue here is that {} is required... (2 Replies)
Discussion started by: msb65
2 Replies

8. Shell Programming and Scripting

Tricky script question

Hi, I'm in the midst of writing a UNIX script that sftp's files to an external host and am stuck with a problem. The problem is that the files created on my server as a order number that correlates to a sequence of directories on the remote host which is where the file should be ftp'ed. ... (3 Replies)
Discussion started by: budrito
3 Replies

9. Shell Programming and Scripting

Tricky Sed

Hello. I am trying to convert occurrences of 'NULL' from a datafile. The 'NULL' occurences appears at this: |NULL| NULL|NULL| NULL|NULL| NULL|NULL| NULL| There should be 52 fields per line. I would like any occurrence of | NULL| or |NULL| to appear as '||' Currently I am using this sed... (2 Replies)
Discussion started by: bestbuyernc
2 Replies

10. Filesystems, Disks and Memory

Tricky File Permission Question

I'm trying to answer the following question about file permissions in Unix. Consider a file with the following permissions: rwx---r-- I am not the owner of this file, but I am a member of the group of this file. My question is: do I have read access to this file? I thought... (3 Replies)
Discussion started by: Hook
3 Replies
Login or Register to Ask a Question