1

I have the below input

L12194C;               
           ;1;8;12     
           ;2;30;46    
           ;3;49;71    
           ;4;0;0      
           ;5;0;0      
           ;6;1;3      
L15698A2;              
           ;4;2;4      
           ;5;0;0      
           ;6;0;0      
L2281A2;               
           ;4;1;2      
           ;5;0;0      
12302C;                
           ;1;8;11     
           ;2;1;1      
           ;3;1;1      
           ;4;1;2      
           ;5;2;4      
           ;6;0;0      

I want the output to be

L12194C;1;8 ;12        
L12194C;2;30;46        
L12194C;3;49;71        
L12194C;4;0;0          
L12194C;5;0;0          
L12194C;6;1;3          

L15698A2;4;2;4         
L15698A2;5;0;0         
L15698A2;6;0;0         

L2281A2;4;1;2          
L2281A2;5;0;0          

12302C;1;8;11          
12302C;2;1;1           
12302C;3;1;1           
12302C;4;1;2           
12302C;5;2;4           
12302C;6;0;0           
0

4 Answers 4

1

use AWk :

awk '!/^[[:blank:]]/{j=$1;print "";next}{print j$1}' FILENAME

output:

L12194C;;1;8;12
L12194C;;2;30;46
L12194C;;3;49;71
L12194C;;4;0;0
L12194C;;5;0;0
L12194C;;6;1;3

L15698A2;;4;2;4
L15698A2;;5;0;0
L15698A2;;6;0;0

L2281A2;;4;1;2
L2281A2;;5;0;0

12302C;;1;8;11
12302C;;2;1;1
12302C;;3;1;1
12302C;;4;1;2
12302C;;5;2;4
12302C;;6;0;0
1

Maybe like this:

sed -E '
    1{ h; d; }
    /^[^ \t]/{ h; s/.*//; p; d; }
    s/^[ \t]+;//; G; s/^(.*)\n(.*)$/\2\1/'

Output:

L12194C;1;8;12
L12194C;2;30;46
L12194C;3;49;71
L12194C;4;0;0
L12194C;5;0;0
L12194C;6;1;3

L15698A2;4;2;4
L15698A2;5;0;0
L15698A2;6;0;0

L2281A2;4;1;2
L2281A2;5;0;0

12302C;1;8;11
12302C;2;1;1
12302C;3;1;1
12302C;4;1;2
12302C;5;2;4
12302C;6;0;0
0
0

Are the fields constant? Meaning, every field between the ;'s are related data... If the fields are constant, personally, I'd use a database such as sqlite and insert the data into the db and then use queries to output the data the way you need it. You can also use an array and then arrange the fields in the output that way too.

2
  • yes they are. I only need unix commands to duplicate the field in the first column Commented Jun 26, 2015 at 17:36
  • The data format above is not the data format I responded to (must have been edited after I responded)... my solution will not work for your data format. Please disregard. Commented Jun 26, 2015 at 18:39
0

Another sed:

sed -ne'h;s/.*//p;x;:n' -e'N;s/;.* //p;tn' -eD <in >out

sed saves a copy of pattern space to hold space, s///ubstitutes away all of pattern space and prints the results, then exchanges hold and pattern spaces. This happens on each non-blank 1st field line, and gets the dividing blanks in output where appropriate.

For every other line sed also appends the Next following an inserted \newline delimiter, then attempts to s///ubstitute away all of pattern space between the first semicolon and last space. If both of those chars occur in pattern space, the substitution tests successful, and sed branches back to the :next label to try again.

Else sed Deletes up to and including the first occurring \newline in pattern space before starting over from the top w/ whatever remains.

My results:

L12194C;1;8;12
L12194C;2;30;46
L12194C;3;49;71
L12194C;4;0;0
L12194C;5;0;0
L12194C;6;1;3

L15698A2;4;2;4
L15698A2;5;0;0
L15698A2;6;0;0

L2281A2;4;1;2
L2281A2;5;0;0

12302C;1;8;11
12302C;2;1;1
12302C;3;1;1
12302C;4;1;2
12302C;5;2;4
4
  • sed -e:n -e'/./!n;$!N;/;.*\n *;/!D;s//;/p;tn' <in >out this command work perfect. thank you so much Commented Jun 26, 2015 at 22:48
  • @Toufik - i guess i copy/pasted it wrong - i had blank line positions transposed. It should work now. Commented Jun 26, 2015 at 23:23
  • well it worked even in the previous command Commented Jun 27, 2015 at 0:01
  • @Toufik - ok! It's easier that way, i know. I rolled it back, Commented Jun 27, 2015 at 0:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.