This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to switch columns in Linux

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

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

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

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

What OS are you doing this on? macOS?

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

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

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.

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

Convert command is as follows:

dos2unix 2.txt

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.

ok

To remove the ^M What can I do?

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.

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

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.