Add a prefix string to beginning of each line
# 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/'
How to add a string at the beginning of each line in a file
$ awk '{print "03/06/2012|" $0;}' input.txt > output.txt
Takes about 0.8 seconds for a file with 1.3M lines on some average 2010 hardware.
How can I prepend a string to the beginning of each line in a file?
a one-line awk command should do the trick also:
awk '{print "prefix" $0}' file
Add prefix to every line in text in bash
Pure bash:
while read line
do
echo "prefix_$line"
done < a.txt
How to add text at the beginning of specific lines using sed?
Whenever @PL
is found, read next line and prepend PREFIX
to it.
sed '/@PL/{n;s/^/PREFIX/}' file
How to add a prefix to all lines that don't start with one of multiple words
Use alternation with multiple words for negation:
sed -i -E '/^(sleep|for|do)/! s/^/PREFIX_/' file
PREFIX_someText
sleep 1
PREFIX_anotherString
sleep 1
for i in {1..50}
do
PREFIX_command
sleep 1
PREFIX_secondCommand
sleep 1
done
/^(sleep|for|do)/!
will match all lines except those that start with sleep
, or for
or do
words.
How to add prefix for each new line in JavaScript?
Maybe you can use tagged template literals (ES6)...
Verbose version with for...of
function addPrefix(str) { let tmp = str[0].split('\n'), res = []; for (const frag of tmp) { res.push(`> ${frag}`); } return res.join('\n');}
let str = addPrefix`Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pellentesque`;
console.log(str);
Add a prefix string to each of the command line parameters
Use sed command, this should be enough:
params=$(echo " $services" | sed "s/\ / $prefix/g")
then just 'echo' params var
Related Topics
Rename All Files in a Folder With a Prefix in a Single Command
How to Turn Off Echo While Executing a Shell Script Linux
How to Prompt For Yes/No/Cancel Input in a Linux Shell Script
What's the Difference of Section and Segment in Elf File Format
How to Instruct Cron to Execute a Job Every Second Week
Run an Untrusted C Program in a Sandbox in Linux That Prevents It from Opening Files, Forking, etc.
How to Programmatically Disable Hardware Prefetching
Does "Argument List Too Long" Restriction Apply to Shell Builtins
Managing Log Files Created by Cron Jobs
How to Detect Invalid Utf8 Unicode/Binary in a Text File
How to Display Number to Two Decimal Places in Bash Function
How to Compare Two Strings in Dot Separated Version Format in Bash
How to Remove the Lines Which Appear on File B from Another File A
Bash Script Processing Limited Number of Commands in Parallel
What Are the Return Values of System Calls in Assembly
How to Normalize a File Path in Bash
How to Pipe Initial Input into Process Which Will Then Be Interactive