Sed script for appending & prepending

Hello Mates

I am trying to write a script, which appends and prepends the text in a file.

I tried testing with a small file and it worked fine.
but for the large file, the script is wiping the entire file and adds only the word to be appended in the file.

mv $file_name $file_name.bak
sed -e "s/^/$prepend/g" $file_name.bak > $file_name

mv $file_name $file_name.bak1
sed -e "s/.*/&$append/g' $file_name.bak1 > $file_name

i execute my script like ./script_name prepend-word append-word filename

Please suggest, I am not very familiar with scripting.:frowning:

Thanks
Satya

is this ur actual script?
if not please post actual one
if i know i will help u

Your script could be something like this:

prepend="$1"
append="$2"
file_name="$3"
sed 's/^/'"$prepend"'/;s/$/'"$append"'/' $file_name > ${file_name}_new
mv ${file_name}_new $file_name

elixir,
still the same problem... when i run the script with the example u gave, it wipes the entire file and adds only the text to be appended.

the script i was trying to run earlier

prepend=$1
append=$2
file_name=$3

mv $file_name $file_name.bak
sed -e "s/^/$prepend/g" $file_name.bak > $file_name

sleep 2

sed 's/.*/&$append/g' $file_name > newname.txt

exit 0

do you want to append and prepend the same thing to all the lines in a file?

if thats the case we can easily do it with awk.

I want to append "Word1" & Prepend "word2", for all the lines in the file.

actual code:

#!/bin/bash
awk -v app=$1 -v  pre=$2 '{printf("%s %s %s\n",app,$0,pre);}' input_file

pass prepend word as first command line argument
append word as second command line argument