开发者

Swapping digits

开发者 https://www.devze.com 2023-02-01 11:21 出处:网络
I would like to swap one digit with the previous one: E.g. 123456 to 214365 How wo开发者_如何转开发uld I do this using sed/awk in bash environment?echo 123456 | sed \'s/\\([0-9]\\)\\([0-9]\\)/\\2\\1/g

I would like to swap one digit with the previous one: E.g. 123456 to 214365 How wo开发者_如何转开发uld I do this using sed/awk in bash environment?


echo 123456 | sed 's/\([0-9]\)\([0-9]\)/\2\1/g'


Following your comment on x13n's answer, which answers your question, it seems that you want to be more specific about which digits you swap.

I'd use awk to swap all pairs of digits in the second column:

bash-3.2$ gawk -V | sed -n 1p
GNU Awk 4.0.0

$ echo 254789123456,5306153059630141,639027041150453 | gawk -F',' '
BEGIN {
    OFS=","
}

{
    $2 = gensub(/(.)(.)/, "\\2\\1", "g", $2)
    print
} '
254789123456,3560510395361014,639027041150453

You've asked a number of questions about sed and awk, I'd recommend getting hold of sed & awk, Second Edition.

I'd also recommend reading Jon Skeet's guide to writing the perfect question, which will help you get the answer you need when asking such questions.

0

精彩评论

暂无评论...
验证码 换一张
取 消