Linux Add A Prefix String To Beginning Of Each L
- About
- Products
- Log in
-
- Home
- Questions
- Users
- Companies
- Labs
- Jobs
- Discussions
-
Collectives
- Communities for your favourite technologies. Explore all Collectives
-
Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Add a prefix string to beginning of each line
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.
](https://stackoverflow.com/users/3266847/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)
40.4k
3939 gold badges
138138 silver badges
175175 bronze badges
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/'
550
55 silver badges
1414 bronze badges
answered Jan 20, 2010 at 6:38
](https://stackoverflow.com/users/226621/alok-singhal)
95.2k
2121 gold badges
127127 silver badges
158158 bronze badges
-
1
@benjamin, I had already upvoted your answer, however, I prefer
sedfor lightweight tasks such as this. If “prefix” is known, it’s very easy to pick a character not from “prefix”. -
7
Don’t forget you can also use
sedin a pipeline, e.g.foo | sed -e 's/^/x /' | bar.– Mattie
-
1
@Dataman cool. Another way would be
sed -e '2,$s/^/prefix/'. -
1
@BinChen escape the
/like\/(in single-quoted strings) or\\/(in double-quoted strings)– user6516765
-
11
Use
sed -e 's/$/postfix/' fileif you want to add string to the end of each line.– Brian
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
](https://stackoverflow.com/users/7123660/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)
66.7k
9090 gold badges
234234 silver badges
325325 bronze badges
-
9
With a pipe/stream or variable:
prtinf "$VARIABLE\n" | awk '$0="prefix"$0' -
5
With a large file (12 G),
awkreportsawk: out of memory in readrec 1 source line number 1, but the solution withsedcompletes successfully.– jrm
-
1
This is the best answer, with AWK it Worked right off the bat without having to annoyingly deal with escaping regex special characters
-
With “normal files” split into lines awk shouldn’t run out of memory…
34
You can use Vim in Ex mode:
ex -sc '%s/^/prefix/|x' file
-
%select all lines -
sreplace -
xsave and close
answered Sep 30, 2012 at 16:18
](https://stackoverflow.com/users/1002260/zombo)
1
-
1
For me, I just open the file in vim and type
:%s/^/prefix/, since this strategy ends up being useful in many situations
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
](https://stackoverflow.com/users/7123660/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)
521
55 silver badges
1212 bronze badges
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
](https://stackoverflow.com/users/6320039/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)
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
13
If you have Perl:
perl -pe 's/^/PREFIX/' input.file
](https://stackoverflow.com/users/7123660/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)
5,725
1313 gold badges
6565 silver badges
117117 bronze badges
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
](https://stackoverflow.com/users/7123660/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)
109
11 silver badge
33 bronze badges
6
Using the shell:
#!/bin/bash
prefix="something"
file="file"
while read -r line
do
echo "${prefix}$line"
done <$file > newfile
mv newfile $file
](https://stackoverflow.com/users/7123660/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)
337k
5858 gold badges
260260 silver badges
348348 bronze badges
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}
](https://stackoverflow.com/users/7123660/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)
51
11 silver badge
11 bronze badge
-
3
use the
-uoption to sed to avoid the buffering. -
2
Buffering can be turned off with unbuffer/stdbuf, see unix.stackexchange.com/q/25372/6205
– myroslav
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.
answered Jun 28, 2018 at 14:32
](https://stackoverflow.com/users/3266847/benjamin-w)
50k
1919 gold badges
122122 silver badges
124124 bronze badges
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
](https://stackoverflow.com/users/7123660/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)
69.9k
6666 gold badges
277277 silver badges
360360 bronze badges
-
2
This will not work if
PREFIXcontains any characters special to sed like a slash.– josch
-
Good point… If you find you use slash alot, you could use a different delimiter with the sed part, as detailed here, which would allow you to use it in searches. Other special sed chars can be put in by escaping with a slash, e.g
prefix_lines \*
3
-
You can also achieve this using the backreference technique
sed -i.bak 's/\(.*\)/prefix\1/' foo.txt -
You can also use with awk like this
awk '{print "prefix"$0}' foo.txt > tmp && mv tmp foo.txt
](https://stackoverflow.com/users/7123660/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)
1,536
33 gold badges
2121 silver badges
3232 bronze badges
2
Using Pythonise (pz):
pz '"preix"+s' <filename
answered Mar 22, 2021 at 9:56
](https://stackoverflow.com/users/2556118/hans-ginzel)
8,581
44 gold badges
2525 silver badges
2222 bronze badges
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"}'
](https://stackoverflow.com/users/7123660/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)
117
11 silver badge
66 bronze badges
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
](https://stackoverflow.com/users/7123660/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)
829
11 gold badge
1010 silver badges
2424 bronze badges
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
](https://stackoverflow.com/users/7123660/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)
189
22 silver badges
55 bronze badges
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.)
answered Apr 19, 2019 at 1:36
](https://stackoverflow.com/users/5682988/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
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
answered Jul 5 at 10:20
](https://stackoverflow.com/users/1054458/fark)
606
11 gold badge
55 silver badges
1717 bronze badges
-1
SETLOCAL ENABLEDELAYEDEXPANSION
YourPrefix=blabla
YourPath=C:\path
for /f "tokens=*" %%a in (!YourPath!\longfile.csv) do (echo !YourPrefix!%%a) >> !YourPath!\Archive\output.csv
](https://stackoverflow.com/users/7123660/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)
107
44 bronze badges
-
1
Please add some explanation to your answer such that others can learn from it
-
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.
-
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.
-
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?
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
- Featured on Meta
-
We spent a sprint addressing your requests — here’s how it went
-
Upcoming initiatives on Stack Overflow and across the Stack Exchange network…
- What makes a homepage useful for logged-in users
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?
Related
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
- Can I change a 12 speed eagle cassete to a 9 speed CUES cassete?
- Real-life problems involving solving triangles
- What scientifically plausible apocalypse scenario, if any, meets my criteria?
- Can non-admins create new domain on local DNS from a client computer?
- ArXiv submission on hold for 11 days. Should I inquire via moderation support or wait longer?
- Keyboard Ping Pong
- Did any other European leader praise China for its peace initiatives since the outbreak of the Ukraine war?
- How to turn a sum into an integral?
- Are you radical enough to solve this SURDOKU?
- The centre of gravity of a triangle on a parabola lies on the axis of symmetry
- Why is this transformer placed on rails?
- Is prescreening not detrimental for paid surveys?
- Why does Macbeth well deserve his name?
- Go the Distance
- Using grout that had hardened in the bag
- Is this a Hadamard matrix?
- Alternatives to iterrow loops in python pandas dataframes
- Car stalls when coming to a stop except when in neutral
- Efficient proof of Bessel’s correction
- Schreier-Sims algorithm for solving Rubik’s cube
- Does accreditation matter in Computer Science (UK)?
- Could a Black Market exist in a cashless society (digital currency)?
- When, if ever, is bribery legal?
- Coping with consequences of a dog bite before buying a puppy
Stack Overflow
Products
Company
Stack Exchange Network
- Technology
- Culture & recreation
- Life & arts
- Science
- Professional
- Business
- API
- Blog
Site design / logo © 2024 Stack Exchange Inc; user contributions licensed under CC BY-SA. rev 2024.7.9.12232