简体   繁体   English

如何打印 word get from variable in specific line with AWK?

[英]How to print word get from variable in specific line with AWK?

I wish print in the "END" of the second line(or also in the end the file) a specific word get from a variable.我希望在第二行的“END”(或者也在文件的末尾)打印一个从变量中获取的特定单词。 it would be appreciated to have both methods.拥有这两种方法将不胜感激。

I have a file my_file.txt with this content:我有一个包含以下内容的文件my_file.txt

#Insert the names separeted by space     
l.lovre p.jhonson p.backer t.thompson q.ward

I tried to coding right so:我试着正确编码:

#!/bin/bash

filename=/path/my_file.txt

read -p "Insert name: " name

#my attempt
awk -v myvar="$name" 'END{print myvar}' $filename
 
echo 'Name added'

But doesn't work但不起作用

I would get this result:我会得到这个结果:

my_file.txt:我的文件.txt:

#Insert the names separeted by space     
l.lovre p.jhonson p.backer t.thompson q.ward **name**

Thanks in advance提前致谢

sed is simpler: sed更简单:

sed -i "$ s/$/ $name/" my_file.txt

Edit: Make sure the last line in the file is not empty.编辑:确保文件中的最后一行不为空。

You could add it to the whole line at line 2 in the file with $0 = $0 FS myvar and keep your END block to print it after all the lines since you want that too.您可以使用$0 = $0 FS myvar将它添加到文件中第 2 行的整行,并保留您的END块以在所有行之后打印它,因为您也需要它。 If you don't want it after all the lines, remove the END block.如果您不想在所有行之后使用它,请删除END块。

awk -v myvar="$name" 'FNR==2 {$0 = $0 FS myvar}1; END{print myvar}' $filename

Output with name=Michele : Output name=Michele

#Insert the names separeted by space
l.lovre p.jhonson p.backer t.thompson q.ward Michele
Michele

By default awk does not update the file.默认情况下awk更新文件。 If you want to update the file you have a couple options:如果你想更新文件,你有几个选择:

######
# write to tmp file and then overwrite original file with tmp file

awk -v myvar="$name" 'END{print myvar}' "$filename" > tmpfile && mv tmpfile "$filename"

######
# if using 'GNU awk' you can use '-i inplace' to modify the file

awk -i inplace -v myvar="$name" 'END{print myvar}' "$filename"

As for the issue of appending the new name onto the end of the 2nd line, or creating a new line if there are no names in the current file: one idea:至于将新名称附加到第二行末尾的问题,或者如果当前文件中没有名称则创建新行的问题:一个想法:

######
# writing to tmp file

awk -v myvar="$name" 'FNR==2 { print $0,myvar; added=1; next} 1; END { if (added!=1) print myvar}' "$filename" > tmpfile
mv tmpfile "$filename"

######
# using 'GNU awk'

awk -i inplace -v myvar="$name" 'FNR==2 { print $0,myvar; added=1; next} 1; ENDFILE { if (added!=1) print myvar}' "$filename"

NOTES:笔记:

  • all input lines need to be echoed to stdout in order for them to 'remain' in the file, hence the standalone 1所有输入行都需要回显到标准输出,以便它们“保留”在文件中,因此是独立的1
  • END {} block processing takes place after a file has been processed; END {}块处理发生一个文件被处理之后; any output generated by the END {} block will go to stdout END {}块生成的任何 output 都将 go 输出到标准输出
  • if using GNU awk we have access to the ENDFILE {} block which is, in essence, the same as an END {} block except that processing in this case does apply to the file如果使用GNU awk ,我们可以访问ENDFILE {}块,这在本质上与END {}块相同,只是这种情况下的处理适用于文件

Taking the GNU awk solution for a test drive:GNU awk解决方案进行试驾:

$ filename=names.dat
$ cat "$filename"
# Insert the names separeted by space

$ name='b.bunny'
$ awk -i inplace -v myvar="$name" 'FNR==2 { print $0,myvar; added=1; next} 1; ENDFILE { if (added!=1) print myvar}' "$filename"
$ cat "$filename"
# Insert the names separeted by space
b.bunny

$ name='d.duck'
$ awk -i inplace -v myvar="$name" 'FNR==2 { print $0,myvar; added=1; next} 1; ENDFILE { if (added!=1) print myvar}' "$filename"
$ cat "$filename"
# Insert the names separeted by space
b.bunny d.duck

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

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