从行中搜索数字并在特定句子中放置

时间:2013-05-23 12:31:09

标签: python linux awk

我正在努力学习一些python代码。

我有一个包含许多行的文件,例如

50
22
35
41

我希望将这些添加到类似结构的句子中,但保持行的顺序。

E.g

This is test 50 of this friday
This is test 22 of this friday
This is test 35 of this friday
This is test 41 of this friday

3 个答案:

答案 0 :(得分:1)

您使用pythonawk标记了问题。对于这个微不足道的awk似乎是明确的选择:

$ awk '{printf "This is a test %d of this friday\n",$0}' file
This is a test 50 of this friday
This is a test 22 of this friday
This is a test 35 of this friday
This is a test 41 of this friday

答案 1 :(得分:1)

Python相当容易:

with open('file.txt') as f:
    for line in f:
        print("This is a test {} of this friday".format(line.strip()))

答案 2 :(得分:0)

sed one-liner

sed 's/.*/This is test & of this friday/' file

或python(2.7)

with open('file') as f:
    for x in f:
        print "this is test %s of this friday" % x.strip()