How to concatenate each newline string in single or double quotes


Momordica charantia

How do I concatenate each newline string together in single or double quotes, separated by commas.

example:

My name is as follows.

$ cat file

James kurt
Suji sane
Bhujji La
Loki Hapa

Expected:

"James kurt", "Suji sane", "Bhujji La", "Loki Hapa"

edit:

My side efforts:

I've done it below this, but I'm doing it in two steps, and jst is curious if it can only be combined into one.

 $ awk '{print "\x22" $1" "$2 "\x22"}'| tr '\n' ','
KamilCuk

First "use to print all lines, then join the lines with commas:

< file xargs -d '\n' printf '"%s"\n' | paste -sd,

In addition to newlines, you can remove trailing (or leading commas):

< file xargs -d '\n' printf '"%s",' | sed 's/,$//'
< file xargs -d '\n' printf ',"%s"' | cut -c2-
< file xargs -d '\n' printf ', "%s"' | cut -c3-   # with space after comma

Using sed, add "and preserve lines, then replace newlines with commas on the last line, and remove leading commands and print:

sed -n 's/^/"/;s/$/"/;H;${x;s/\n/, /g;s/^, //;p}' file

you are near! After " "you try to add space between lines ". You can:

awk '{print "\x22" $0 "\x22"}' | tr '\n' ',' |
# and then remove trailing comma:
sed 's/,$//'

However, it's just much simpler to concatenate the lines with paste, then replace the newlines with commas and remove the last one:

awk '{print "\x22" $0 "\x22"}' | paste -sd,

Related


Replace single and double quotes in a string

sansmerci05: I'm trying to learn python and I'm having a problem with strings that need to replace single and double quotes. My goal is to split them into a list, but keep single and double quotes for other reasons. I tried the following but got the error s=("

Replace single quotes with double quotes inside a string

username My code replaces all object keys with double quotes but not single quotes var str = "db.projects.insert({'projectid':'1001','projteamname':'DBTeam','amount':100})" var objKeysRegex = /({|,)(?:\s*)(?:')?([A-Za-z_$\.][A-Za-z0-9_ \-\.$]*)(?:')?(?:\s*):/g

How to add single quotes to each word in a string

AbhinavVaidya8 I have several parameters in my variable. I want to replace each variable with single quotes, separated by commas. var_list=emp location branch. I want my output like: var_list='emp', 'location', 'branch' Codeforester Using Bash parameter ex

How to format python string with double and single quotes

Tiny I need to create a python string like: 'A' "B" I think it should be produced reliably using: my_string = "'A' \"B\"" What I get here is: '\'A\' "B"' what we want. help! (Why... InfluxDB uses both single and double quotes in queries) Alexis Your string

Replace single and double quotes in a string

Geancarlo Murillo For a given string, I'm trying to replace single quotes (') and double quotes (") with \' and \" respectively to render it in a Django view. I am trying lote.descripcion.replace("'", '\'') lote.lote.replace("'", "\'") But it

How to remove single or double quotes from a string?

alibag90 I am new to nodejs. I have a code like this now. var array1 = ["a","b"]; array1.forEach(function(element) { console.log(element); }); This answers the following response: "a" "b" But I need to remove double quotes like this: a b who can help me?

How to pass a string containing single and double quotes?

green saber I have a string variable called name. nameContains strings 14'6". I am trying to pass nameto a function. I am trying to add one to the table row onclicklike this : $("#trID").attr('onclick', 'testFunction(' + name + ')'); I tried using escape char

Convert string double quotes to single quotes

Suyatanar I got a password from the database, which is stored in a variable $passwordwith "" (double quotes) . I want to change the variable value $passwordwith " (single quote) . How can I change it? When I test with a static value, $password = '$2y$10$wFgLki

Convert string double quotes to single quotes

Suyatanar I got a password from the database, which is stored in a variable $passwordwith "" (double quotes) . I want to change the variable value $passwordwith " (single quote) . How can I change it? When I test with a static value, $password = '$2y$10$wFgLki

How to remove single or double quotes from a string?

alibag90 I am new to nodejs. I have a code like this now. var array1 = ["a","b"]; array1.forEach(function(element) { console.log(element); }); This answers the following response: "a" "b" But I need to remove double quotes like this: a b who can help me?

Replace single and double quotes in a string

Geancarlo Murillo For a given string, I'm trying to replace single quotes (') and double quotes (") with \' and \" respectively to render it in a Django view. I am trying lote.descripcion.replace("'", '\'') lote.lote.replace("'", "\'") But it

Powershell replace double quotes with single quotes in string

Sasham I just did it wrong The below statement throws an exception and I can't get the correct format $appendedQry = $appendedQry -replace "\"","'" What is the correct syntax? Sasham it should be $appendedQry = $appendedQry -replace '"',''''

How to search for a string with double single quotes (SQL)

fc123 I have a varcharfield LOCATIONCITYwith a value St. John''s I want to search like below select * from city where locationcity='St. John''s' Since this is the default behavior of sql server, convert double single quotes to single double qoite how do i se

Replace single quotes with double quotes inside a string

username My code replaces all object keys with double quotes but not single quotes var str = "db.projects.insert({'projectid':'1001','projteamname':'DBTeam','amount':100})" var objKeysRegex = /({|,)(?:\s*)(?:')?([A-Za-z_$\.][A-Za-z0-9_ \-\.$]*)(?:')?(?:\s*):/g

How to search for a string with double single quotes (SQL)

fc123 I have a varcharfield LOCATIONCITYwith a value St. John''s I want to search like below select * from city where locationcity='St. John''s' Since this is the default behavior of sql server, convert double single quotes to single double qoite How do I se

grep for single and double quotes in a string

Anya I'm trying to write a grep to find the .epl filename or filepath in a given file. I tried the following regex Part 1: Results obtained with grep -rEco "/(.+).epl|(.+).epl" ./index.epl used by REGEX url => 'xyz.epl url => 'xyz.epl url => 'xyz.epl url => "p

How to concatenate each newline string in single or double quotes

Momordica charantia How do I concatenate each newline string together in single or double quotes, separated by commas. example: My name is as follows. $ cat file James kurt Suji sane Bhujji La Loki Hapa Expected: "James kurt", "Suji sane", "Bhujji La", "Loki

Replace single and double quotes in a string

sansmerci05: I'm trying to learn python and I'm having a problem with strings that need to replace single and double quotes. My goal is to split them into a list, but keep single and double quotes for other reasons. I tried the following but got the error s=("

Replace single and double quotes in a string

Geancarlo Murillo For a given string, I'm trying to replace single quotes (') and double quotes (") with \' and \" respectively to render it in a Django view. I am trying lote.descripcion.replace("'", '\'') lote.lote.replace("'", "\'") But it

Replace single and double quotes in a string

Geancarlo Murillo For a given string, I'm trying to replace single quotes (') and double quotes (") with \' and \" respectively to render it in a Django view. I am trying lote.descripcion.replace("'", '\'') lote.lote.replace("'", "\'") But it

How to pass a string containing single and double quotes?

green saber I have a string variable called name. nameContains strings 14'6". I am trying to pass nameto a function. I am trying to add one to the table row onclicklike this : $("#trID").attr('onclick', 'testFunction(' + name + ')'); I tried using escape char

How to add single quotes to each word in a string

AbhinavVaidya8 I have several parameters in my variable. I want to replace each variable with single quotes, separated by commas. var_list=emp location branch. I want my output like: var_list='emp', 'location', 'branch' Codeforester Using Bash parameter ex

Replace single and double quotes in a string

Geancarlo Murillo For a given string, I'm trying to replace single quotes (') and double quotes (") with \' and \" respectively to render it in a Django view. I am trying lote.descripcion.replace("'", '\'') lote.lote.replace("'", "\'") But it

Replace single and double quotes in a string

Geancarlo Murillo For a given string, I'm trying to replace single quotes (') and double quotes (") with \' and \" respectively to render it in a Django view. I am trying lote.descripcion.replace("'", '\'') lote.lote.replace("'", "\'") But it

Replace single and double quotes in a string

Geancarlo Murillo For a given string, I'm trying to replace single quotes (') and double quotes (") with \' and \" respectively to render it in a Django view. I am trying lote.descripcion.replace("'", '\'') lote.lote.replace("'", "\'") But it

How to search for a string with double single quotes (SQL)

fc123 I have a varcharfield LOCATIONCITYwith a value St. John''s I want to search like below select * from city where locationcity='St. John''s' Since this is the default behavior of sql server, convert double single quotes to single double qoite How do I se

How to search for a string with double single quotes (SQL)

fc123 I have a varcharfield LOCATIONCITYwith a value St. John''s I want to search like below select * from city where locationcity='St. John''s' Since this is the default behavior of sql server, convert double single quotes to single double qoite How do I se

grep for single and double quotes in a string

Anya I'm trying to write a grep to find the .epl filename or filepath in a given file. I tried the following regex Part 1: Results obtained with grep -rEco "/(.+).epl|(.+).epl" ./index.epl used by REGEX url => 'xyz.epl url => 'xyz.epl url => 'xyz.epl url => "p