Seperated by columns, merge in a file, sort them on common column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Seperated by columns, merge in a file, sort them on common column
# 1  
Old 08-01-2014
Seperated by columns, merge in a file, sort them on common column

Hi All,

I have 4 files in below format. I took them as an example.

File 1: Cut from position 1-4 then 6-7 then 8-14 then rest left and make them as columns in one new file. Inserting character H to the initial of all line like HCTOT.
Code:
 
CTOT 456787897 Low fever
CTOR 556712345 High fever

File 2: Cut from position 1-4 then 6-7 then 8-14 then rest left and make them as columns in one new file. Inserting D as initial.
Code:
CTWE 456711111 Some Risk
CTWR 551124567 High Risk

File 3: Cut from position 1-4 then 6-7 then 8-14 then rest left and make them as columns in one new file. Insert L as initial.
Code:
CTRE 459754125 Doctor Required
CTEE 559754214 Home Sickness

File 4: Cut from position 1-4 then 6-7 then 8-14 then rest left and make them as columns in one new file.Insert M as initial.
Code:
CRRE 458757845 Hospital Required
CREE 558757812 Hospital Not required

Column 2 become common column in all 4 files.

Its easy to cut columns usind cut -c command and insertion using sed command but not get the desired output while merge them as columns. I used paste command for that but while pasting I see one blank column in between two columns like CTOT<space><space>45.

I would see my output as:
Code:
HCTOT 45 6787897 Low fever
DCTWE 45 6711111 Some Risk
LCTRE 45 9754125 Doctor Required
MCRRE 45 8757845 Hospital Required
HCTOR 55 6712345 High fever
DCTWR 55 1124567 High Risk
LCTEE 55 9754214 Home Sickness
MCREE 55 8757812 Hospital Not required

I used sorting after that and combined all my logic into a shell script but I didn't get the desired output. Please help me out.
Thank you.

Last edited by Mannu2525; 08-01-2014 at 03:03 PM.. Reason: formatting
# 2  
Old 08-01-2014
Try
Code:
awk 'BEGIN {split("HDLM",INIT,"")} FNR==1 {++CNT} {$1=INIT[CNT]$1; $2=substr($2,1,2)" "substr($2,3)}1 ' file[1-4] | sort -k2,2
DCTWE 45 6711111 Some Risk
HCTOT 45 6787897 Low fever
LCTRE 45 9754125 Doctor Required
MCRRE 45 8757845 Hospital Required
DCTWR 55 1124567 High Risk
HCTOR 55 6712345 High fever
LCTEE 55 9754214 Home Sickness
MCREE 55 8757812 Hospital Not required

# 3  
Old 08-02-2014
Thats great...its working fine for me after some modification according to my files...thanks buddy..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Seperated a Column from 'ESC' Character seperated file

Hi Experts I have an escape seperated fields in the unix file. And in the below format file I need to extract the first column. Please help its urgent. cat -v op.dat | head 24397028^ I want to extract the file in below format ( with only first column ) 24397028 2439707 thanks. ... (6 Replies)
Discussion started by: neha_suri06
6 Replies

2. Shell Programming and Scripting

Paste columns based on common column: multiple files

Hi all, I've multiple files. In this case 5. Space separated columns. Each file has 12 columns. Each file has 300-400K lines. I want to get the output such that if a value in column 2 is present in all the files then get all the columns of that value and print it side by side. Desired output... (15 Replies)
Discussion started by: genome
15 Replies

3. UNIX for Dummies Questions & Answers

Merge selective columns from files based on common key

Hi, I am trying to selectively merge two files based on keys reported in the 1st column. File1: #file1-header1 file1-header2 111 qwe rtz uio 198 asd fgh jkl 165 yxc 789 poi uzt rew 89 lkj File2: #file2-header2 file2-header2 165 ghz nko2 ... (2 Replies)
Discussion started by: dovah
2 Replies

4. Shell Programming and Scripting

Merge with common column

hi i have two files and i wanted to join them using common column. try to do this using "join" command but that did not help. File 1: 123 9a.vcf hy92.vcf hy90.vcf Index Ref Alt Ref Alt Ref Alt 315 14 0 7 4 ... (6 Replies)
Discussion started by: empyrean
6 Replies

5. Shell Programming and Scripting

Count and merge using common column

I have the following records from multiple files. 415 A G 415 A G 415 A T 415 A . 415 A . 421 G A 421 G A,C 421 G A 421 G A 421 G A,C 421 G . 427 A C 427 A ... (3 Replies)
Discussion started by: empyrean
3 Replies

6. Shell Programming and Scripting

file merge based on common columns

I have two files 1.txt 34, ABC, 7, 8, 0.9 35, CDE, 6.5, -2, 0.01 2.txt 34, ABC, 9, 6, -1.9 35, CDE, 8.5, -2.3, 5.01 So in both files common columns are 1 and 2 so final o/p should look like 34, ABC, 7, 8, 0.9, 9, 6, -1.9 35, CDE, 6.5, -2, 0.01, 8.5, -2.3, 5.01 I tried using... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

7. UNIX for Dummies Questions & Answers

Merge rows with common column

Dear all I have big file with two columns A_AA960715 GO:0006952 A_AA960715 GO:0008152 A_AA960715 GO:0016491 A_AA960715 GO:0007165 A_AA960715 GO:0005618 A_AA960716 GO:0006952 A_AA960716 GO:0005618 A_AA960716... (15 Replies)
Discussion started by: AAWT
15 Replies

8. UNIX for Dummies Questions & Answers

Writing a loop to merge multiple files by common column

I have 100 data files labelled 250.1.txt through 250.100.txt. The second column of the data files partially match (there is about %90 overlap). Each data file has 4 columns. I want the merge all these text files by the matching values in the second column. In the output, the first column should... (1 Reply)
Discussion started by: evelibertine
1 Replies

9. UNIX for Dummies Questions & Answers

find common lines using just one column to compare and result with all columns

Hi. If we have this file A B C 7 8 9 1 2 10 and this other file A C D F 7 9 2 3 9 2 3 4 The result i´m looking for is intersection with A B C D F so the answer here will be (10 Replies)
Discussion started by: alcalina
10 Replies

10. Shell Programming and Scripting

merge rows based on a common column

Hi guys, Please guide me if you have a solution to this problem. I have tried paste -s but it's not giving the desired output. I have a file with the following content- A123 box1 B345 bat2 C431 my_id A123 service C431 box1 A123 my_id I need two different outputs- OUTPUT1 A123... (6 Replies)
Discussion started by: smriti_shridhar
6 Replies
Login or Register to Ask a Question