How to generate one long column by merging two separate two columns in a single file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to generate one long column by merging two separate two columns in a single file?
# 1  
Old 01-10-2014
How to generate one long column by merging two separate two columns in a single file?

Dear all,
I have a simple question. I have a file like below (separated by tab):
Code:
col1   col2      col3        col4   col5        col6      col7
21     66745     rs1234      21     rs5678      23334     0.89
21     66745     rs2334      21     rs9978      23334     0.89
21     66745     rs3334      21     rs7578      23334     0.89
21     66745     rs6664      21     rs9998      23334     0.89
21     66745     rs8884      21     rs5588      23334     0.89

I want to generate one file contains only one column by merging col3 and col5, like this:
Code:
rs1234
rs2334
rs3334
rs6664
rs8884
rs5678
rs9978
rs7578
rs9998      
rs5588

Is there a simple way to do it? My unix knowledge is limited. I know I can use cut to get two separate file containing col3 and col5 separately and then cat them. I am wondering if there is any better way to do it. Thank you very much for your time!

Last edited by zaxxon; 01-10-2014 at 01:39 PM..
# 2  
Old 01-10-2014
If order of values don't matter, then something like
Code:
awk 'BEGIN{getline}{printf("%s\n%s\n",$3,$5)} file.txt

or
Code:
awk 'BEGIN{getline}{printf("%s\n%s\n",$3,$5)} file.txt | sort

the BEGIN{getline} is to throw the header line away, I didn't test this
This User Gave Thanks to blackrageous For This Post:
# 3  
Old 01-10-2014
Another approach:
Code:
awk 'NR>1{print $3 RS $5}' file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-10-2014
Hello,

Following may also help.



Code:
awk 'NR==1 {next}; $3 {f=$3"\n"f} $5 {k=$5"\n"k} END{print f"\n"k}' file_name | grep -v '^$'

Output wil be as follows.


Code:
rs8884
rs6664
rs3334
rs2334
rs1234
rs5588
rs9998
rs7578
rs9978
rs5678

Thanks,
R. Singh
Moderator's Comments:
Mod Comment Don't use font tags inside code tags..
# 5  
Old 01-11-2014
Perhaps, this could be one way to do so

Code:
$ awk 'function out(v){if(FNR>1)print $v}FNR==NR{out(3);next}{out(5)}' file file
rs1234
rs2334
rs3334
rs6664
rs8884
rs5678
rs9978
rs7578
rs9998
rs5588

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merging multiple lines into single line based on one column

I Want to merge multiple lines based on the 1st field and keep into single record. SRC File: AAA_POC_DB.TAB1 AAA_POC_DB.TAB2 AAA_POC_DB.TAB3 AAA_POC_DB.TAB4 BBB_POC_DB.TAB1 BBB_POC_DB.TAB2 CCC_POC_DB.TAB6 OUTPUT ----------------- 'AAA_POC_DB','TAB1','TAB2','TAB3','TAB4'... (10 Replies)
Discussion started by: raju2016
10 Replies

2. Shell Programming and Scripting

How to parse parts of 1 column into two separate columns?

I have a shell script that is currently transferring a csv file from a server into a Teradata database table. One of the 30 or so columns is called "destination_url". In that URL there are parameters, and it is possible for those parameters to be repeated because of referring companies copying... (3 Replies)
Discussion started by: craigwg
3 Replies

3. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

4. Shell Programming and Scripting

Merging columns based on one or more column in two files

I have two files. FileA.txt 30910 rs7468327 36587 rs10814410 91857 rs9408752 105797 rs1133715 146659 rs2262038 152695 rs2810979 181843 rs3008128 182129 rs3008131 192118 rs3008170 FileB.txt 30910 1.9415219673 0 36431 1.3351312477 0.0107191428 36587 1.3169171182... (2 Replies)
Discussion started by: genehunter
2 Replies

5. Shell Programming and Scripting

Multiple columns to a single column

I have this input: 10 22 1 100 11 22 10 1 50 14 3 1 100 23 3 1 100 24 15 1 100 10 22 5 3 1 33.333 11 22 1 100 It has an inconsistent number of fields but the last field is determined by 100/(NF-2) using awk. I want to take this multiple columned input file and transform so that... (2 Replies)
Discussion started by: mdlloyd7
2 Replies

6. UNIX for Dummies Questions & Answers

How to separate a single column file into files of the same size (i.e. number of rows)?

I have a text file with 1,000,000 rows (It is a single column text file of numbers). I would like to separate the text file into 100 files of equal size (i.e. number of rows). The first file will contain the first 10,000 rows, the second row will contain the second 10,000 rows (rows 10,001-20,000)... (2 Replies)
Discussion started by: evelibertine
2 Replies

7. Shell Programming and Scripting

Read duplicate column, then generate a single line

Dear experts, How to generate the result listed below ? Input file: col1col2col3Aname1size1Aname2size2Aname3size1Bname4size3Bname5size5Cname6size8Cname7size6Cname8size9Cname9size11Cname10size16 What I want is: Aname1, size1name2, size2name3, size1Bname4,size3name5, size5Cname6,... (1 Reply)
Discussion started by: tojzz
1 Replies

8. Shell Programming and Scripting

Extracting columns from a matrix and storing each column in a separate file

Hi All, I have a huge matrix file consisting some some millions rows and 6000 columns. The contents are just floating point numbers in the matrix. I want to extract each column (i.e. 6000 of them) and store each column in a separate file. For example, 1.dat will consist of elements from column... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

9. Shell Programming and Scripting

merging similar columns in a single line file

Hi Guys. I have tried the commands sort and join. But I couldn't able to to find the command for joining in a single line based on keys.My example inputs and outputs are like the following. Help would be appreciated.:D Input file a1tabXXXXXXX a2tabXXXXXXX a6tabYYYYYYYYY a71tabXXXXXXX... (7 Replies)
Discussion started by: repinementer
7 Replies

10. UNIX for Dummies Questions & Answers

two files.say a and b.both have long columns.i wanna match the column fron 1st file w

ex: a file has : 122323 123456456 125656879 678989965t635 234323432 b has : this is finance no. this is phone no this is extn ajkdgag idjsidj i want the o/p as: 122323 his is finance no. 123456456 this is phone no 123456456 ... (4 Replies)
Discussion started by: TRUPTI
4 Replies
Login or Register to Ask a Question