# Adding words to beginning of lines

**URL:** <https://community.unix.com/t/adding-words-to-beginning-of-lines/191535>\
**Category:** Shell Programming and Scripting\
**Created:** [April 3, 2008, 11:52am UTC](https://community.unix.com/t/adding-words-to-beginning-of-lines/191535 "2008-04-03T11:52:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ernst](https://community.unix.com/letter_avatar/ernst/32/5_5575768a8748004e209b776fc1b2916d.png) [@Ernst](https://community.unix.com/u/Ernst)\
**Post date:** [April 3, 2008, 11:52am UTC](https://community.unix.com/t/adding-words-to-beginning-of-lines/191535/1 "2008-04-03T11:52:35Z")

</div>

I have a file that contains a great number of lines, let's say 183 lines, and I want to add: echo " to the beginning of each line. What is the easiest way to do it?

Tx

---

<div class="post-metadata">

**Author:** ![rvegmond](https://community.unix.com/letter_avatar/rvegmond/32/5_5575768a8748004e209b776fc1b2916d.png) [@rvegmond](https://community.unix.com/u/rvegmond)\
**Post date:** [April 3, 2008, 11:55am UTC](https://community.unix.com/t/adding-words-to-beginning-of-lines/191535/2 "2008-04-03T11:55:26Z")

</div>

I would use sed for that purpose, remember to escape the "

---

<div class="post-metadata">

**Author:** ![Tytalus](https://community.unix.com/user_avatar/community.unix.com/tytalus/32/87_2.png) [@Tytalus](https://community.unix.com/u/Tytalus)\
**Post date:** [April 3, 2008, 11:55am UTC](https://community.unix.com/t/adding-words-to-beginning-of-lines/191535/3 "2008-04-03T11:55:43Z")

</div>

```nohighlight
 awk '{print "echo \""$0}' infile

```

or

```nohighlight
 sed 's/^/echo "/' infile

```

---

<div class="post-metadata">

**Author:** ![ShawnMilo](https://community.unix.com/user_avatar/community.unix.com/shawnmilo/32/3304_2.png) [@ShawnMilo](https://community.unix.com/u/ShawnMilo)\
**Post date:** [April 3, 2008, 11:56am UTC](https://community.unix.com/t/adding-words-to-beginning-of-lines/191535/4 "2008-04-03T11:56:31Z")

</div>

> [@ernst](#):
>
> I have a file that contains a great number of lines, let's say 183 lines, and I want to add: echo " to the beginning of each line. What is the easiest way to do it?
> 
> Tx

```plaintext
cat filename.txt | perl -ne 'print "echo " . $_' > output.txt

```

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [April 3, 2008, 12:12pm UTC](https://community.unix.com/t/adding-words-to-beginning-of-lines/191535/5 "2008-04-03T12:12:34Z")

</div>

Another one:

```nohighlight
sed 's/.*/echo "&"/' file

```

Regards

---

<div class="post-metadata">

**Author:** ![Ernst](https://community.unix.com/letter_avatar/ernst/32/5_5575768a8748004e209b776fc1b2916d.png) [@Ernst](https://community.unix.com/u/Ernst)\
**Post date:** [April 3, 2008, 1:56pm UTC](https://community.unix.com/t/adding-words-to-beginning-of-lines/191535/6 "2008-04-03T13:56:19Z")

</div>

Thanks!

This command works: sed 's/.\*/echo "&"/'

What if I want at the same time to add something to the end of each line.  
i.e echo "unixtest=test" \>\> firsttest

what would the command be to add the \>\> firsttest to teh end of each line

---

<div class="post-metadata">

**Author:** ![shamrock](https://community.unix.com/letter_avatar/shamrock/32/5_5575768a8748004e209b776fc1b2916d.png) [@shamrock](https://community.unix.com/u/shamrock)\
**Post date:** [April 3, 2008, 2:13pm UTC](https://community.unix.com/t/adding-words-to-beginning-of-lines/191535/7 "2008-04-03T14:13:49Z")

</div>

```nohighlight
sed 's/.*$/& >> firsttest/' file

```

---

<div class="post-metadata">

**Author:** ![ShawnMilo](https://community.unix.com/user_avatar/community.unix.com/shawnmilo/32/3304_2.png) [@ShawnMilo](https://community.unix.com/u/ShawnMilo)\
**Post date:** [April 3, 2008, 2:14pm UTC](https://community.unix.com/t/adding-words-to-beginning-of-lines/191535/8 "2008-04-03T14:14:35Z")

</div>

> [@ernst](#):
>
> Thanks!
> 
> This command works: sed 's/.\*/echo "&"/'
> 
> What if I want at the same time to add something to the end of each line.  
> i.e echo "unixtest=test" \>\> firsttest
> 
> what would the command be to add the \>\> firsttest to teh end of each line

What have you tried? Considering you have the basic layout there, you might want to try modifying it slightly and seeing if it works. That will earn you a lot more respect. Perhaps more importantly, you could actually learn something which will help you in the future.

ShawnMilo

---

<div class="post-metadata">

**Author:** ![shamrock](https://community.unix.com/letter_avatar/shamrock/32/5_5575768a8748004e209b776fc1b2916d.png) [@shamrock](https://community.unix.com/u/shamrock)\
**Post date:** [April 3, 2008, 2:18pm UTC](https://community.unix.com/t/adding-words-to-beginning-of-lines/191535/9 "2008-04-03T14:18:38Z")

</div>

...combine both seds into one single command.

```nohighlight
sed 's/^.*$/echo "&" >> firsttest/' file

```

---

<div class="post-metadata">

**Author:** ![Ernst](https://community.unix.com/letter_avatar/ernst/32/5_5575768a8748004e209b776fc1b2916d.png) [@Ernst](https://community.unix.com/u/Ernst)\
**Post date:** [April 3, 2008, 2:23pm UTC](https://community.unix.com/t/adding-words-to-beginning-of-lines/191535/10 "2008-04-03T14:23:47Z")

</div>

Great!!

Thanks!
