With this command Columns shifted But last column moved to the next line
How to switch columns 5 and 6 in a file in Linux؟Without going the last column into the second line
I data samples as follows:
R950E03 111006 930226 910008 Resistant 1
R950E06 110917 950085 910043 Resistant 2
R950C09 110892 950085 910125 Resistant 2
R949B05 111101 870141 840409 Resistant 2
2 answers
Assuming, your data are in data.txt:
awk 'BEGIN {OFS="\t";} {tmp=$5;$5=$6;$6=tmp;print $0;}' data.txt
Edit: mixed up blockquote and code Edit2: added BEGIN-block, setting OFS to tab
awk '{tmp=$5;$5=$6;$6=tmp;print $0;}' test.txt
yields
first line: R950E03 111006 930226 910008 1 Resistant
second line: R950E06 110917 950085 910043 2 Resistant
third line: R950C09 110892 950085 910125 2 Resistant
fourth line: R949B05 111101 870141 840409 2 Resistant
for me...
my OS is linux.........
Note that in your example, if the delimiters were tabulations, they will be silently converted to spaces:
$ cat test.txt
a b c
1 2 3
$ cat -A test.txt
a^Ib^Ic$
1^I2^I3$
$ awk '{print $1, $3, $2}' test.txt | cat -A
a c b$
1 3 2$
$ awk '{OFS="\t"}{print $1, $3, $2}' test.txt | cat -A
a^Ic^Ib$
1^I3^I2$
Thanks for your guidance. But the $ ,^M signs in output There! I do not want this mark The output is shown below:
R950E03 1 111006 930226 910008 1^M Resistant$
R950E06 1 110917 950085 910043 2^M Resistant$
R950C09 1 110892 950085 910125 2^M Resistant$
It's usually an CR-character. Meaning you have a file from the Windows/Dos world. Just run the dos2unix on that file to convert it.
Good point, all too easy to forget about that.
Try to learn yourself the magic of common linux tools, in this case cut and paste. Grep, sed and awk are also good friends.
paste <(cut -f1-4,6 yourfile.txt) <(cut -f5 yourfile.txt) > rearrangedfile.txt
Solved an age-old question of mine how to "leave out a column" without awk. Of course, process substitution...
I find it more intuitive than awk solutions, but whatever gets the job done...
Yea, I agree, until a couple of months ago, I really hated awk-based solutions. That's why I like your's as I never thought of that way and usually reverted to a python-based solution or something like that...
With this command. Data was repeated!!!!!!!!!!
1 R950E03 111006 930226 910008 Resistant 1
1 R950E06 110917 950085 910043 Resistant 2
1 R950C09 110892 950085 910125 Resistant 2
1 R950E03 111006 930226 910008 Resistant 1
1 R950E06 110917 950085 910043 Resistant 2
1 R950C09 110892 950085 910125 Resistant 2
I noticed I made a mistake and corrected it, but that doesn't explain the duplication of those columns. Also, I will understand your sentence with fewer exclamation marks, thank you.
What is your text delimiter?
My language is not English! And I do not understand the meaning of some sentences Please speak more clearly
I didn't mean to be rude. I just wasn't aware that there are languages in which adding 10 exclamation marks was considered polite.
What is your text delimiter?
By which invisible character, such as a tab or space, are the columns in your text file separated?
space...............
Log in to answer this question.
First stop for these sort of questions should be a search. http://stackoverflow.com/questions/11967776/swap-two-columns-awk-sed-python-perl
http://www.theunixschool.com/2012/11/how-to-swap-2-columns-in-file-in-linux.html