Skip to main content

Stack Overflow

  1. About
  2. Products
  3. OverflowAI

  4. Log in
  5. Sign up

    1. Home
    2. Questions
    3. Tags

    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives

    10. Communities for your favourite technologies. Explore all Collectives
  6. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Explore Teams Create a free Team

Add a prefix string to beginning of each line

Ask Question

Asked 14 years, 5 months ago

Modified 4 days ago

Viewed 547k times

461

I have a file as below:

line1
line2
line3

And I want to get:

prefixline1
prefixline2
prefixline3

I could write a Ruby script, but it is better if I do not need to.

prefix will contain /. It is a path, /opt/workdir/ for example.

Share

Improve this question

edited Jun 28, 2018 at 14:33

](https://stackoverflow.com/users/3266847/benjamin-w)

Benjamin W.

50k

1919 gold badges

122122 silver badges

124124 bronze badges

asked Jan 20, 2010 at 6:36

](https://stackoverflow.com/users/115722/pierrotlefou)

pierrotlefou

40.4k

3939 gold badges

138138 silver badges

175175 bronze badges

Add a comment

19 Answers

Sorted by:

710

# If you want to edit the file in-place
sed -i -e 's/^/prefix/' file

# If you want to create a new file
sed -e 's/^/prefix/' file > file.new

If prefix contains /, you can use any other character not in prefix, or escape the /, so the sed command becomes

's#^#/opt/workdir#'
# or
's/^/\/opt\/workdir/'

Share

Improve this answer

edited Oct 25, 2018 at 22:35

PhysicalChemist's user avatar

PhysicalChemist

550

55 silver badges

1414 bronze badges

answered Jan 20, 2010 at 6:38

](https://stackoverflow.com/users/226621/alok-singhal)

Alok Singhal

95.2k

2121 gold badges

127127 silver badges

158158 bronze badges

Show 16 more comments

159

awk '$0="prefix"$0' file > new_file

In awk the default action is '{print $0}' (i.e. print the whole line), so the above is equivalent to:

awk '{print "prefix"$0}' file > new_file

With Perl (in place replacement):

perl -pi 's/^/prefix/' file

Share

Improve this answer

edited Dec 21, 2023 at 17:55

](https://stackoverflow.com/users/7123660/benjamin-loison)

Benjamin Loison

5,462

44 gold badges

1818 silver badges

3737 bronze badges

answered Jan 20, 2010 at 6:41

](https://stackoverflow.com/users/134713/vijay)

Vijay

66.7k

9090 gold badges

234234 silver badges

325325 bronze badges

Add a comment

34

You can use Vim in Ex mode:

ex -sc '%s/^/prefix/|x' file
  1. % select all lines

  2. s replace

  3. x save and close

Share

Improve this answer

edited Nov 5, 2016 at 14:30

answered Sep 30, 2012 at 16:18

](https://stackoverflow.com/users/1002260/zombo)

Zombo

1

Add a comment

31

If your prefix is a bit complicated, just put it in a variable:

prefix=path/to/file/

Then, you pass that variable and let awk deal with it:

awk -v prefix="$prefix" '{print prefix $0}' input_file.txt

Share

Improve this answer

edited Dec 21, 2023 at 17:55

](https://stackoverflow.com/users/7123660/benjamin-loison)

Benjamin Loison

5,462

44 gold badges

1818 silver badges

3737 bronze badges

answered Feb 17, 2015 at 21:35

](https://stackoverflow.com/users/2030962/melka)

Melka

521

55 silver badges

1212 bronze badges

Add a comment

21

Here is a oneliner solution using the ts command from moreutils

$ cat file | ts prefix

And how it’s derived step by step:

# Step 1. create the file

$ cat file
line1
line2
line3
# Step 2. add prefix to the beginning of each line

$ cat file | ts prefix
prefix line1
prefix line2
prefix line3

Note that the prefix will be space separated from the content

Share

Improve this answer

edited Jan 23 at 0:12

](https://stackoverflow.com/users/6320039/ulysse-bn)

Ulysse BN

11k

77 gold badges

6060 silver badges

9090 bronze badges

answered Apr 19, 2019 at 9:41

](https://stackoverflow.com/users/4602592/btwiuse)

btwiuse

2,871

11 gold badge

2727 silver badges

3333 bronze badges

  • 6

    ‘ts’ is not installed by default on many Linux distros. Also, downvoting because the trailing “tr -d ‘ ‘” in this answer will remove all spaces from the lines, not just the space that was added by ‘ts’

    – Tim Bird

    Commented May 13, 2020 at 17:21

Add a comment

13

If you have Perl:

perl -pe 's/^/PREFIX/' input.file

Share

Improve this answer

edited Dec 21, 2023 at 17:55

](https://stackoverflow.com/users/7123660/benjamin-loison)

Benjamin Loison

5,462

44 gold badges

1818 silver badges

3737 bronze badges

answered Dec 11, 2013 at 20:08

](https://stackoverflow.com/users/654269/majid-azimi)

Majid Azimi

5,725

1313 gold badges

6565 silver badges

117117 bronze badges

Add a comment

8

Using & (the whole part of the input that was matched by the pattern”):

cat in.txt | sed -e "s/.*/prefix&/" > out.txt

OR using back references:

cat in.txt | sed -e "s/\(.*\)/prefix\1/" > out.txt

Share

Improve this answer

edited Dec 21, 2023 at 17:55

](https://stackoverflow.com/users/7123660/benjamin-loison)

Benjamin Loison

5,462

44 gold badges

1818 silver badges

3737 bronze badges

answered Apr 5, 2020 at 1:15

](https://stackoverflow.com/users/13160840/ark25)

Ark25

109

11 silver badge

33 bronze badges

Add a comment

6

Using the shell:

#!/bin/bash
prefix="something"
file="file"
while read -r line
do
 echo "${prefix}$line"
done <$file > newfile
mv newfile $file

Share

Improve this answer

edited Dec 21, 2023 at 17:56

](https://stackoverflow.com/users/7123660/benjamin-loison)

Benjamin Loison

5,462

44 gold badges

1818 silver badges

3737 bronze badges

answered Jan 20, 2010 at 7:05

](https://stackoverflow.com/users/131527/ghostdog74)

ghostdog74

337k

5858 gold badges

260260 silver badges

348348 bronze badges

Add a comment

5

While I don’t think pierr had this concern, I needed a solution that would not delay output from the live “tail” of a file, since I wanted to monitor several alert logs simultaneously, prefixing each line with the name of its respective log.

Unfortunately, sed, cut, etc. introduced too much buffering and kept me from seeing the most current lines. Steven Penny’s suggestion to use the -s option of nl was intriguing, and testing proved that it did not introduce the unwanted buffering that concerned me.

There were a couple of problems with using nl, though, related to the desire to strip out the unwanted line numbers (even if you don’t care about the aesthetics of it, there may be cases where using the extra columns would be undesirable). First, using “cut” to strip out the numbers re-introduces the buffering problem, so it wrecks the solution. Second, using “-w1” doesn’t help, since this does NOT restrict the line number to a single column - it just gets wider as more digits are needed.

It isn’t pretty if you want to capture this elsewhere, but since that’s exactly what I didn’t need to do (everything was being written to log files already, I just wanted to watch several at once in real time), the best way to lose the line numbers and have only my prefix was to start the -s string with a carriage return (CR or ^M or Ctrl-M). So for example:

#!/bin/ksh

# Monitor the widget, framas, and dweezil
# log files until the operator hits <enter>
# to end monitoring.

PGRP=$$

for LOGFILE in widget framas dweezil
do
(
    tail -f $LOGFILE 2>&1 |
    nl -s"^M${LOGFILE}>  "
) &
sleep 1
done

read KILLEM

kill -- -${PGRP}

Share

Improve this answer

edited Dec 21, 2023 at 17:56

](https://stackoverflow.com/users/7123660/benjamin-loison)

Benjamin Loison

5,462

44 gold badges

1818 silver badges

3737 bronze badges

answered Mar 7, 2014 at 21:44

](https://stackoverflow.com/users/3394513/scriptguy)

ScriptGuy

51

11 silver badge

11 bronze badge

Add a comment

4

Using ed:

ed infile <<'EOE'
,s/^/prefix/
wq
EOE

This substitutes, for each line (,), the beginning of the line (^) with prefix. wq saves and exits.

If the replacement string contains a slash, we can use a different delimiter for s instead:

ed infile <<'EOE'
,s#^#/opt/workdir/#
wq
EOE

I’ve quoted the here-doc delimiter EOE (“end of ed”) to prevent parameter expansion. In this example, it would work unquoted as well, but it’s good practice to prevent surprises if you ever have a $ in your ed script.

Share

Improve this answer

answered Jun 28, 2018 at 14:32

](https://stackoverflow.com/users/3266847/benjamin-w)

Benjamin W.

50k

1919 gold badges

122122 silver badges

124124 bronze badges

Add a comment

3

Here’s a wrapped up example using the sed approach from this answer:

$ cat /path/to/some/file | prefix_lines "WOW: "

WOW: some text
WOW: another line
WOW: more text

prefix_lines

function show_help()
{
  IT=$(CAT <<EOF
    Usage: PREFIX {FILE}

    e.g.

    cat /path/to/file | prefix_lines "WOW: "

      WOW: some text
      WOW: another line
      WOW: more text
  )
  echo "$IT"
  exit
}

# Require a prefix
if [ -z "$1" ]
then
  show_help
fi

# Check if input is from stdin or a file
FILE=$2
if [ -z "$2" ]
then
  # If no stdin exists
  if [ -t 0 ]; then
    show_help
  fi
  FILE=/dev/stdin
fi

# Now prefix the output
PREFIX=$1
sed -e "s/^/$PREFIX/" $FILE

Share

Improve this answer

edited Dec 21, 2023 at 17:57

](https://stackoverflow.com/users/7123660/benjamin-loison)

Benjamin Loison

5,462

44 gold badges

1818 silver badges

3737 bronze badges

answered Mar 16, 2016 at 13:24

](https://stackoverflow.com/users/26510/brad-parks)

Brad Parks

69.9k

6666 gold badges

277277 silver badges

360360 bronze badges

Add a comment

3

  1. You can also achieve this using the backreference technique

    sed -i.bak 's/\(.*\)/prefix\1/' foo.txt
    
  2. You can also use with awk like this

    awk '{print "prefix"$0}' foo.txt > tmp && mv tmp foo.txt
    

Share

Improve this answer

edited Dec 21, 2023 at 17:58

](https://stackoverflow.com/users/7123660/benjamin-loison)

Benjamin Loison

5,462

44 gold badges

1818 silver badges

3737 bronze badges

answered Apr 11, 2020 at 8:44

](https://stackoverflow.com/users/6341379/k-vishwanath)

k_vishwanath

1,536

33 gold badges

2121 silver badges

3232 bronze badges

Add a comment

2

Using Pythonise (pz):

pz '"preix"+s' <filename

Share

Improve this answer

answered Mar 22, 2021 at 9:56

](https://stackoverflow.com/users/2556118/hans-ginzel)

Hans Ginzel

8,581

44 gold badges

2525 silver badges

2222 bronze badges

Add a comment

2

You can do it using AWK

echo example| awk '{print "prefix"$0}'

or

awk '{print "prefix"$0}' file.txt > output.txt

For suffix: awk '{print $0"suffix"}'

For prefix and suffix: awk '{print "prefix"$0"suffix"}'

Share

Improve this answer

edited Dec 21, 2023 at 17:59

](https://stackoverflow.com/users/7123660/benjamin-loison)

Benjamin Loison

5,462

44 gold badges

1818 silver badges

3737 bronze badges

answered Jan 11, 2022 at 8:57

](https://stackoverflow.com/users/16510338/rauf)

Rauf

117

11 silver badge

66 bronze badges

Add a comment

1

Simple solution using a for loop on the command line with bash:

for i in $(cat yourfile.txt); do echo "prefix$i"; done

Save the output to a file:

for i in $(cat yourfile.txt); do echo "prefix$i"; done > yourfilewithprefixes.txt

Share

Improve this answer

edited Dec 21, 2023 at 17:58

](https://stackoverflow.com/users/7123660/benjamin-loison)

Benjamin Loison

5,462

44 gold badges

1818 silver badges

3737 bronze badges

answered Apr 18, 2021 at 15:19

](https://stackoverflow.com/users/6352735/michael)

Michael

829

11 gold badge

1010 silver badges

2424 bronze badges

Add a comment

1

If you need to prepend a text at the beginning of each line that has a certain string, try following. In the following example, I am adding # at the beginning of each line that has the word “rock” in it.

sed -i -e 's/^.*rock.*/#&/' file_name

Share

Improve this answer

edited Dec 21, 2023 at 17:59

](https://stackoverflow.com/users/7123660/benjamin-loison)

Benjamin Loison

5,462

44 gold badges

1818 silver badges

3737 bronze badges

answered Dec 10, 2020 at 19:31

](https://stackoverflow.com/users/1392873/mandroid)

mandroid

189

22 silver badges

55 bronze badges

Add a comment

0

For people on BSD/OSX systems there’s utility called lam, short for laminate. lam -s prefix file will do what you want. I use it in pipelines, eg:

find -type f -exec lam -s "{}: " "{}" \; | fzf

…which will find all files, exec lam on each of them, giving each file a prefix of its own filename. (And pump the output to fzf for searching.)

Share

Improve this answer

edited Dec 15, 2019 at 18:15

answered Apr 19, 2019 at 1:36

](https://stackoverflow.com/users/5682988/ray)

Ray

11

33 bronze badges

  • You’re right, it appears this is a BSD only command. POSIX replaced it with paste, but paste doesn’t have the feature of adding a full separator string. I’ll update my answer.

    – Ray

    Commented Dec 15, 2019 at 18:14

Add a comment

0

Another alternative (sponge is optional):

$ cat file | xargs -I{} echo 'prefix-{}-suffix' | sponge file

Example:

$ seq 1 10 | xargs -I{} echo 'prefix-{}-suffix'
prefix-1-suffix
prefix-2-suffix
prefix-3-suffix
prefix-4-suffix
prefix-5-suffix
prefix-6-suffix
prefix-7-suffix
prefix-8-suffix
prefix-9-suffix
prefix-10-suffix

Share

Improve this answer

answered Jul 5 at 10:20

](https://stackoverflow.com/users/1054458/fark)

FarK

606

11 gold badge

55 silver badges

1717 bronze badges

Add a comment

-1

SETLOCAL ENABLEDELAYEDEXPANSION

YourPrefix=blabla

YourPath=C:\path

for /f "tokens=*" %%a in (!YourPath!\longfile.csv)     do (echo !YourPrefix!%%a) >> !YourPath!\Archive\output.csv

Share

Improve this answer

edited Dec 21, 2023 at 18:00

](https://stackoverflow.com/users/7123660/benjamin-loison)

Benjamin Loison

5,462

44 gold badges

1818 silver badges

3737 bronze badges

answered Nov 10, 2020 at 15:15

](https://stackoverflow.com/users/6719006/pietervnk)

pietervnk

107

44 bronze badges

  • 1

    Please add some explanation to your answer such that others can learn from it

    – Nico Haase

    Commented Nov 10, 2020 at 16:15

  • 1

    Please don’t post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.

    – Yagiz Degirmenci

    Commented Nov 10, 2020 at 17:03

  • If you were looking for an answer in a knowledgebase, trying to solve an issue, do you think this answer would be helpful? Not so much.

    – Trenton McKinney

    Commented Nov 10, 2020 at 23:33

  • Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?

    – Jeremy Caney

    Commented Dec 22, 2023 at 0:08

Add a comment

Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

##

Not the answer you’re looking for? Browse other questions tagged

or ask your own question.

Linked

0Add a sentence in each line of a file with sed

0how to add text (shell var.) in the first place in each line using sed

0Print string variable that stores the output of a command in Bash

-1How can I concatenate pipeline string into file

-1Adding a command at the beginning of every line using bash command

-1Add string at the beginning of each line with the name of the file

-2How can I add text to a text file using Bash or Python

205Rename Files and Directories (Add Prefix)

193How to insert strings containing slashes with sed?

45Why does “1” in awk print the current line?

See more linked questions

22Add suffix to each line with shell script

16Add prefix to every line in text in bash

0How to add a prefix for each line according to a line field

3Add prefix to each line of output with sed

2Add prefix to every line shell

0bash script to add prefix to line.

1Add input lines subset to prefix shell script output

1Add a prefix to beginning of each line depending on its size

1Linux Bash: add prefix to lines if match the condition

0Add prefix to a line from the line before it in shell

Hot Network Questions

Question feed

Stack Overflow
Products
Company
Stack Exchange Network

Site design / logo © 2024 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2024.7.9.12232

Updated: