尴尬变量和函数语法

发布于 2025-02-04 18:43:53 字数 1099 浏览 2 评论 0原文

我想在文本中删除“ _”和“:”:然后将文本转换为尴尬的小写,但无法正常工作。这是一个简单的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 技术交流群。

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

发布评论

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

评论(3

画骨成沙 2025-02-11 18:43:53

您接近,需要在功能和变量上删除Sigills $ $ 1:

BEGIN{
  FS=",";
}
{
        print("raw -->", $1);
        gsub(/_|:/, "", $1);     # return integer and modify $1 directly 
        replaced=$1
        print("replacement -->", replaced);
        lowered=tolower(replaced);
        print("to lowercase -->", lowered);
        print("\n");
}

输出

raw --> Count_Part:AA
replacement --> CountPartAA
to lowercase --> countpartaa


raw --> Name_Date:BB
replacement --> NameDateBB
to lowercase --> namedateb

You are close, need to remove sigills $ on functions and variables apart $1 :

BEGIN{
  FS=",";
}
{
        print("raw -->", $1);
        gsub(/_|:/, "", $1);     # return integer and modify $1 directly 
        replaced=$1
        print("replacement -->", replaced);
        lowered=tolower(replaced);
        print("to lowercase -->", lowered);
        print("\n");
}

output

raw --> Count_Part:AA
replacement --> CountPartAA
to lowercase --> countpartaa


raw --> Name_Date:BB
replacement --> NameDateBB
to lowercase --> namedateb
久随 2025-02-11 18:43:53

你是这样的吗?

{m,n,g}awk '$!NF=tolower($!(NF=NF))' FS='[_:]' OFS=
 
countpartaa,1,2,3,4,5
namedatebb,4,5,6,7,8

you mean like this ?

{m,n,g}awk '$!NF=tolower($!(NF=NF))' FS='[_:]' OFS=
 
countpartaa,1,2,3,4,5
namedatebb,4,5,6,7,8
年华零落成诗 2025-02-11 18:43:53

如前所述,您不应使用$进行前缀函数调用,因此不要:

replaced=$gsub(/_|:/, "", $1);

但是

replaced=gsub(/_|:/, "", $1);

您也误解了gsub的工作方式,来自 gnu awk用户指南

gsub(regexp,替换[,target])

search target 最长,最左, nonoverplapping 匹配子字符串可以找到并用替换。(...)gsub()函数返回所做的替换数。(...)


因此您可能会忽略返回值,如果您只需要更改字符串,例如,如果您需要从第一列中删除数字,则可能只需

awk '{gsub(/[0-9]/, "", $1);print}' file.txt

As already noted, you should not prefix function calls using $ so do not:

replaced=$gsub(/_|:/, "", $1);

but rather

replaced=gsub(/_|:/, "", $1);

You are also misunderstanding how gsub works, from GNU AWK User's Guide

gsub(regexp, replacement [, target])

Search target for all of the longest, leftmost, nonoverlapping matching substrings it can find and replace them with replacement.(...)The gsub() function returns the number of substitutions made.(...)

Therefore 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

awk '{gsub(/[0-9]/, "", $1);print}' file.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文