读取输入文件并将文本插入输出文件
Reading input file and inserting text into output file
我有一个名为 "namelist.txt" 的输入文件,其中包含电子邮件地址:
user1@domain.com
user2@domain.com
user3@domain.com
我正在尝试从此输入文件中读取地址并将它们的值插入输出文件 (newfile.txt) 中,该文件应为:
user "user1@domain.com" with pass"pass" is "user1@domain.com" here
user "user2@domain.com" with pass"pass" is "user2@domain.com" here
user "user3@domain.com" with pass"pass" is "user3@domain.com" here
我最接近的是使用 awk:
awk '{print "user [=12=] there with pass"pass" is user [=12=] here"}' < namelist.txt > newfile.txt
然而,这会打印以下内容:
user [=13=] there with pass is user [=13=] here
user [=13=] there with pass is user [=13=] here
user [=13=] there with pass is user [=13=] here
这不会打印变量值和 "pass" 值。
我不知道我哪里出错了,也不知道我是否在正确的轨道上,所以任何建议和/或指示将不胜感激。
编辑:
使用:
awk '{print "user \""[=14=]"\" there with pass \"pass\" is user \""[=14=]"\" here}' file
在 Ubuntu (14.04.2 LTS) 上产生以下输出:
" heree with pass "pass" is user"user1@domain.com
" heree with pass "pass" 是用户 "user2@domain.com
" heree with pass "pass" 是用户 "user3@domain.com
上述代码在 UNIX 终端中运行良好,但在 Ubuntu 和 Raspberry Pi.
中均无效
我对发行版之间的差异感到困惑。
$
变量需要在引号之外,您需要转义要查看的引号。
awk '{print "user \""[=10=]"\" there with pass\"pass\" is user \""[=10=]"\" here"}' file
user "user1@domain.com" there with pass"pass" is user "user1@domain.com" here
user "user2@domain.com" there with pass"pass" is user "user2@domain.com" here
user "user3@domain.com" there with pass"pass" is user "user3@domain.com" here
您已将文本 [=11=]
包含在文字字符串中,但在任何情况下,当将输入数据插入某些格式化字符串以进行输出时,如果您使用 printf
而不是 print
:
$ awk '{printf "user \"%s\" there with pass \"pass\" is user \"%s\" here\n", [=10=], [=10=]}' file
user "user1@domain.com" there with pass "pass" is user "user1@domain.com" here
user "user2@domain.com" there with pass "pass" is user "user2@domain.com" here
user "user3@domain.com" there with pass "pass" is user "user3@domain.com" here
并且不要将输入重定向与 awk 一起使用,因为这会删除脚本访问输入文件名的能力。 awk 完全可以自己打开文件。
我有一个名为 "namelist.txt" 的输入文件,其中包含电子邮件地址:
user1@domain.com
user2@domain.com
user3@domain.com
我正在尝试从此输入文件中读取地址并将它们的值插入输出文件 (newfile.txt) 中,该文件应为:
user "user1@domain.com" with pass"pass" is "user1@domain.com" here
user "user2@domain.com" with pass"pass" is "user2@domain.com" here
user "user3@domain.com" with pass"pass" is "user3@domain.com" here
我最接近的是使用 awk:
awk '{print "user [=12=] there with pass"pass" is user [=12=] here"}' < namelist.txt > newfile.txt
然而,这会打印以下内容:
user [=13=] there with pass is user [=13=] here
user [=13=] there with pass is user [=13=] here
user [=13=] there with pass is user [=13=] here
这不会打印变量值和 "pass" 值。
我不知道我哪里出错了,也不知道我是否在正确的轨道上,所以任何建议和/或指示将不胜感激。
编辑:
使用:
awk '{print "user \""[=14=]"\" there with pass \"pass\" is user \""[=14=]"\" here}' file
在 Ubuntu (14.04.2 LTS) 上产生以下输出:
" heree with pass "pass" is user"user1@domain.com
" heree with pass "pass" 是用户 "user2@domain.com
" heree with pass "pass" 是用户 "user3@domain.com
上述代码在 UNIX 终端中运行良好,但在 Ubuntu 和 Raspberry Pi.
中均无效我对发行版之间的差异感到困惑。
$
变量需要在引号之外,您需要转义要查看的引号。
awk '{print "user \""[=10=]"\" there with pass\"pass\" is user \""[=10=]"\" here"}' file
user "user1@domain.com" there with pass"pass" is user "user1@domain.com" here
user "user2@domain.com" there with pass"pass" is user "user2@domain.com" here
user "user3@domain.com" there with pass"pass" is user "user3@domain.com" here
您已将文本 [=11=]
包含在文字字符串中,但在任何情况下,当将输入数据插入某些格式化字符串以进行输出时,如果您使用 printf
而不是 print
:
$ awk '{printf "user \"%s\" there with pass \"pass\" is user \"%s\" here\n", [=10=], [=10=]}' file
user "user1@domain.com" there with pass "pass" is user "user1@domain.com" here
user "user2@domain.com" there with pass "pass" is user "user2@domain.com" here
user "user3@domain.com" there with pass "pass" is user "user3@domain.com" here
并且不要将输入重定向与 awk 一起使用,因为这会删除脚本访问输入文件名的能力。 awk 完全可以自己打开文件。