AWK 脚本:如何使用 awk 删除字段分隔符

发布于 2024-09-03 16:58:15 字数 769 浏览 2 评论 0原文

以下输出

ONGC044 
ONGC043
ONGC042
ONGC041
ONGC046
ONGC047

需要从此输入得到

Medium Label                   Medium ID                            Free Blocks
===============================================================================
[ONGC044] ECCPRDDB_FS_43       ac100076:4aed9b39:44f0:0001            195311616
[ONGC043] ECCPRDDB_FS_42       ac100076:4aed9b1d:44e8:0001            195311616
[ONGC042] ECCPRDDB_FS_41       ac100076:4aed9af4:4469:0001            195311616
[ONGC041] ECCPRDDB_FS_40       ac100076:4aed9ad3:445e:0001            195311616
[ONGC046] ECCPRDDB_FS_44       ac100076:4aedd04a:68c6:0001            195311616
[ONGC047] ECCPRDDB_FS_45       ac100076:4aedd4a0:6bf5:0001            195311616

Need the following output

ONGC044 
ONGC043
ONGC042
ONGC041
ONGC046
ONGC047

from this input

Medium Label                   Medium ID                            Free Blocks
===============================================================================
[ONGC044] ECCPRDDB_FS_43       ac100076:4aed9b39:44f0:0001            195311616
[ONGC043] ECCPRDDB_FS_42       ac100076:4aed9b1d:44e8:0001            195311616
[ONGC042] ECCPRDDB_FS_41       ac100076:4aed9af4:4469:0001            195311616
[ONGC041] ECCPRDDB_FS_40       ac100076:4aed9ad3:445e:0001            195311616
[ONGC046] ECCPRDDB_FS_44       ac100076:4aedd04a:68c6:0001            195311616
[ONGC047] ECCPRDDB_FS_45       ac100076:4aedd4a0:6bf5:0001            195311616

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗失的美好 2024-09-10 16:58:15
awk -F"[][]" 'NR>2{print $2}' file
awk -F"[][]" 'NR>2{print $2}' file
骄傲 2024-09-10 16:58:15

我认为你想要的是:

awk '/^\[.*\]/  {print substr($1,2,7)}' < ~/tmp/awk_test/file

这假设你的第一个字段每次正好是 9 个(其中 7 个是你想要的)字符。如果不是这种情况,请使用以下命令从第一个字段中删除 [ 和 ]:

awk '/^\[.*\]/  {gsub(/[\[\]]/,"",$1); print $1 }' < ~/tmp/awk_test/file

I think what you want is:

awk '/^\[.*\]/  {print substr($1,2,7)}' < ~/tmp/awk_test/file

This assumes that your first field is exactly 9 (7 of them are the ones you want) characters each time. If this is not the case, then use the following instead to strip off of the [ and the ] from the first field:

awk '/^\[.*\]/  {gsub(/[\[\]]/,"",$1); print $1 }' < ~/tmp/awk_test/file
伴我老 2024-09-10 16:58:15

如果数据确实那么统一,一个非常简单的管道 to:

cut -b 2-8

将为您完成。

(哦,除了前两行之外;如果需要的话,可以在管道中使用 grep ^\[ 删除它们)

If the data is really that uniform, a very simple pipe to:

cut -b 2-8

will do it for you.

(Oh, apart from the first two lines; get rid of them with grep ^\[ in your pipeline, if you need to)

假装不在乎 2024-09-10 16:58:15

这是一个仅涉及 awk 的解决方案。这个想法是让 awk 解析所需的列并从此标记中删除不需要的方括号。唯一的“技巧”是“转义”[ 字符,以免它被理解为打开重新设置。 (我们也可以使用 substr 来代替,因为括号预计作为第一个和最后一个字符)

{
     #skip the column header lines
     if (NR < 3)
       next;

     # $1 is almost what we want: [xxxx] 
     #   ==> just remove the square brackets
     sub("[[]", "", $1);
     sub("]", "", $1);
     print $1;
}

Here's a solution which only involves awk. The idea is to let awk parse the desired column and remove the undesired square brackets from this token. The only "trick" is to "escape" the [ character lest it is understood as an opening re set. (We could also use substr instead since the brackets are expected as the first and last characters)

{
     #skip the column header lines
     if (NR < 3)
       next;

     # $1 is almost what we want: [xxxx] 
     #   ==> just remove the square brackets
     sub("[[]", "", $1);
     sub("]", "", $1);
     print $1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文