user2413531
user2413531

Reputation: 1

search number from lines and place in specific sentence

I am struggling with some python code.

I have a file with many lines e.g

50
22
35
41

I wish to add these into a sentences of similar structure however keeping the order of the lines.

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

Upvotes: 0

Views: 53

Answers (3)

Kent
Kent

Reputation: 195179

sed one-liner

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

or python (2.7)

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

Upvotes: 0

pedram
pedram

Reputation: 3087

Fairly easy in Python:

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

Upvotes: 1

Chris Seymour
Chris Seymour

Reputation: 85845

You tagged the question with python and awk. For something this trivial awk seems the clear choice:

$ 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

Upvotes: 1

Related Questions