SED command for read and write to different files

Hi

I need some help on SED command

I am writing a shell script which does the following:

  1. Read one line at a time from a file abc.txt which has millions of lines

  2. Prefix each line read with some text "

  3. Post fix each line read with a quote "

  4. Write the new modified line to a new file xyz.txt

  5. This should happen for all the lines in abc.txt and the data should be written to xyz.txt linewise

If some body could help, if would just awesome.

Tnx

This smells like a homework question. If it isn't what is the "real world problem" of your question and what have you done to attempt to solve this problem yourself?

I have never used sed before, i tried searching on google but of no avail, i know it has a very simple solution for someone who has already used sed......

could somebody reply with a solution?

I have never used sed before, i tried searching on google but of no avail, i know it has a very simple solution for someone who has already used sed......

could somebody reply with a solution?

You can start here:
Sed - An Introduction and Tutorial

Thanks buddy, I will try to solve it by going thru this link......

i tried..

sed "s/^/prefix/;s/\n/sufix/" oldfile > newfile

but it is only adds the prefix...

sed 's/^/hello gaurav "/g;s/$/"/g' abc.txt > xyz.txt

Try this:

sed 's/.*/Some text "&/' file > newfile

sed 's/^/hello gaurav "/g;s/$/"/g' abc.txt > xyz.txt

The reason is that there is no "\n" at the end of the line as far as sed is concerned. There is a far more easy device to match line ends or line beginnings, which you have already seen used here:

"^" at the beginning of a regex means "line beginning". "x" finds any x, "^x" finds only an "x" on first position of the line.

"$" at the end of a regex means "line end". "x$" would match only a line ending with "x".

There is another metacharacter you have seen used here: "&" in a replacement string this means "everything that has been matched by the regex". Example:

echo "xxxabcxxx" | sed 's/abc/y&y/' ==> will give "xxxyabcyxxx"
echo "xxxabcxxx" | sed 's/ab/y&y/' ==> will give "xxxyabycxxx"

You should now be able to create a correct sed script yourself. In fact you have two ways of doing it shown here.

I hope this helps.

bakunin

thanks everybody for their help

i am tried the following script and it works fine

sed "s/^/prefix/;s/$/suffix/" oldfile > newfile