What is the easiest way to add a string on the beginning of every line of the file from the command line?

I am looking for a way to add some string to the beginning of every line (same string for every line).
Not something customizable but rather something that will be easy to remember and available on every POSIX-compliant platform (and every shell as well).

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

:|paste -d'foo ' - - - - input > output

(just kidding, though you’ll probably find it’s the fastest of all the solutions posted here :-b).

The canonical way is:

sed 's/^/foo /' < input > output

However, it’s not easily adapted to arbitrary strings. For instance,

sed "s/^/$var /"

Only works if $var doesn’t contain, &, , / nor newline characters.

In that regard,

export var
awk '{print ENVIRON["var"], $0}'

or

perl -pe '$_="$ENV{var} $_"'

would work better.

Method 2

You can use sed:

sed -i 's/^/your_string /' your_file

Thanks to Stephane and Marco’s comments, note that the -i option isn’t POSIX. A POSIX way to do the above would be

sed 's/^/your_string /' your_file > tmp_copy && mv tmp_copy your_file

or perl:

perl -pi -e 's/^/your_string /' your_file

Explanation

Both commands perform a regex substitution, replacing the beginning of a line (^) with your desired string. The -i switch in both commands makes sure the file is edited in place (i.e. the changes are reflected in the file instead of printed to stdout).

sed should be available on any POSIX-compliant OS and perl should be available on most modern Unices except perhaps for the ones that have gone through the effort of removing it.

Method 3

I present a solution using awk prepending the string “foo”.

awk '{ print "foo", $0; }' input > output

awk is cross-platform and available on any POSIX system. It does not do in-place editing. If you want to edit a file without creating a second one, you will have to use a temporary file. See Joseph’s sed answer, it shows the syntax. Another hack is to use the following syntax, which is basically creating a temporary file with the same file name as the original file.

{ rm file; awk '{ print "foo", $0 }' > file; } < file

Method 4

You can avoid the problems of in-place editing with the stream tools by using a tool that normally does in-place editing – an editor!

ex sample.txt -c "%s/^/foo /" -c wq

There is an additional advantage that the commands are easy and obvious to anyone who is versed in the one true editor.

Method 5

You can use perl to do this:

$ perl -pi -e 's/^/mystring /' afile.txt

Example

Create a sample file.

$ seq 5 > afile.txt

$ cat afile.txt
1
2
3
4
5

Run the above command:

$ perl -pi -e 's/^/mystring /' afile.txt

$ cat afile.txt
mystring 1
mystring 2
mystring 3
mystring 4
mystring 5

Method 6

Simply using Bash

 while IFS= read -r line; do echo "foo" "${line}" ; done  < input > Output

Using Python

python -c "import sys; print 'foo '.join([ l for l in sys.stdin.readlines() ])" < input > Output

If you want to edit in-place

import fileinput
import sys

for line in fileinput.input(['inputfile'], inplace=True):
    sys.stdout.write('foo {l}'.format(l=line))

Reference link

Method 7

Or you can do the following:

vi filename.txt

Esc (to make sure you’re in NORMAL mode), and then enter the following command:

:1,$ s/^/mystring


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x