尴尬变量和函数语法
我想在文本中删除“ _”和“:”:然后将文本转换为尴尬的小写,但无法正常工作。这是一个简单的data.txt
文件:
Count_Part:AA,1,2,3,4,5
Name_Date:BB,4,5,6,7,8
和我的脚本process.awk
:
BEGIN{
FS=",";
}
{
print("raw -->", $1);
replaced=$gsub(/_|:/, "", $1);
print("replacement -->", $replaced);
lowered=$tolower($replaced);
print("to lowercase -->", $lowered);
print("\n");
}
但是我从cat data.txt | awk -f process.awk
是这样,这不是我所期望的:
raw --> Count_Part:AA
replacement --> CountPartAA
to lowercase --> CountPartAA 1 2 3 4 5
raw --> Name_Date:BB
replacement --> 6
to lowercase -->
我想知道1)为什么countpartaa
不打印为countpartaaa
,而2 )为什么data.txt的第二行没有与第一行相似的输出。
我怀疑这是由于变量分配和函数返回语法所致,但我无法使其起作用。我的预期输出就是这样:
raw --> Count_Part:AA
replacement --> CountPartAA
to lowercase --> countpartaa
raw --> Name_Date:BB
replacement --> NameDateBB
to lowercase --> namedatebb
请帮助。谢谢!
I would like to remove "_" and ":" in the texts and then convert the texts into lowercase in AWK but couldn't get it working. Here's a simple data.txt
file:
Count_Part:AA,1,2,3,4,5
Name_Date:BB,4,5,6,7,8
and my script process.awk
:
BEGIN{
FS=",";
}
{
print("raw -->", $1);
replaced=$gsub(/_|:/, "", $1);
print("replacement -->", $replaced);
lowered=$tolower($replaced);
print("to lowercase -->", $lowered);
print("\n");
}
but what I get from cat data.txt | awk -f process.awk
is this, which is not what I expected:
raw --> Count_Part:AA
replacement --> CountPartAA
to lowercase --> CountPartAA 1 2 3 4 5
raw --> Name_Date:BB
replacement --> 6
to lowercase -->
I am wondering 1) why the CountPartAA
is not printed as countpartaa
, and 2) why the 2nd row of the data.txt did not have the similar output as the 1st row did.
I suspect it's due to the variable assignment and function return syntax but I could not make it work. My expected output is like this:
raw --> Count_Part:AA
replacement --> CountPartAA
to lowercase --> countpartaa
raw --> Name_Date:BB
replacement --> NameDateBB
to lowercase --> namedatebb
Please kindly help. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您接近,需要在功能和变量上删除Sigills
$
$ 1:输出
You are close, need to remove sigills
$
on functions and variables apart $1 :output
你是这样的吗?
you mean like this ?
如前所述,您不应使用
$
进行前缀函数调用,因此不要:但是
您也误解了
gsub
的工作方式,来自 gnu awk用户指南因此您可能会忽略返回值,如果您只需要更改字符串,例如,如果您需要从第一列中删除数字,则可能只需
As already noted, you should not prefix function calls using
$
so do not:but rather
You are also misunderstanding how
gsub
works, from GNU AWK User's GuideTherefore you might disregard return value if you just need changes in string, e.g. if you need to remove digits from first column you might just do