2016-01-22 190 views
1

我有一个名为“namelist.txt”与电子邮件地址输入文件:读取输入文件,插入文本输出文件

[email protected] 
[email protected] 
[email protected] 

我试图读取该输入文件中的地址,将其价值的应该读输出文件(newfile.txt):

user "[email protected]" with pass"pass" is "[email protected]" here 
user "[email protected]" with pass"pass" is "[email protected]" here 
user "[email protected]" with pass"pass" is "[email protected]" here 

我来最接近的是用awk:

awk '{print "user $0 there with pass"pass" is user $0 here"}' <namelist.txt> newfile.txt 

然而,这打印如下:

user $0 there with pass is user $0 here 
user $0 there with pass is user $0 here 
user $0 there with pass is user $0 here 

这不打印变量值,也不是“通”值。

我不知道我错了,还是我甚至在正确的轨道,所以任何意见和/或指针将不胜感激。

编辑:

使用:

awk '{print "user \""$0"\" there with pass \"pass\" is user \""$0"\" here}' file 

导致在Ubuntu(14.04.2 LTS)以下输出:

“heree与通 ”合格“ 是用户” USER1 @ domain.com
“heree with pass”pass“is user”[email protected]
“heree with pass”pass“is user”[email protected]

上述代码工作正常在UNIX终端而不在Ubuntu也不树莓裨。

我很困惑,为什么发行版之间的差异。

回答

3

$变量需要报价之外,你需要逃避你想看到的报价。

awk '{print "user \""$0"\" there with pass\"pass\" is user \""$0"\" here"}' file 

user "[email protected]" there with pass"pass" is user "[email protected]" here 
user "[email protected]" there with pass"pass" is user "[email protected]" here 
user "[email protected]" there with pass"pass" is user "[email protected]" here 
+0

感谢您的快速回复。我想我可能还有其他问题。当我在联机unix终端中尝试你的答案时,它的工作原理非常完美。然而,当我尝试在Ubuntu(或Raspi)我得到不同的结果:“heree与通”通”是用户‘[email protected] ’heree与通‘通’是用户‘[email protected] ’heree与通“通”是用户“[email protected] – hibbijibbies

+0

显示在您的文章你格式化文本的任何问题,这是不可能的阅读和猜你在评论中写的是什么格式。 – 123

+0

对不起。我编辑了原文,以反映我尝试使用您的答案。 – hibbijibbies

2

将数据输入到输出的一些格式的字符串时,您已经包含文字字符串内,但在任何情况下,文本$0,它通常是最清晰和最容易在将来提高,如果你使用printf代替print

$ awk '{printf "user \"%s\" there with pass \"pass\" is user \"%s\" here\n", $0, $0}' file 
user "[email protected]" there with pass "pass" is user "[email protected]" here 
user "[email protected]" there with pass "pass" is user "[email protected]" here 
user "[email protected]" there with pass "pass" is user "[email protected]" here 

并且不使用awk输入重定向,因为这会删除脚本访问输入文件名的能力。 awk完全有能力打开文件本身。

+1

Thx为您的洞察力。我非常重视它。 – hibbijibbies