How to switch columns in Linux
2
0
Entering edit mode
8.0 years ago
mm ▴ 20

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 • 13k views
ADD COMMENT
3
Entering edit mode
8.0 years ago
cschu181 ★ 2.8k

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 COMMENT
2
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
1
Entering edit mode

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 REPLY
0
Entering edit mode

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

Convert command is as follows:

dos2unix 2.txt

ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

ok

To remove the ^M What can I do?

ADD REPLY
1
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

Good point, all too easy to forget about that.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode
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 REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

What OS are you doing this on? macOS?

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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 REPLY
1
Entering edit mode
8.0 years ago

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 COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2000 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6