If I have a list such as:
5 chr1
5 chr1
5 chr2
3 chr5
How do I use grep -P to switch the position of column 1 and 2?
If I use this line:
grep -P "(\w+)\t(\w+)" test.txt
to capture the text, how would I then replace it?
Better (easier) to do that in awk:
awk '{a=$1; $1=$2; $2=a}1' OFS='\t' file
chr1 5
chr1 5
chr2 5
chr5 3
Even simpler is: (thanks to @BrenoZan)
awk '{print $2"\t"$1}' file
You could do this
grep -oP "\w+" file | rev | paste - - | rev
but you wouldn't
您可以使用@@ anubhava提及之类的awk ( 我爱awk )可以正常工作,但是如果您要更改文件内容,请尝试sed ...
sed 's/\([^ ]\+\)\s*\(.*\)$/\2\t\1/' -i file
与纯壳。
while read -r a b X; do echo $b $a; done < test.txt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.