Merge files with file names added | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Merge files with file names added

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-04-2013
luoruicd luoruicd is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 7 February 2013, 7:45 PM EST
Posts: 10
Thanks: 10
Thanked 0 Times in 0 Posts
Merge files with file names added

I want to merge several files with identical format:
file 1:

Code:
rs3094315       0.0006105222804 0.9528743638
rs3131972       -0.05461465109  0.3139864854
rs3115860       -0.06041530955  0.3195499498

file 2:

Code:
rs2073813       -0.06039552152  0.2956527097
rs11564776      -0.1864266568   0.1976612108
rs12562034      0.00225669783   0.8555456823
rs12124819      -0.05026905981  0.2383563616
...

For each file, they have a unique name as XX1.phe.ps. XX1.phe.ps I want to merge all the files AND, add part of the file name "XXX" to the first column.
So the expected output is:

Code:
XX1 rs3094315       0.0006105222804 0.9528743638
XX1 rs3131972       -0.05461465109  0.3139864854
XX1 rs3115860       -0.06041530955  0.3195499498
XX2 rs2073813       -0.06039552152  0.2956527097
XX2 rs11564776      -0.1864266568   0.1976612108
XX2 rs12562034      0.00225669783   0.8555456823
XX2 rs12124819      -0.05026905981  0.2383563616

How can I do this?

Moderator's Comments:
Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 01-04-2013 at 03:49 PM..
Sponsored Links
    #2  
Old Unix and Linux 01-04-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 April 2015, 5:28 PM EDT
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 Posts

Code:
printf "%s\n" *.phe.ps | awk -F"." '{ file=$0; pre=sprintf("%s",$1); while(( getline line < file ) > 0 ) { print pre,line; } }'

The Following User Says Thank You to Yoda For This Useful Post:
luoruicd (01-07-2013)
Sponsored Links
    #3  
Old Unix and Linux 01-04-2013
binlib binlib is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 15 March 2013, 10:40 AM EDT
Location: New Jersey
Posts: 380
Thanks: 7
Thanked 90 Times in 75 Posts

Code:
grep . *.phe.ps |sed 's/\.phe\.ps:/ /'

The Following User Says Thank You to binlib For This Useful Post:
luoruicd (01-07-2013)
    #4  
Old Unix and Linux 01-05-2013
mukulverma2408 mukulverma2408 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 4 September 2014, 7:17 AM EDT
Posts: 61
Thanks: 12
Thanked 2 Times in 2 Posts
You can try :

Code:
awk '{ print FILENAME,$0 ;}' file1 file2 > tmp.tmp

The Following User Says Thank You to mukulverma2408 For This Useful Post:
luoruicd (01-07-2013)
Sponsored Links
    #5  
Old Unix and Linux 01-05-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 April 2015, 5:28 PM EDT
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 Posts
mukulverma2408, your code is good for requester with some minor changes:

Code:
awk '{ split(FILENAME,A,"."); print A[1],$0; } ' *.phe.ps

The Following User Says Thank You to Yoda For This Useful Post:
luoruicd (01-07-2013)
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Merge two files into one file chiru_h Shell Programming and Scripting 1 01-19-2012 12:28 AM
merge files along with file names (awk)? wei.deng UNIX for Dummies Questions & Answers 5 11-04-2011 04:49 PM
Merge files from /etc to one file using C sussil Programming 2 06-26-2010 10:45 AM
Merge two files whose names are given in other file harshada Shell Programming and Scripting 1 10-01-2008 04:17 AM
merge two files in one file nvkuriseti Shell Programming and Scripting 3 08-27-2007 06:06 AM



All times are GMT -4. The time now is 12:26 AM.