简体   繁体   English

如何在Unix Shell脚本中找到文件中字符串的位置

[英]how to find the position of a string in a file in unix shell script

Can you please help me solve this puzzle? 你能帮我解决这个难题吗? I am trying to print the location of a string (ie, line #) in a file, first to the std output, and then capture that value in a variable to be used later. 我试图在文件中打印字符串(即行号)的位置,首先打印到std输出,然后在变量中捕获该值,以备后用。 The string is “my string”, the file name is “myFile” which is defined as follows: 字符串为“我的字符串”,文件名为“ myFile”,定义如下:

this is first line
this is second line
this is my string on the third line
this is fourth line
the end

Now, when I use this command directly at the command prompt: 现在,当我直接在命令提示符下使用此命令时:

% awk ‘s=index($0, “my string”) { print “line=” NR, “position= ” s}’ myFile

I get exactly the result I want: 我得到的正是我想要的结果:

% line= 3, position= 9

My question is: if I define a variable VAR=”my string”, why can't I get the same result when I do this: 我的问题是:如果定义变量VAR =“我的字符串”,为什么在执行此操作时无法得到相同的结果:

% awk ‘s=index($0, $VAR) { print “line=” NR, “position= ” s}’ myFile

It just won't work!! 只是行不通! I even tried putting the $VAR in quotation marks, to no avail? 我什至尝试将$ VAR放在引号中,无济于事? I tried using VAR (without the $ sign), no luck. 我尝试使用VAR(不带$符号),但不走运。 I tried everything I could possibly think of ... Am I missing something? 我尝试了所有我可能想到的一切……我是否缺少某些东西?

awk variables are not the same as shell variables. awk变量与shell变量不同。 You need to define them with the -v flag 您需要使用-v标志来定义它们

For example: 例如:

$ awk -v var="..." '$0~var{print NR}' file

will print the line number(s) of pattern matches. 将打印图案匹配的行号。 Or for your case with the index 或针对您的索引

$ awk -v var="$Var" 'p=index($0,var){print NR,p}' file

using all uppercase may not be good convention since you may accidentally overwrite other variables. 使用所有大写字母可能不是一个好习惯,因为您可能会意外覆盖其他变量。

to capture the output into a shell variable 将输出捕获到shell变量中

$ info=$(awk ...)

for multi line output assignment to shell array, you can do 对于将多行输出分配给shell数组,您可以执行

$ values=( $(awk ...) ); echo ${values[0]}

however, if the output contains more than one field, it will be assigned it's own array index. 但是,如果输出包含多个字段,则会为其分配自己的数组索引。 You can change it with setting the IFS variable, such as 您可以通过设置IFS变量来更改它,例如

$ IFS=$(echo -en "\n\b"); values=( $(awk ...) )

which will capture the complete lines as the array values. 它将捕获完整的行作为数组值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM