4mahmoud
4mahmoud

Reputation: 853

Unix command line: how to add double quotes to a word?

i have a text file "names.txt" that contains a list of words like so :

Adidas
Android
Bluetooth
Minaret
Mushroom
acorn
airplane
amazon

i want this file to be exactly like :

"Adidas" = "";
"Android" = "";
"Bluetooth" = "";
"Minaret" = "";
"Mushroom" = "";
"acorn" = "";
"airplane" = "";
"amazon" = "";

Any idea how to accomplish this ? Thank you

Upvotes: 0

Views: 4993

Answers (4)

Kent
Kent

Reputation: 195209

awk -v ORS='" = "";\n' '$1="\""$1'

Upvotes: 1

user3442743
user3442743

Reputation:

Awk way

awk '$0="\""$0"\" = \"\";"' file

Sed way

sed 's/.*/"&" = "";/' file

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263547

Use this sed command:

sed 's/.*/"&" = "";/'

To modify the file in place, you can do this:

sed 's/.*/"&" = "";/' filename > tmp && mv tmp filename

(pick a temporary file name that doesn't already exist; I often use $$, the current shell's process ID) or:

sed -i.bak 's/.*/"&" = "";/' filename

The latter copies the original file to filename.bak.

Upvotes: 2

fedorqui
fedorqui

Reputation: 290155

What about this sed?

$ sed -e 's/^/"/' -e 's/$/" = "";/' file
"Adidas" = "";
"Android" = "";
"Bluetooth" = "";
"Minaret" = "";
"Mushroom" = "";
"acorn" = "";
"airplane" = "";
"amazon" = "";

It replaces the beginning of the line (^) with " and the end of line ($) with " = "";. So it eventually adds " at the beginning and " = ""; at the end.

Use sed -i.bak '...' to in place edit.


You can also use bash for this:

while read line
do
    printf "%s%s%s\n" '"' $line '" = "";'
done < file

Or awk:

awk '{printf "%s%s%s\n", "\"", $0, "\" = \"\";"}' file

Upvotes: 3

Related Questions