LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-19-2013, 03:57 PM   #1
SAAAD
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Rep: Reputation: Disabled
Question copy and past a column between files


Hi there,

I would like to know the Linux syntax needed to copy a column from file.A and past it to file.B.
Where file.B contains 6 clumns already and I want the new copied column to be the 7th.

waiting for your usual cooperation..
 
Old 04-19-2013, 04:30 PM   #2
cortman
Member
 
Registered: Jan 2012
Location: ZZ9 Plural Z Alpha
Distribution: Crunchbang 11, LFS 7.3, DSL 4.1.10, Lubuntu 12.10, Debian 7
Posts: 219

Rep: Reputation: 43
Do some research on awk- it should be quite simple.

Code:
awk '{print $1}'
and see here for info on adding it to the second file.
 
Old 04-19-2013, 04:51 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by SAAAD View Post
I would like to know the Linux syntax needed to copy a column from file.A and past it to file.B.
Where file.B contains 6 clumns already and I want the new copied column to be the 7th.
If there is only one column present in file.A:
Code:
paste -d" " file.B file.A
If file.A has more columns:
Code:
awk '{ print $X }' file.A | paste -d" " file.B -
Replace X with the column number.
 
2 members found this post helpful.
Old 04-19-2013, 08:39 PM   #4
SAAAD
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
If there is only one column present in file.A:
Code:
paste -d" " file.B file.A
If file.A has more columns:
Code:
awk '{ print $X }' file.A | paste -d" " file.B -
Replace X with the column number.
thank you..
 
Old 04-19-2013, 11:52 PM   #5
SAAAD
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by SAAAD View Post
thank you..
I had just tried it, but its not working!
No data copied!
 
Old 04-20-2013, 12:00 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
@SAAD:

Perhapds you made some mistake while choosing column number (i.e. $X) in awk cmd, else what druuna has mentioned is working perfectly.

Anyway, if it's still not working then can you show us some example of fileA and fileB?
 
Old 04-20-2013, 12:13 AM   #7
SAAAD
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by SAAAD View Post
I had just tried it, but its not working!
No data copied!
file.A
178
255
23

file.B
jhon 17 male
tom 15 male
marry 13 female

I need to copy the lonely column in file.A to file.B by creating a new 4th column..
Hope this clear.

Thank you
 
Old 04-20-2013, 12:50 AM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
In that case, no need to use awk, but simply paste cmd will do it:
Code:
~$ paste -d" " fileB fileA

john 17 male 178
tom 15 male 255
marry 13 female 23

OR
~$ paste -d"\t" fileB fileA
Just give it a try.

Last edited by shivaa; 04-20-2013 at 12:52 AM.
 
Old 04-20-2013, 01:35 AM   #9
SAAAD
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
Cool

Quote:
Originally Posted by shivaa View Post
In that case, no need to use awk, but simply paste cmd will do it:
Code:
~$ paste -d" " fileB fileA

john 17 male 178
tom 15 male 255
marry 13 female 23

OR
~$ paste -d"\t" fileB fileA
Just give it a try.
I tried it.. and its working.
Thanks a lot!
 
Old 04-20-2013, 02:09 AM   #10
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Good :-). BTW, you can Mark the thread as solved (option is under Thread Tools on top menu), if you think it has so.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot copy and past to a ext4 Disk kujirasan Fedora 4 02-03-2011 08:49 PM
How to copy a column satimis Linux - General 27 11-25-2010 03:05 PM
how to copy-past on command line? jameshao Linux - Newbie 5 02-01-2009 04:55 PM
How to Copy and Past ? I cannot do anything redlynch Linux - Newbie 8 07-25-2008 07:23 AM
Qcad copy and past? kalleanka Linux - Software 2 10-07-2006 08:43 AM


All times are GMT -5. The time now is 06:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration