How to add a prefix string at the beginning of each line in Bash shell script on Linux?

How to add a prefix string at the beginning of each line in Bash shell script on Linux?

For example, assume we have a file a.txt:

line 1
line 2

I want to have,

pre line 1
pre line 2

You may use sed, the stream editor for filtering and transforming text:

sed -e 's/^/pre /'

For example,

$ echo "line 1
line 2" | sed -e 's/^/pre /'
pre line 1
pre line 2

You may also do prefix adding to lines of certain patterns. But this is out of this question.

Answered by Eric Z Ma.

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *