script needed to add word to each line

March 1, 2011 at 12:43:00
Specs: Windows XP
I need a script that will add the same word to the beginning of each line in a file

Any ideas?

See More: script needed to add word to each line

Report •

March 1, 2011 at 12:44:13
Acually not to add to just a single file but multiple files

Report •

March 1, 2011 at 12:45:57
Use sed where ^ is a regular expression, signifying the beginning of the line:


# untested, no spaces in filenames
for i in file1 file2 file3
   sed 's/^/same word/' $i > newfile.txt
   mv newfile.txt $i

Report •

March 1, 2011 at 12:57:03
thanks that worked but how do I run it against multiple files and keep the same filenames with the change..maybe overwrite the original file

Report •

Related Solutions

March 1, 2011 at 13:10:24
What does 'mulitiple files' mean? The script does, as presented, processes file1, file2, file3. As the loop processes, each file should be renamed by newfile.txt to the original file name, $i, using the move command, mv.

If you are using Linux (i.e. GNU) sed, it has an -i option which edits files in place:

sed -i 's/^/same word/' $i

meaning you don't need the temp file, newfile.txt, and you don't need the mv command.

Report •

March 1, 2011 at 13:19:37
If you have a pattern for your mulitiple files, you can use wildcards. This processes all files with a txt extension:

for i in *.txt
   echo "$i"

Report •

March 1, 2011 at 13:30:42
what I mean by multiple files is that I have many csv files in a directory. when I run the script against all files I get
Can't open file1
Can't open file2
Can't open file3

Report •

March 1, 2011 at 13:42:45
When I ran the script it showed running through each file and adding the word but it did not keep the changes in the existing files

Report •

March 1, 2011 at 14:28:16
I thought you would realize that file1, file2, and file3 are example files. Evidently, I was wrong. Please post your code.

Report •

March 2, 2011 at 04:32:13
for i in *.csv


sed 's/^/Blade8,/' $i

echo "$i"


Report •

March 2, 2011 at 07:32:30
Did I not show you in my first reply how to redirect the output to a file, and then, using that file, rename the original? Look at the move command in my first reply:

mv newfile.txt $i

Report •

March 2, 2011 at 09:21:06
Thank-you very great..I appreciate all your help

Report •

Ask Question