查找和替换仅限于特定列(单行) [英] Find and replace confined to a specific column (one-liner)

查看:71
本文介绍了查找和替换仅限于特定列(单行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法(在一行中)将查找和替换限制在制表符分隔的 .txt 文件的特定列中?

Is there a way (in a one-liner) to confine find and replace to within a specific column of a tab-delimited .txt file?

例如:

perl -pe 's/A/B/g;'

但是它只在第 2 列中找到并用 'B' 替换 'A' 并忽略 A 的所有其他实例,转换此:

But which only finds and replaces 'A' with 'B' within column-2 and ignores all other instances of A, converting this:

1   A   A2
2   A   A
3   B   B1
4   A   B
5   A   A
6   B   A3
7   A   A
8   C   A
9   E   B
10  D   B
11  A   C6
12  A   G
13  B   L
14  A   E
15  B   A
16  A   A

进入:

1   B   A2
2   B   A
3   B   B1
4   B   B
5   B   A
6   B   A3
7   B   A
8   C   A
9   E   B
10  D   B
11  B   C6
12  B   G
13  B   L
14  B   E
15  B   A
16  B   A

推荐答案

这样的操作将转换制表符分隔行的第二个字段.

Something like this will transform the second field of a tab separated row.

perl -lne '@row = split /\t/; $row[1] =~ s/A/B/; print join ( "\t", @row );' 

这篇关于查找和替换仅限于特定列(单行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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