Ryan Cauldwell
Ryan Cauldwell

Reputation: 61

How to add line to beginning of sed output

So the problem I'm having is that I'm using sed to pull out a bunch of objects from a string in a bash script, but I need to add the same string to the beginning of the sed output. An example:

Data:

|field1|field2|select * from object1, object2|

sed output:

object1|
object2|

What I've tried:

Sticking the whole line into a variable called LINE, then:

sed "s/^.*/$LINE&/g"

This resulted in an error that says:

unknown option to s

echo $LINE
|field1|field2|select * from object1, object2|

So there's nothing wrong with the variable.

This is the desired output:

|field1|field2|select * from object1, object2|object1|

|field1|field2|select * from object1, object2|object2|

Any help is sincerely appreciated :)

-Ryan

Upvotes: 1

Views: 184

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74695

Some of the characters in your string are being interpreted by sed. To avoid this problem, you should use a string-based method, rather than a regex-based one. For example, using awk:

awk -v line="$line" '{ print line $0 }' file

This adds the string in the shell variable $line to the start of each line in file.

Upvotes: 3

Related Questions