How to put =" " in the field?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to put =" " in the field?
# 1  
Old 12-04-2013
How to put =" " in the field?

Hi All,

Seeking for your assistance on how to put =" " in the field.

Ex.
Input File:
Code:
this is a test1,this is test2,this is test3

Expected output

Code:
="this is a test1",="this is test2",="this is test3"

Please advise,

Thanks,
-nik
# 2  
Old 12-04-2013
Code:
echo "this is a test1,this is test2,this is test3" | sed 's/[^,]*/="&"/g'

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 12-04-2013
one way of doing this is as below on sun

Code:
echo "this is a test1,this is test2,this is test3"|sed -e 's/,/",="/g' -e 's/^/="/g' -e 's/$/"/g'

# 4  
Old 12-04-2013
I suppose the real problem is the double quotes: they will get "eaten away" when you display the string using shell means:

Naive try, won't work:
Code:
# var="="this is a test1",="
# echo $var
=this is a test1,=

If you want the double quotes to show up, even when you display the variables content using echo or similar commands you have to escape them (prepend a backslash):

Code:
# var="=\"this is a test1\",=\""
# echo $var
="this is a test1",="

Note, that this protection works only once and the shell interpretes a line every time it uses it. If you want the protection to last through two interpretations you have to use several backslashes and - this is even more complicated - escape the first backslash itself by prepending it with another backslash. See the following for the effects of interpreting a line once, then twice, then thrice:

Code:
bakunin@ksh88 # var="\\\"\\\""
bakunin@ksh88 # echo $var
\"\"
bakunin@ksh88 # eval echo $var
""
bakunin@ksh88 # eval eval echo $var
 
bakunin@ksh88 #

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

SFTP batch not renaming file with "put"

I have a .ksh script that creates an sftp batch file and runs it through sftp. It works except for one thing. If I try to "put" to a different name, it doesn't use the specified remote name...it still "puts" the original local name. I've tried both of these, and neither work...it will always... (4 Replies)
Discussion started by: dbiggied
4 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. UNIX for Dummies Questions & Answers

How to put "= " in the field?

Hi All, Seeking for your assistance on how to put =" " in the field. Ex. Input File: this is a test1,this is test2,this is test3 Expected output ="this is a test1",="this is test2",="this is test3" Please advise, Thanks, -nik (1 Reply)
Discussion started by: nikki1200
1 Replies

5. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

6. UNIX for Advanced & Expert Users

Should I say "field 8" or "column 8" in this case?

I saw some recent posts where I thought the terms "field" and "column" were being misused. I work with data a lot, and have my opinions. I'm wondering if those opinions are correct. ***** Rows seem clear - I don't think there is any controversy about what a row is, either for database or text... (10 Replies)
Discussion started by: hanson44
10 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question