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 expansion

delimited="'${var_list//[[:space:]]/"','"}'"

If there are multiple spaces in the string, use extended globbing:

shopt -s extglob
delimited="'${var_list//+([[:space:]])/"','"}'"

use an array

words=($var_list)                           # create array from string, using word splitting
printf -v delimited ",'%s'" "${words[@]}"   # yields ",'one','two',..."
delimited=${delimited:1}                    # remove the leading ','

use loop

delim=''
for word in $var_list; do                   # rely on word splitting by shell
  delimited="$delimited$delim'$word'"
  delim=", "
done

related:

Related


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

How to add quotes around each word in a string in R?

Thomas Navarro I have a string: words<-"Monday, Tuesday, Wednesday, Thursday,Friday" And I just need to put quotes around each word: "Monday", "Tuesday", "Wednesday", "Thursday","Friday" Get the length of five strings. I know there are many articles on this

How to add quotes around each word in a string in R?

Thomas Navarro I have a string: words<-"Monday, Tuesday, Wednesday, Thursday,Friday" And I just need to put quotes around each word: "Monday", "Tuesday", "Wednesday", "Thursday","Friday" Get the length of five strings. I know there are many articles on this

Add double quotes at the first word of each line

Pushpa I have an R file with results like this: filename totalvar result runtime file1 100 0 20.45 file2 400 4 4.50 ... filen 200 1 2.00 Some filenames contain strange characters so I have to put quotes in them. What's the easiest way to put quotes aroun

How to add a character after each word in a string?

TreyZept So what I have is the string(str) I get from fgets(str, x, stdin);. If I write for example "Hello World", I want to be able to add a character to the front of each word in the string. Get this "Hello? World?" for example. I think I'm getting more diff

Add quotes to each word in the file

jitter I have some comma separated words in a file like this: variable1, variable2, variable3, variable4 What's the easiest way to add quotes to each word using BASH? The final result should look like this: "variable1", "variable2", "variable3", "variable4"

I want to add single quotes and commas to each word in javascript

VyTcdc I want to add single quotes and commas to each word in javascript, example below. If my input value is 0000 1111 2222 3333 4444 I need the above in this schema. '0000','1111','2222','3333','4444' I tried several examples but failed. My code is as fol

How to add quotes around each word in a string in R?

Thomas Navarro I have a string: words<-"Monday, Tuesday, Wednesday, Thursday,Friday" And I just need to put quotes around each word: "Monday", "Tuesday", "Wednesday", "Thursday","Friday" Get the length of five strings. I know there are many articles on this

How to add a character after each word in a string?

TreyZept So what I have is the string(str) I get from fgets(str, x, stdin);. If I write for example "Hello World", I want to be able to add a character to the front of each word in the string. Get this "Hello? World?" for example. I think I'm getting more diff

Add double quotes at the first word of each line

Pushpa I have an R file with results like this: filename totalvar result runtime file1 100 0 20.45 file2 400 4 4.50 ... filen 200 1 2.00 Some filenames contain strange characters so I have to put quotes in them. What's the easiest way to put quotes aroun

put single quotes around each word

User 123 I have a newline separated list of words like: apple ball cat dog I want to separate each word with a comma and then put single quotes around each word. I can remove the newline and put a comma, but I can't put single quotes around every word. I woul

Add quotes and commas to each word in the file

missionary I have a list of words like: string1 string2 string3 .... string12312 How do I convert these words into a way that I can use the output as a JS array, i.e. "String1", "String2"..., "String12312" - in other words, how do I add quotes and commas? I k

How to add single quotes in string php?

Moses I have got$a = "6,9,13,1" I want to change it to$a = "'6','9','13','1'" Is there any solution to my problem? Ahmed If it $a's a string and you want to change the number this way, you can use the following code: $a = preg_replace("/\d+/", "'$0'", $a);

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

How to add a character after each word in a string?

TreyZept So what I have is the string(str) I get from fgets(str, x, stdin);. If I write for example "Hello World", I want to be able to add a character to the front of each word in the string. Get this "Hello? World?" for example. I think I'm getting more diff

Add double quotes at the first word of each line

Pushpa I have an R file with results like this: filename totalvar result runtime file1 100 0 20.45 file2 400 4 4.50 ... filen 200 1 2.00 Some filenames contain strange characters so I have to put quotes in them. What's the easiest way to put quotes aroun

Add single quotes in string builder

Mukul Khatter I want to add single quotes to string value received from object sb.Append("<input type='button' id='buyNow_"+i+"' class='GetQuote' onclick='return getProposerSection('"+ objResponse.objQuoteDetails[i].PlanName+ "')'/>" Fabio Milheiro The easiest

How to add double quotes to the first word of each line via sed?

tester I have a file with similar content, but I want it to be valid JSON I need to add each word of a line in double quotes. I tried the existing answers on StackOverflow and none of them worked. {container:"proxy", endpoint:"proxy", exception:"ApiException

How to add quotes around each word in a string in R?

Thomas Navarro I have a string: words<-"Monday, Tuesday, Wednesday, Thursday,Friday" And I just need to put quotes around each word: "Monday", "Tuesday", "Wednesday", "Thursday","Friday" Get the length of five strings. I know there are many articles on this

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 add a character after each word in a string?

TreyZept So what I have is the string(str) I get from fgets(str, x, stdin);. If I write for example "Hello World", I want to be able to add a character to the front of each word in the string. Get this "Hello? World?" for example. I think I'm getting more diff

Add double quotes at the first word of each line

Pushpa I have an R file with results like this: filename totalvar result runtime file1 100 0 20.45 file2 400 4 4.50 ... filen 200 1 2.00 Some filenames contain strange characters so I have to put quotes in them. What's the easiest way to put quotes aroun

put single quotes around each word

User 123 I have a newline separated list of words like: apple ball cat dog I want to separate each word with a comma and then put single quotes around each word. I can remove the newline and put a comma, but I can't put single quotes around every word. I woul