awk 打印没有固定宽度格式的字符串?

发布于 2024-10-02 14:22:56 字数 1195 浏览 2 评论 0原文

有没有办法让 awk 不打印固定宽度的列?

例如:

"9", "TAYLOR TOWER, JACOB B.   "
"9", "UNIVERSITY HOSPITALS-EAST"
"AR", "SCHOTTENSTEIN CENTER     "
"B1", "NEWARK RESIDENCE HALL    "
"B2", "NEWARK RESIDENCE HALL    "
"CC", "OHIO STADIUM             "
"D1", "TELECOM NETWORK CENTER  "
"DC", "SCHOTTENSTEIN CENTER     "
"EN", "SCHOTTENSTEIN CENTER     "
"GR", "UNIVERSITY HOSPITALS-EAST"
"LL", "RANEY COMMONS, 2ND LT. AL"
"MZ", "PHYSICS RESEARCH        "
"MZ", "SCHOTTENSTEIN CENTER     "
"NS", "STARLING LOVING HALL    "
"P1", "OHIO STADIUM             "
"P2", "OHIO STADIUM             "
"P3", "OHIO STADIUM             "

我希望将其打印为:

"9", "TAYLOR TOWER, JACOB B."
"9", "UNIVERSITY HOSPITALS-EAST"
"AR", "SCHOTTENSTEIN CENTER"
"B1", "NEWARK RESIDENCE HALL"
"B2", "NEWARK RESIDENCE HALL"
"CC", "OHIO STADIUM"
"D1", "TELECOM NETWORK CENTER"
"DC", "SCHOTTENSTEIN CENTER"
"EN", "SCHOTTENSTEIN CENTER"
"GR", "UNIVERSITY HOSPITALS-EAST"
"LL", "RANEY COMMONS, 2ND LT. AL"
"MZ", "PHYSICS RESEARCH"
"MZ", "SCHOTTENSTEIN CENTER"
"NS", "STARLING LOVING HALL"
"P1", "OHIO STADIUM"
"P2", "OHIO STADIUM"
"P3", "OHIO STADIUM"

请注意,此文本中的第二个字段引号内没有空格。这可能吗?

Is there a way to have awk not print a fixed width column?

For example:

"9", "TAYLOR TOWER, JACOB B.   "
"9", "UNIVERSITY HOSPITALS-EAST"
"AR", "SCHOTTENSTEIN CENTER     "
"B1", "NEWARK RESIDENCE HALL    "
"B2", "NEWARK RESIDENCE HALL    "
"CC", "OHIO STADIUM             "
"D1", "TELECOM NETWORK CENTER  "
"DC", "SCHOTTENSTEIN CENTER     "
"EN", "SCHOTTENSTEIN CENTER     "
"GR", "UNIVERSITY HOSPITALS-EAST"
"LL", "RANEY COMMONS, 2ND LT. AL"
"MZ", "PHYSICS RESEARCH        "
"MZ", "SCHOTTENSTEIN CENTER     "
"NS", "STARLING LOVING HALL    "
"P1", "OHIO STADIUM             "
"P2", "OHIO STADIUM             "
"P3", "OHIO STADIUM             "

I want this to be printed as:

"9", "TAYLOR TOWER, JACOB B."
"9", "UNIVERSITY HOSPITALS-EAST"
"AR", "SCHOTTENSTEIN CENTER"
"B1", "NEWARK RESIDENCE HALL"
"B2", "NEWARK RESIDENCE HALL"
"CC", "OHIO STADIUM"
"D1", "TELECOM NETWORK CENTER"
"DC", "SCHOTTENSTEIN CENTER"
"EN", "SCHOTTENSTEIN CENTER"
"GR", "UNIVERSITY HOSPITALS-EAST"
"LL", "RANEY COMMONS, 2ND LT. AL"
"MZ", "PHYSICS RESEARCH"
"MZ", "SCHOTTENSTEIN CENTER"
"NS", "STARLING LOVING HALL"
"P1", "OHIO STADIUM"
"P2", "OHIO STADIUM"
"P3", "OHIO STADIUM"

Notice the second field in this text has no whitespace inside the quotes. Is this possible?

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

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

发布评论

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

评论(2

独夜无伴 2024-10-09 14:22:56

在 AWK 中(gawk/nawk)

{sub(/[ ]*\"$/, "\"");print}

In AWK (gawk/nawk)

{sub(/[ ]*\"$/, "\"");print}
欢你一世 2024-10-09 14:22:56

我会使用 sed 而不是 awk,尽管如果必须的话,您可以在 awk 中执行 gsub

sed -e 's/  *"$/"/'

意思是“将行末尾的一个或多个空格后跟引号变成仅引号”

I'd use sed instead of awk, although you can do gsub in awk if you must:

sed -e 's/  *"$/"/'

Meaning "turn one or more spaces followed by quote at the end of the line into just quote"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文