仅使用 awk 和 sed 修改第 2 列 [英] Modify column 2 only using awk and sed

查看:44
本文介绍了仅使用 awk 和 sed 修改第 2 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3 列的列表:

I have a list of 3 columns:

2547123456789,5391074043372870,639027123456789

我想像这样修改第二列:

I want to modify second column like this:

sed 's/([0-9])([0-9])/21/g' so that it becomes:

3519700434738207

我的问题是如何在一行 awk/sed 中执行此操作,同时保持其他列不变,以便我的最终文件具有:

My problem is how do I do this in one line of awk/sed while leaving the other columns untouched so that my final file has:

2547123456789,3519700434738207,639027123456789

谢谢

推荐答案

#!/bin/sh

awk -F, '{
            printf("%s,",$1)
            len=split($2,a,"")
            for(i=1;i<len;i+=2)
              printf("%s%s",a[i+1],a[i])
            printf(",%s
",$3)
         }' /path/to/input/file

输入

$ cat infile
2547123456789,5391074043372870,639027123456789
1234567890123,1234567890123456,123456789012345

输出

awk -F, '{printf("%s,",$1);len=split($2,a,"");for(i=1;i<len;i+=2)printf("%s%s",a[i+1],a[i]);printf(",%s
",$3)}' ./infile
2547123456789,3519700434738207,639027123456789
1234567890123,2143658709214365,123456789012345

这篇关于仅使用 awk 和 sed 修改第 2 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