awk 意外更改文本

发布于 2024-10-22 18:57:16 字数 465 浏览 1 评论 0原文

我在 shell 脚本中使用以下 awk 语句。

#!/bin/sh
# read file line by line
file="/pdump/country.000000.txt"
while read line
do
mycol=`echo $line | awk -F"," '{print $2}'`
mycol_new=`echo $mycol | tr "[:lower:]" [:upper:]`
echo $line | awk -v var="$mycol_new" -F"," '{print $1 "," var "," $3 "," $4 "," $5 "," $6 "," $7 "," $8}'
done < $file

它正在按预期工作。

唯一的问题是,如果原始文本在任何其他列中为 \N(斜杠 N),例如 $4 或 $7,则它会更改为 N(不带斜杠)。 如何在仅替换第二列的同时保留原始值。

I am using the following awk statement in my shell script.

#!/bin/sh
# read file line by line
file="/pdump/country.000000.txt"
while read line
do
mycol=`echo $line | awk -F"," '{print $2}'`
mycol_new=`echo $mycol | tr "[:lower:]" [:upper:]`
echo $line | awk -v var="$mycol_new" -F"," '{print $1 "," var "," $3 "," $4 "," $5 "," $6 "," $7 "," $8}'
done < $file

It is working as expected.

The only problem is that if the original text is \N (slash N) in any other column for e.g. $4 or $7 then it changes to N (without slash).
How do I preserve the original values while replacing only the second column.

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

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

发布评论

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

评论(3

千纸鹤 2024-10-29 18:57:16

您需要在 while 循环中使用 -r 选项进行 read

while read -r line

这会保留输入中的反斜杠。几乎应该始终使用该选项。让它成为一种习惯。

You need to use the -r option for read in your while loop:

while read -r line

That preserves backslashes in the input. That option should almost always be used. Make it a habit.

风筝在阴天搁浅。 2024-10-29 18:57:16

如果反斜杠不是可识别的转义序列之一,则 awk 会去掉它。因此,如果它是 \n,awk 会将其识别为换行符,但 \N 会简单地解释为 N。更多详细信息 此处

awk strips out the backslash if it's not one of the recognized escape sequences. So if it was \n, awk would have recognized it as newline but \N is simply interpreted as N. More details here

浪漫之都 2024-10-29 18:57:16

如果我正确地阅读了您的代码,您正在尝试:

  1. 从逗号分隔值 (CSV) 文件中读取输入
  2. 将第二个字段更改为大写
  3. 打印结果。

如果是这样的话,直接使用AWK。将以下内容保存到 toupper_second_field.awk:

BEGIN { FS = ","; OFS="," }
{ $2 = toupper($2); print }

第一行将输入 (FS) 和输出 (OFS) 的字段分隔符设置为逗号。第二个将字段 #2 转换为大写,然后打印。调用它:

awk -f toupper_second_field.awk /pdump/country.000000.txt

逻辑更简单,您不必担心反斜杠。

If I read your code correctly, you are trying:

  1. Read input from a comma-separated-values (CSV) file
  2. Change the second field to uppercase
  3. Print the result.

If that is the case, use AWK directly. Save the following to toupper_second_field.awk:

BEGIN { FS = ","; OFS="," }
{ $2 = toupper($2); print }

The first line sets the field separators for both input (FS) and output (OFS) to comma. The second converts field #2 to upper case, then print. To invoke it:

awk -f toupper_second_field.awk /pdump/country.000000.txt

The logic is much simpler and you don't have to worry about backslashes.

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