sed command to add special character (')

I have a file (input) which contains are below.

Member Analytics Engine
Enterprise Manager
Dev Tutorial

I want to change contains as below by using sed command

'Member Analytics Engine';
'Enterprise Manager';
'Dev Tutorial';

First, I tried to add (') on every first line by using sed comand but nothing seems to be working . commands which I used are below

sed -e 's/^/'/g' input > output
sed 's/^/\'//g' input > output
sed 's/^/[']/g' input > output

Please suggest me what I am missing.

Hello anshu ranjan,

Could you please try following.

sed "s/^/'/g;s/$/';/g"  Input_file

Output will be as follows.

'Member Analytics Engine';
'Enterprise Manager';
'Dev Tutorial';
 

Thanks,
R. Singh

2 Likes

Thanks a lot Rabinder ...
it worked perfectly ...

Why the g option?

sed "s/^/'/;s/$/';/"  file

should do...

1 Like

The usual trick for having a quote within a string in quotes is
quote(end) escaped-quote quote(start)

sed 's/^/'\''//' input

awk

awk '{ print "\x27" $0 "\x27" ";"}'  file

---------- Post updated at 12:42 PM ---------- Previous update was at 12:35 PM ----------

Hi MIG,
I am getting below error while running the code.

sed "s/.*/'&';/" file
1 Like
sed "s/^/\'/ ; s/$/\';/" Input_file.txt

HTH

Sorry, the last / was too many. Also my example was only showing a part of the solution.
The full story is either

sed 's/^/'\''/; s/$/'\'';/' file

or

sed 's/.*/'\''&'\'';/' file