Question: How to switch columns in Linux
0
gravatar for mm
2.8 years ago by
mm20
mm20 wrote:

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

linux • 4.5k views
ADD COMMENTlink modified 2.8 years ago by cschu1811.8k • written 2.8 years ago by mm20
3
gravatar for cschu181
2.8 years ago by
cschu1811.8k
cschu1811.8k wrote:

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

ADD COMMENTlink modified 2.8 years ago • written 2.8 years ago by cschu1811.8k
2

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$
ADD REPLYlink written 2.8 years ago by Charles Plessy2.7k

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$

ADD REPLYlink modified 2.8 years ago • written 2.8 years ago by mm20
1

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.

ADD REPLYlink written 2.8 years ago by michael.ante3.4k

I convert the file. But there is still a sign!

Convert command is as follows:

dos2unix 2.txt

ADD REPLYlink written 2.8 years ago by mm20

If it's only the '$', it's OK. This is a Unix encoding for end of line/ new line.

Charles Plessy's reply shows the typical behaviour of a unix file.

ADD REPLYlink modified 2.8 years ago • written 2.8 years ago by michael.ante3.4k

ok

To remove the ^M What can I do?

ADD REPLYlink modified 2.8 years ago • written 2.8 years ago by mm20
1

dos2unix should have got rid of those. Since it has not

sed -e "s/\^M//g" filename > new_filename Note: You need to type ctrl+v ctrl+M to type ^M correctly.

ADD REPLYlink modified 2.8 years ago • written 2.8 years ago by genomax71k

Are you currently using the | cat -A, by chance?

ADD REPLYlink written 2.8 years ago by cschu1811.8k

Good point, all too easy to forget about that.

ADD REPLYlink written 2.8 years ago by cschu1811.8k

With this command Columns shifted But last column moved to the next line

ADD REPLYlink written 2.8 years ago by mm20
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...

ADD REPLYlink modified 2.8 years ago by genomax71k • written 2.8 years ago by cschu1811.8k

my OS is linux.........

ADD REPLYlink modified 2.8 years ago • written 2.8 years ago by mm20

What OS are you doing this on? macOS?

ADD REPLYlink written 2.8 years ago by genomax71k

In my case it works fine on both macOS and a CentOS bash shell...

ADD REPLYlink written 2.8 years ago by cschu1811.8k

I've seen similar things with Windows/dos line terminators, but can't replicate that here since my home computer is Ubuntu. Maybe tomorrow.

ADD REPLYlink written 2.8 years ago by WouterDeCoster41k
1
gravatar for WouterDeCoster
2.8 years ago by
Belgium
WouterDeCoster41k wrote:

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
ADD COMMENTlink modified 2.8 years ago • written 2.8 years ago by WouterDeCoster41k

Solved an age-old question of mine how to "leave out a column" without awk. Of course, process substitution...

ADD REPLYlink written 2.8 years ago by cschu1811.8k

I find it more intuitive than awk solutions, but whatever gets the job done...

ADD REPLYlink written 2.8 years ago by WouterDeCoster41k
1

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...

ADD REPLYlink written 2.8 years ago by cschu1811.8k

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

ADD REPLYlink written 2.8 years ago by mm20

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?

ADD REPLYlink written 2.8 years ago by WouterDeCoster41k

My language is not English! And I do not understand the meaning of some sentences Please speak more clearly

ADD REPLYlink written 2.8 years ago by mm20
1

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?

ADD REPLYlink modified 2.8 years ago • written 2.8 years ago by WouterDeCoster41k

space...............

ADD REPLYlink written 2.8 years ago by mm20
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 709 users visited in the last hour