Vicky
Vicky

Reputation: 1338

Quoting lines of a file in Linux

I am trying to enclose each line of a file in single quotes and append , at the end of each line.

I have tried,

sed 's/^*$/\'&\',/g' filename 

I have also tried,

sed 's/^/'/;s/$/',/'  filename 

both don't seem to help.Please advise.

Upvotes: 2

Views: 57

Answers (3)

James Brown
James Brown

Reputation: 37454

Another in awk:

$ awk 'gsub(/^|$/,"'"'"'")' file
'line one'
'line two'
'line three'

ie. the ' is double-quoted " ' " and that is single-quoted ' " ' " ' and again double-quoted in the gsub " ' " ' " ' ". No, I'm not kidding.

Upvotes: 0

Cyrus
Cyrus

Reputation: 88809

I suggest:

sed "s/.*/'&',/" file

Upvotes: 4

P....
P....

Reputation: 18411

awk -v q="'" '{$0= q $0 q ","}1' input
'line one',
'line two',
'line three',

Using awk: 1. Enclosing the whole line between single quotes. 2. Adding comma at the end of each line.

Upvotes: 3

Related Questions