SED/AWK – Add to the End or Beginning of a Line

From time to time it is required to modify some file very fast.

And it goes without saying that the most popular command line tools for this in Linux are sed and awk – the two best text processing programs.

In the following article, you’ll find an information about how to add some text, character or comma to the beginning or to the end of every line in a file using sed and awk.

Also you will learn how to turn multiple lines of a file into one comma-separated line.

SED/AWK – Add to the Beginning

Use the below commands to append some PREFIX (some text or character) to the beginning of every line in a FILE:

$ awk '{print "PREFIX"$0}' FILE

– or –

$ sed 's/^/PREFIX/' FILE

Cool Tip: Do not be a bore! Add COLORS to your Bash script! Make it look AWESOME! Read more →

SED/AWK – Add to the End

Use the following commands to append some SUFFIX (some text or character) to the end of every line in a FILE:

$ awk '{print $0"SUFFIX"}' FILE

– or –

sed 's/$/SUFFIX/' FILE

SED/AWK – Add to the Beginning and End

Use the following commands to append some PREFIX to the beginning and some SUFFIX to the end of every line in a FILE:

$ awk '{print "PREFIX"$0"SUFFIX"}' FILE

– or –

$ sed "s/.*/PREFIX&SUFFIX/" FILE

Cool Tip: You can also easily remove characters from the beginning or from the end of a line using cut command! Read more →

Multiple Lines into One Comma-Separated

Let’s say you have some file with multiple lines.

And you need to turn these multiple lines into one comma-separated line.

There are many ways how it can be achieved, but i often use one of the following one-liners.

Use one of these commands to append commas to the end of every line in a file, except the last one, and turn the multiple lines into one comma-separated line:

$ echo $(awk 'NR > 1{print line", "}{line=$0;}END{print $0" "}' FILE)

– or –

$ paste -d, -s FILE

Example:

$ cat file.txt
I Love Bash
I Love Bash
I Love Bash

$ echo $(awk 'NR > 1{print line", "}{line=$0;}END{print $0" "}' file.txt)
I Love Bash, I Love Bash, I Love Bash

$ paste -d, -s file.txt
I Love Bash,I Love Bash,I Love Bash
Was it useful? Share this post with the world!

13 Replies to “SED/AWK – Add to the End or Beginning of a Line”

  1. вау круто

  2. You forgot -i in your sed command.

  3. So it doesn’t actually save this in the file for me…?
    sudo awk ‘{print “/library/music/”$0}’ go.m3u
    responds with:
    /library/music/(( Singles ))/The departed Soundtrack- Dropkick Murphys – I’m Shipping Up To Boston.mp3
    but cat go.m3u comes back with
    (( Singles ))/The departed Soundtrack- Dropkick Murphys – I’m Shipping Up To Boston.mp3

  4. Nothing like this goes without saying or is taken for granted or even known at all by people who haven’t learned it yet, which is most people.

  5. Thank you for all the info. However, I need to add (‘ preffix and ‘) as suffix. I guess you can understand the problem with it. Since bash command has ‘ involved, it is not happening. Any hints?

  6. @Rahul, had the same issue. For sed, if you add the flag -i it should solve the issue.

    1. Nicholas Salsbury says: Reply

      Sometimes sed requires experimentation. not using the -i flag is good to make sure the output is what you want it to be, before ruining your source file on the first go.

  7. I need to add a ” (double quote) at the beginning and end of each line in the file.
    When I do the following
    sed -i ‘s/.*/”/’ $OUTDIR/$OUTFILE it removes everything on the line and I just get a double quote on each line (just one and nothing else).

    Any suggestions?

    1. Lock at the right synthax:
      sed “s/.*/PREFIX&SUFFIX/” FILE
      You forgot “&SUFFIX”
      This should work:
      sed -i ‘s/.*/\”&\”/’ $OUTDIR/$OUTFILE
      At all:
      Special characters like \ or ” must be escaped with a backslash:
      \\ or \”

  8. in place of prefix i need to pass a variable which has data assigned to it/ how i can achieve that

  9. Hello,
    Thanks a lot for the great article! 🙂
    Could you please explain the last command ?
    echo $(awk ‘NR > 1{print line”, “}{line=$0;}END{print $0” “}’ FILE)
    I don’t understand the usage of NR > 1 and {line=$0;} part.
    If I remove line and do echo $(awk ‘NR > 1{print $0”, “}END{print $0” “}’ FILE) I don’t get the first line (which is logical since NR > 1) and I get the last line printed twice.
    If I do echo $(awk ‘NR > 0{print $0”, “}END{print $0” “}’ FILE) I get the first line.
    So my question is how {line=$0;} removes double last line, and why it works with NR > 1 (logically it would be NR > 0)
    Thanks in advance ! 🙂

  10. hello
    How can I insert “1000/1000” to the end of line 2?
    for example:
    line 1: Anything
    line 2: Region=
    answer
    line 1: Anything
    line 2: Region= 1000/1000

  11. sed -e 2’s/$/:SUFIXX&/’ fichero

Leave a Reply