LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-23-2010, 10:59 AM   #1
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,677

Rep: Reputation: 55
How to copy a column


Hi folks,

Ubuntu 10.10
Gnome desktop

On a text file, .txt, it is possible to high-light and copy a row on a table. But how to high-light and copy a column. TIA

B.R.
satimis
 
Old 11-23-2010, 11:22 AM   #2
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
You could use awk to accomplish this. Another way would be using the "cut" command.
 
Old 11-23-2010, 11:23 AM   #3
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Rep: Reputation: 43
you can use the "cut" command to do this.
 
Old 11-23-2010, 06:50 PM   #4
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,677

Original Poster
Rep: Reputation: 55
Hi fordeck and ncsuapex,


Thanks for your advice.

cut command:-

I think following article is relevant:-
How do I Select Columns From a File?
http://lowfatlinux.com/linux-columns-cut.html

But if the data file is as follow, column separated by space, what can I do?
Code:
  Year Yield Rainfall Temperature
1 1963	  60        8          56
2 1964    50       10          47
3 1965    70       11          53
4 1966    70       10          53
5 1967    80        9          56
6 1968    50        9          47
7 1969    60       12          44
8 1970    40       11          44

fordeck,

How to do it with awk? TIA


B.R.
satimis
 
Old 11-23-2010, 07:02 PM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,829
Blog Entries: 1

Rep: Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227
What exactly are you trying to do? Which ones?
 
Old 11-23-2010, 07:04 PM   #6
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
One way would be:

Code:
my_machine:~$ cat test.txt
  Year Yield Rainfall Temperature
1 1963	  60        8          56
2 1964    50       10          47
3 1965    70       11          53
4 1966    70       10          53
5 1967    80        9          56
6 1968    50        9          47
7 1969    60       12          44
8 1970    40       11          44

Code:
my_machine:~$ awk '{print $1}' test.txt
Year
1
2
3
4
5
6
7
8
Regards,

fordeck
 
Old 11-23-2010, 07:04 PM   #7
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,978
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by satimis View Post
Hi fordeck and ncsuapex,


Thanks for your advice.

cut command:-

I think following article is relevant:-
How do I Select Columns From a File?
http://lowfatlinux.com/linux-columns-cut.html

But if the data file is as follow, column separated by space, what can I do?
Code:
  Year Yield Rainfall Temperature
1 1963	  60        8          56
2 1964    50       10          47
3 1965    70       11          53
4 1966    70       10          53
5 1967    80        9          56
6 1968    50        9          47
7 1969    60       12          44
8 1970    40       11          44

fordeck,

How to do it with awk? TIA


B.R.
satimis
awk '{print $3}'

Will print the 3rd coloum
 
Old 11-23-2010, 07:07 PM   #8
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,677

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by sycamorex View Post
What exactly are you trying to do? Which ones?
Hi,

I prefer to copy column/columns on the table with cut command. I suppose it is easier.

If it can't then how can I do it with awk? TIA

B.R.
satimis
 
Old 11-23-2010, 07:09 PM   #9
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,829
Blog Entries: 1

Rep: Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227
My question is what are you trying to copy? Eg. column 2 to column 4, etc.
 
Old 11-23-2010, 07:21 PM   #10
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,677

Original Poster
Rep: Reputation: 55
Hi fordeck, custangro

$ cat company.data1
Code:
  Year Yield Rainfall Temperature
1 1963	  60        8          56
2 1964    50       10          47
3 1965    70       11          53
4 1966    70       10          53
5 1967    80        9          56
6 1968    50        9          47
7 1969    60       12          44
8 1970    40       11          44
$ awk '{print $1}' company.data1
Code:
Year
1
2
3
4
5
6
7
8
$ awk '{print $2}' company.data1
Code:
 
Yield
1963
1964
1965
1966
1967
1968
1969
1970
$ awk '{print $3}' company.data1
Code:
Rainfall
60
50
70
70
80
50
60
40
Columns 2,3... confused.


B.R.
satimis
 
Old 11-23-2010, 07:25 PM   #11
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,677

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by sycamorex View Post
My question is what are you trying to copy? Eg. column 2 to column 4, etc.
Hi

Column 2 or 3 etc.
Column 2 to 3/4 etc.

B.R.
satimis
 
Old 11-23-2010, 07:25 PM   #12
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,829
Blog Entries: 1

Rep: Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227
Quote:
Columns 2,3... confused.
Probably, the easiest solution would be to add the title header above numbers, eg:

No Year Yield Rainfall Temperature
 
Old 11-23-2010, 07:29 PM   #13
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,829
Blog Entries: 1

Rep: Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227
This would copy column 1 to column 3:
Code:
cat file.txt | awk '{ $1=$3; print; }'
This would swap columns 1 and 3:

Code:
cat file.txt | awk '{ temp=$1; $1=$3; $3=temp; print; }'
 
Old 11-23-2010, 07:31 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
Quote:
Originally Posted by satimis View Post
Hi fordeck and ncsuapex,


Thanks for your advice.

cut command:-

I think following article is relevant:-
How do I Select Columns From a File?
http://lowfatlinux.com/linux-columns-cut.html

But if the data file is as follow, column separated by space, what can I do?
Code:
  Year Yield Rainfall Temperature
1 1963	  60        8          56
2 1964    50       10          47
3 1965    70       11          53
4 1966    70       10          53
5 1967    80        9          56
6 1968    50        9          47
7 1969    60       12          44
8 1970    40       11          44

fordeck,

How to do it with awk? TIA


B.R.
satimis
Quote:
man awk
has a host of useful information!


Cheers,
Tink
 
Old 11-23-2010, 07:48 PM   #15
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,677

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by sycamorex View Post
This would copy column 1 to column 3:
Code:
cat file.txt | awk '{ $1=$3; print; }'
This would swap columns 1 and 3:

Code:
cat file.txt | awk '{ temp=$1; $1=$3; $3=temp; print; }'
Hi,

Sorry they can't work correctly here.

$ cat company.data1 | awk '{ $1=$3; print; }'
Code:
Rainfall Yield Rainfall Temperature
60 1963 60 8 56
50 1964 50 10 47
70 1965 70 11 53
70 1966 70 10 53
80 1967 80 9 56
50 1968 50 9 47
60 1969 60 12 44
40 1970 40 11 44
$ cat company.data1 | awk '{ temp=$1; $1=$3; $3=temp; print; }'
Code:
Rainfall Yield Year Temperature
60 1963 1 8 56
50 1964 2 10 47
70 1965 3 11 53
70 1966 4 10 53
80 1967 5 9 56
50 1968 6 9 47
60 1969 7 12 44
40 1970 8 11 44
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM
Read text file column by column RVF16 Programming 11 05-31-2009 07:16 AM
Concatenate column 1 and column 2 of related lines cgcamal Programming 4 11-20-2008 10:43 AM
sql command to copy column from a table tooparam Programming 1 06-19-2007 05:21 AM
boot error after using Copy Commander to copy debian system onto larger new drive Interceptor Linux - Hardware 7 05-04-2003 12:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:19 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