With bash
:
while read -r line
do
echo "I have a $line"
done < file > tmp_file && mv tmp_file file
Basically, read each line and echo them back with the leading title. Then redirect to a temp file. In case the command succeeds, then move the temp file into the original one.
With awk
:
awk '{print "I have a", $0}' file > tmp_file && mv tmp_file file
With sed
: check anubhava's answer for the best way. This can be another.
sed -i.bak -n 's/^/I have a /p' file
Note it is good practice to use -i.bak
(or -i.whatever
) to generate a backup file with the .bak
extension.
Just for fun, you can also use paste
:
$ paste -d" " <(printf "%0.sI have a\n" $(seq $(wc -l <file))) file
I have a dog
I have a cat
Explanation