Help with merge data at same column but different row inquiry


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with merge data at same column but different row inquiry
# 1  
Old 01-04-2016
Help with merge data at same column but different row inquiry

Hi,

Anyone did experience to merge data at same column but different row previously by using awk, sed, perl, etc?

Input File:
Code:
SSO12256
SSO0001
thiD-1
rbsK-1
SSO0006
SSO0007
SSO0008
SSO0009
SSO0010
SSO0011

Desired Output File:
Code:
SSO12256#SSO0001
SSO0001#thiD-1
thiD-1#rbsK-1
rbsK-1#SSO0006
SSO0006#SSO0007
SSO0007#SSO0008
SSO0008#SSO0009
SSO0009#SSO0010
SSO0010#SSO0011

I wanna merge same column but different row data and separate it by "#" symbol.
The last row data can be just ignore it. As it can't "merge" with any data next row to it.
eg. my Input File raw data 10 record "individual" data, while my Desired Output File got 9 set of "merge" data.

Many thanks for any advice.
Thanks a lot and again.
# 2  
Old 01-04-2016
Hi,
with (gnu) sed:
Code:
$ sed -n '1x;2,${H;x;s/\n/#/p;}' file.txt
SSO12256#SSO0001
SSO0001#thiD-1
thiD-1#rbsK-1
rbsK-1#SSO0006
SSO0006#SSO0007
SSO0007#SSO0008
SSO0008#SSO0009
SSO0009#SSO0010
SSO0010#SSO0011

With awk:
Code:
$ awk '{B=A;A=$0};NR > 1 && $0=B"#"A' file.txt
SSO12256#SSO0001
SSO0001#thiD-1
thiD-1#rbsK-1
rbsK-1#SSO0006
SSO0006#SSO0007
SSO0007#SSO0008
SSO0008#SSO0009
SSO0009#SSO0010
SSO0010#SSO0011

Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 01-04-2016
Hello perl_beginner,

Following may help you in same. Let's say we have following Input_file.
Code:
cat Input_file
SSO12256
SSO0001
thiD-1
rbsK-1
SSO0006
SSO0007
SSO0008
SSO0009
SSO0010
SSO0011
SSO0013

Following is the code then:
Code:
awk '{A[++i]=$0;B[++j]=$0} END{for(q=1;q<i-1;){print A[q] "#" B[++q] ORS B[q] "#" A[++q]};if(i%2==0){print A[q] "#" B[++q]}}' Input_file

Output will be as follows.
Code:
SSO12256#SSO0001
SSO0001#thiD-1
thiD-1#rbsK-1
rbsK-1#SSO0006
SSO0006#SSO0007
SSO0007#SSO0008
SSO0008#SSO0009
SSO0009#SSO0010
SSO0010#SSO0011
SSO0011#SSO0013

EDIT: Adding one more solution for same.
Code:
awk '{A[NR]=$0;n=NR} END{for(i=1;i<n;i++){print A[i] "#" A[++i];--i}}'  Input_file

Output will be as follows.
Code:
SSO12256#SSO0001
SSO0001#thiD-1
thiD-1#rbsK-1
rbsK-1#SSO0006
SSO0006#SSO0007
SSO0007#SSO0008
SSO0008#SSO0009
SSO0009#SSO0010
SSO0010#SSO0011
SSO0011#SSO0013

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-04-2016 at 09:15 AM.. Reason: Adding one more solution for same now too.
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 01-04-2016
A Perl version:

Code:
$
$ cat f7
SSO12256
SSO0001
thiD-1
rbsK-1
SSO0006
SSO0007
SSO0008
SSO0009
SSO0010
SSO0011
$
$ perl -lne 'print "$x#$_" if $.>1; $x=$_' f7
SSO12256#SSO0001
SSO0001#thiD-1
thiD-1#rbsK-1
rbsK-1#SSO0006
SSO0006#SSO0007
SSO0007#SSO0008
SSO0008#SSO0009
SSO0009#SSO0010
SSO0010#SSO0011
$
$

This User Gave Thanks to durden_tyler For This Post:
# 5  
Old 01-04-2016
hi Disedorgue,
in code
Code:
awk '{B=A;A=$0};NR > 1 && $0=B"#"A ' file

