put a string to the beginnig of every line...

put a string to the beginnig of every line...

Post by Massimo Dalla Giustin » Wed, 26 Jun 2002 06:34:17



Hi,

I want to create a shell script to put the same string
at the beginnig of every line in a text file.

i.e.

$ cat file.txt
cat
dog
frog
cow
....

I want to create the shell put_string.sh with file.txt and the string "This
is a " as input.
Then it creates file2.txt such as:
$ cat  file2.txt
This is a cat
This is a dog
This is a frog
This is a cow
....

I really don't know what kind of commands I must to use...

Thanks for your help.

Max

 
 
 

put a string to the beginnig of every line...

Post by Andreas K?h? » Wed, 26 Jun 2002 06:46:24


Submitted by "Massimo Dalla Giustina" to comp.unix.shell:

Quote:> Hi,

> I want to create a shell script to put the same string
> at the beginnig of every line in a text file.

> i.e.

> $ cat file.txt
> cat
> dog
> frog
> cow
> ....

> I want to create the shell put_string.sh with file.txt and the string "This
> is a " as input.
> Then it creates file2.txt such as:
> $ cat  file2.txt
> This is a cat
> This is a dog
> This is a frog
> This is a cow
> ....

> I really don't know what kind of commands I must to use...

> Thanks for your help.

> Max

sed 's/^/This is a /' < file.txt > file2.txt

This means "replace the beginning of each line with 'This is a '".

--
Andreas K?h?ri
--------------------------------------------------------------
Stable, secure, clean, free:  www.netbsd.org

 
 
 

put a string to the beginnig of every line...

Post by Barry Margoli » Wed, 26 Jun 2002 06:45:26




Quote:>I want to create the shell put_string.sh with file.txt and the string "This
>is a " as input.
>Then it creates file2.txt such as:

sed "s/^/$1/" file.txt > file2.txt

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

put a string to the beginnig of every line...

Post by John W. Krah » Wed, 26 Jun 2002 06:56:24



> I want to create a shell script to put the same string
> at the beginnig of every line in a text file.

> i.e.

> $ cat file.txt
> cat
> dog
> frog
> cow
> ....

> I want to create the shell put_string.sh with file.txt and the string "This
> is a " as input.
> Then it creates file2.txt such as:
> $ cat  file2.txt
> This is a cat
> This is a dog
> This is a frog
> This is a cow

sed 's/^/This is a /' file.txt > file2.txt

John
--
use Perl;
program
fulfillment

 
 
 

put a string to the beginnig of every line...

Post by Andreas K?h? » Wed, 26 Jun 2002 07:14:10


Submitted by "LANkrypt0" to comp.unix.shell:

Quote:

> On Jun 24. Massimo Dalla Giustina shared the following message with...:
[cut]
> MDG> Thanks for your help.
> MDG>
> MDG> Max

> I would just use:
> $ sed -e "s/^/This is a /g" file2.txt

The g modifier for sed is unneeded.  There will never be two ^'s
on one line.

--
Andreas K?h?ri
--------------------------------------------------------------
Stable, secure, clean, free:  www.netbsd.org

 
 
 

put a string to the beginnig of every line...

Post by Richard Howlet » Wed, 26 Jun 2002 08:16:21



> Hi,

> I want to create a shell script to put the same string
> at the beginnig of every line in a text file.

> i.e.

> $ cat file.txt
> cat
> dog
> frog
> cow
> ....

> I want to create the shell put_string.sh with file.txt and the string "This
> is a " as input.
> Then it creates file2.txt such as:
> $ cat  file2.txt
> This is a cat
> This is a dog
> This is a frog
> This is a cow
> ....

Everyone else has used sed so here is an awk example:

awk '{print "This is a " $1}' file.txt >file2.txt

Having said this your posting suggests that you want to create a shell
script that you can pass an argument to and this argument will be prepended
to each line in file.txt so here is an awk way to do that. Note the
combination of double and single quotes. Get these wrong and it will not
work. So to prepend "This is a" to each line type:

$ cat put_string.sh
awk '{print "'"$1"' " $1}' file.txt >file2.txt

$ put_string.sh "This is a"
$ cat file2.txt
This is a cat
This is a dog
This is a frog
This is a cow

--
Richard Howlett


 
 
 

put a string to the beginnig of every line...

Post by Chris F.A. Johnso » Thu, 27 Jun 2002 05:54:24



> Hi,

> I want to create a shell script to put the same string
> at the beginnig of every line in a text file.

> i.e.

> $ cat file.txt
> cat
> dog
> frog
> cow
> ....

> I want to create the shell put_string.sh with file.txt and the string "This
> is a " as input.
> Then it creates file2.txt such as:
> $ cat  file2.txt
> This is a cat
> This is a dog
> This is a frog
> This is a cow
> ....

> I really don't know what kind of commands I must to use...

    Other posters have made suggestions using sed and awk; you can
    also do it without any external commands:

        while IFS="" read line
        do
            echo "This is a $line"
        done < file.txt > file2.txt

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

1. Put file lines in an array: how keep line blanks in the string?

Hi,
The pb is that if file 'myFile.txt' contains:

If I do "set -A fileArray `cat myFile.txt`", I'll get:
with the following commands:

But of course I'd like to retrieve EACH line for a single
call to the "echo" command corresponding to a given line
number (minus one)...

Do you know how I could store each string in each cell
of my array (for ex. "here's line 1" in $fileArray[0]) ?

Thanks a lot !

Regards,
Seb

Sent via Deja.com http://www.deja.com/
Before you buy.

2. Printing from Netscape - Jibberish!

3. How to put a string in the command line ?

4. named/sendmail

5. Insert a character in the beginnig and at the end of each line.

6. how to trap interrupt signals !

7. Find a string, delete that line, delete 1 line before it and all lines after it...

8. What the linvocates need to grok

9. how to find a unknown string and then the next line with the string

10. Insert a line of text every nth line

11. inserting a blank line after every line - using sed

12. adding new line to external text file below a line that contains <string>

13. How to display the line with a string AND the following line of text?