Add a Prefix String to Beginning of Each Line

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



Leave a reply



Submit