how to exchange two columns of a file
hello,
now i have met a problem as follows: there is a file having two columns for examples: Name Age ..... ..... ...... ..... ..... ..... what should i do to exchange these two columns and save the file like that: Age Name ..... ....... ..... ....... ..... ....... Thanks a lot! |
If you have a file called list containing:
Code:
Bruce 34 Code:
awk '{ print $2 " " $1 }' list |
another method is to use the 'cut' and 'paste' commands
'cut -f2 fileName' will cut the 'Age' column; the second field of your tab-delimited file 'fileName'. 'cut -f1 fileName' will cut the 'Name' column; the second field of your tab-delimited file 'fileName'. you could save the output of these commands to a file, ie 'age' and 'name', then run 'paste age name' and it will print the original file, with the columns switched. again, just save the output of that paste command to a file and itll be saved... and delete the temoprary files created with the 2 cut commands. this is messy but oh well.. just another method. |
Quote:
I will learn more about "awk". that's powerful |
Quote:
|
also thanks a lot:)
Quote:
|
your very welcome jackk
|
I am a beginner of linux and just now enter a company about linux.
I graduated from university only just , so i have more to learn and have a lot of things to ask, i will disturb you again, hehe. Quote:
|
'awk' is definitely a program to get to know.
Basically, it's a complete programmable tool based on the idea of pattern-recognition. So you can search for various patterns (any one of several), as well as other, more-sophisticated conditions. And you can then break-up the resulting strings pretty much any way you like. One of the most useful things I've done with it is to extract output from a mainframe printout. The mainframe could spit out the "printout" to a file which we could then download. And AWK could do the rest, even though the printout was anything but regular. There was a commercial product called "cambio" which did much the same thing, with a fancy GUI. |
All times are GMT -5. The time now is 06:55 AM. |