awk 意外更改文本
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在
while
循环中使用-r
选项进行read
:这会保留输入中的反斜杠。几乎应该始终使用该选项。让它成为一种习惯。
You need to use the
-r
option forread
in yourwhile
loop:That preserves backslashes in the input. That option should almost always be used. Make it a habit.
如果反斜杠不是可识别的转义序列之一,则 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
如果我正确地阅读了您的代码,您正在尝试:
如果是这样的话,直接使用AWK。将以下内容保存到 toupper_second_field.awk:
第一行将输入 (FS) 和输出 (OFS) 的字段分隔符设置为逗号。第二个将字段 #2 转换为大写,然后打印。调用它:
逻辑更简单,您不必担心反斜杠。
If I read your code correctly, you are trying:
If that is the case, use AWK directly. Save the following to toupper_second_field.awk:
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:
The logic is much simpler and you don't have to worry about backslashes.