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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
11-23-2010, 10:59 AM
|
#1
|
|
Senior Member
Registered: Apr 2003
Posts: 3,698
Rep:
|
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
|
|
|
|
11-23-2010, 11:22 AM
|
#2
|
|
Member
Registered: Oct 2006
Location: Utah
Posts: 520
Rep:
|
You could use awk to accomplish this. Another way would be using the "cut" command.
|
|
|
|
11-23-2010, 11:23 AM
|
#3
|
|
Member
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770
Rep:
|
you can use the "cut" command to do this.
|
|
|
|
11-23-2010, 06:50 PM
|
#4
|
|
Senior Member
Registered: Apr 2003
Posts: 3,698
Original Poster
Rep:
|
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
|
|
|
|
11-23-2010, 07:02 PM
|
#5
|
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
What exactly are you trying to do? Which ones?
|
|
|
|
11-23-2010, 07:04 PM
|
#6
|
|
Member
Registered: Oct 2006
Location: Utah
Posts: 520
Rep:
|
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
|
|
|
|
11-23-2010, 07:04 PM
|
#7
|
|
Senior Member
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
|
Quote:
Originally Posted by satimis
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
|
|
|
|
11-23-2010, 07:07 PM
|
#8
|
|
Senior Member
Registered: Apr 2003
Posts: 3,698
Original Poster
Rep:
|
Quote:
Originally Posted by sycamorex
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
|
|
|
|
11-23-2010, 07:09 PM
|
#9
|
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
My question is what are you trying to copy? Eg. column 2 to column 4, etc.
|
|
|
|
11-23-2010, 07:21 PM
|
#10
|
|
Senior Member
Registered: Apr 2003
Posts: 3,698
Original Poster
Rep:
|
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
|
|
|
|
11-23-2010, 07:25 PM
|
#11
|
|
Senior Member
Registered: Apr 2003
Posts: 3,698
Original Poster
Rep:
|
Quote:
Originally Posted by sycamorex
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
|
|
|
|
11-23-2010, 07:25 PM
|
#12
|
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
Probably, the easiest solution would be to add the title header above numbers, eg:
No Year Yield Rainfall Temperature
|
|
|
|
11-23-2010, 07:29 PM
|
#13
|
|
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
|
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; }'
|
|
|
|
11-23-2010, 07:31 PM
|
#14
|
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
Quote:
Originally Posted by satimis
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
|
has a host of useful information!
Cheers,
Tink
|
|
|
|
11-23-2010, 07:48 PM
|
#15
|
|
Senior Member
Registered: Apr 2003
Posts: 3,698
Original Poster
Rep:
|
Quote:
Originally Posted by sycamorex
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
|
|
|
|
All times are GMT -5. The time now is 04:56 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|