I got the code how it is working but i have a doubt.In the highlighted part how pattern matching and action is being performed together?. && is between action and pattern matching.As we know that action is performed in braces , otherwise default action is print $0 if braces are not used.Kindly explain.

Last edited by looney; 01-04-2016 at 02:01 PM..
# 6  
Old 01-04-2016
Quote:
Originally Posted by looney
hi Disedorgue,
in code
Code:
awk '{B=A;A=$0};NR > 1 && $0=B"#"A ' file

I got the code how it is working but i have a doubt.In the highlighted part how pattern matching and action is being performed together?. && is between action and pattern matching.As we know that action is performed in braces , otherwise default action is print $0 if braces are not used.Kindly explain.
Hi looney,

All the highlighted part is pattern, so it will be the equivalent of:
Code:
NR > 1 && $0=B"#"A {print $0}

If every term of the pattern evaluates to true print the whole record. The interesting bit is that if NR == 1 (first line) it will be false and the other expression is not evaluated, therefore $0 never gets modified as B"#"A, nor it gets printed.

Last edited by Aia; 01-05-2016 at 05:35 PM..
These 3 Users Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with merge row if share same column info

Input file: 32568 SSO7483 32568 SSO7486 117231 SSO1293 117231 SSO1772 178081 SSO3076 178081 SSO3077 222417 porA-2 222417 porB-2 263778 SSO1245 263778 SSO0509 . . Desired output: 32568 SSO7483,SSO7486 117231 ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

2. Shell Programming and Scripting

Column to Row Data.

HI Guys, I have below Input :- X L1 5 Y L1 10 Z L1 15 X L2 20 Y L2 12 Z L2 15 X L3 100 Y L3 Z L3 300 Output:- ID L1 L2 L3 X 5 10 15 Y 20 12 15 Z 100 Null 300 (11 Replies)
Discussion started by: pareshkp
11 Replies

3. Shell Programming and Scripting

Convert Data from Column to Row

Hi FileA.txt E_TIM 16, ETE 15, EOND 26, EEC 81, E_1 un, E_2 un, E_3 un, E_4 284, E_TIM 17, ETE 15, EOND 29, EEC 82, E_1 un, E_2 un, E_3 un, E_4 249, (6 Replies)
Discussion started by: asavaliya
6 Replies

4. UNIX for Advanced & Expert Users

Convert column data to row data using shell script

Hi, I want to convert a 3-column data to 3-row data using shell script. Any suggestion in this regard is highly appreciated. Thanks. (4 Replies)
Discussion started by: sktkpl
4 Replies

5. Shell Programming and Scripting

How to merge multiple rows into single row if first column matches ?

Hi, Can anyone suggest quick way to get desired output? Sample input file content: A 12 9 A -0.3 2.3 B 1.0 -4 C 34 1000 C -111 900 C 99 0.09 Output required: A 12 9 -0.3 2.3 B 1.0 -4 C 34 1000 -111 900 99 0.09 Thanks (3 Replies)
Discussion started by: cbm_000
3 Replies

6. Shell Programming and Scripting

Sort data from column to row

Hi, I need somebody's help with sorting data with awk. I've got a file: 10 aaa 4584 12 bbb 6138 20 ccc 4417 21 ddd 7796 10 eee 7484 12 fff ... (5 Replies)
Discussion started by: killerbee
5 Replies

7. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

8. Shell Programming and Scripting

row to column and position data in to fixed column width

Dear friends, Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number. Program COUNT=1 for MYDIR in `ls /` do VOBS=${MYDIR} echo "${COUNT}. ${MYDIR}" ... (4 Replies)
Discussion started by: baluchen
4 Replies

9. Shell Programming and Scripting

Convert row data to column data

Hi Guys, I have a file as follows: a 1 b 786 c 90709 d 99 a 9875 b 989 c 887 d 111 I want: a 1 9875 b 786 989 (3 Replies)
Discussion started by: npatwardhan
3 Replies

10. Shell Programming and Scripting

Move data from Column to Row

Hi all, I have a file with several columns of data, eg: A B C D 1 2 5 1 2 2 2 2 8 4 4 4 4 2 3 4 10 9 4 4 9 7 1 2 I need to get the values from, say, column B and place them into a string separated by a semicolon, eg: 2;2;4;2;9;7 Does... (4 Replies)
Discussion started by: danhodges99
4 Replies
Login or Register to Ask a Question