Adding one string at the beginning of each line in a file

Hi,

I have file a.txt as below. I want to add one string root beginning of each line.

Sample file a.txt
aaa
bbb
ccc

Sample output
Root aaa
Root bbb
Root ccc
Can any one help me on this?

for i in $(cat a.txt); do echo "Root $i"; done

actually, probably better with awk

awk '{print "Root " $0}' a.txt

Or with sed

sed 's/^/Root /g' a.txt

BTW, I'm not showing off, I'm trying to improve my sed and awk skills and am using your question to try them out :slight_smile:

MYVAR="Root "
awk -v MYVAR=$MYVAR '{print MYVAR, $0}' a.txt >b.txt

Really It's helpful. Actually I was expecting the solutions with the use sed or awk. Thanks a lot.....

One question:
If I use sed from the command line. It will serve the purpose. But It wont save it in the file. It will just display it on the screen.
Then I tried to redirect the output to the same file. In this example, below is the command I did. And this command made file empty.

sed 's/^/Root /g' a.txt > a.txt

Why is this SED not redirecting to the same file?

sed -i 's/^/Root /g' a.txt

-i option will do inplace modification.

Thanks Sank.
Thanks is what I want.