shell script - to append single quotes and comma


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script - to append single quotes and comma
# 1  
Old 09-08-2011
Java shell script - to append single quotes and comma

file1
----

Code:
34556745
32678343
31576776
31455566
21356666

I want to assign the record values to a variable in the below format, so that I can use output in .sql file for querying in database.

Code:
('34556745', '32678343', '31576776', '31455566', '21356666')

-----------
below is the script which I used.

Code:
var='(\''
for record in `cat file1`
do
var=${var}"\,'"${record}
done 
var=${var}'\)'
echo $var > output

---------

Code:
$ cat output 
(3455674532678343315767763145556621356666)

SmilieSmilie

Last edited by radoulov; 09-08-2011 at 03:36 PM.. Reason: Code tags!
# 2  
Old 09-08-2011
Code:
% cat infile 
34556745
32678343
31576776
31455566
21356666
% printf '(%s)\n' $(printf "'%s'\n" $(<infile) | paste -sd,)
('34556745','32678343','31576776','31455566','21356666')

This User Gave Thanks to radoulov For This Post:
# 3  
Old 09-08-2011
Question

gets the below message Smilie
-------------------------
Code:
Usage: paste [-s] [-d List] File...


Last edited by radoulov; 09-08-2011 at 04:28 PM.. Reason: Code tags!
# 4  
Old 09-08-2011
Try this:

Code:
printf '(%s)\n' $(printf "'%s'\n" $(<infile) | paste -s -d,)

# 5  
Old 09-08-2011
Question

still no success..same message

Code:
$ printf '(%s)\n' $(printf "'%s'\n" $(<infile) | paste -s -d,)
 
Usage: paste [-s] [-d List] File...
()


Last edited by radoulov; 09-08-2011 at 04:39 PM.. Reason: Code tags!
# 6  
Old 09-08-2011
Which operating system are you using?
# 7  
Old 09-08-2011
This should work on most systems:

Code:
perl -nle'push @_, $_;
  print "(", (join ",", map "\47$_\47", @_,), ")" 
    if eof
  ' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace newline with comma and append quotes

Hi, I have below requirement. Apple Orange Banana Required O/p in bash 'Apple,Orange,Banana' Can you please help. Please wrap your samples, codes in CODE TAGS as per forum rules. (3 Replies)
Discussion started by: Rtk
3 Replies

2. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

3. Shell Programming and Scripting

Need shell script to append double quotes for each column in a file

Hi Experts, I am beginner to the shell scripting, My requirement is to append double quotes for each column in a file if double quotes does not exist. Example: "abc"|123|"gh-ch"|23.067 Use code tags, thanks. (10 Replies)
Discussion started by: spidy
10 Replies

4. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

5. Shell Programming and Scripting

Bash shell adding extra single quotes

AIX 6.1 bash shell #!/bin/bash -x STATEMENT="cvs commit -m \"This is\" ../PBP/EIR.ENTRY" echo $STATEMENT exit 0 This is the output + STATEMENT='cvs commit -m "This is" ../PBP/EIR.ENTRY' + echo cvs commit -m '"This' 'is"' ../PBP/EIR.ENTRY cvs commit -m "This is" ../PBP/EIR.ENTRY + exit... (26 Replies)
Discussion started by: hpodhrad
26 Replies

6. Shell Programming and Scripting

Having a terrible problem with quotes/single quotes!

Hello. I'm trying to write a bash script that uses GNU screen and have hit a brick wall that has cost me many hours... (I'm sure it has something to do with quoting/globbing, which is why I post it here) I can make a script that does the following just fine: test.sh: #!/bin/bash # make... (2 Replies)
Discussion started by: jondecker76
2 Replies

7. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

8. Shell Programming and Scripting

Script to append a value seperated by comma

I have a file like below. How can I add a new value after moss separated by a comma. This adding script should work in such a way that each time i pass a value it should be added one after the other separated by commas. hru:122: hello:123:john,philip,mary,kp,moss hi:124: bye:125: Can... (7 Replies)
Discussion started by: Tuxidow
7 Replies

9. Shell Programming and Scripting

How can i use single quotes for SQL command in shell script

Hi. please help me to write the following query in a shell script. the Query is :select no,salary from emp_info where name='$var_name' the following is my code. #! /bin/sh var_name=$1 sqlplus -s user/pwd@DB << EOF select no,salary from emp_info where name="'$var_name'";... (4 Replies)
Discussion started by: little_wonder
4 Replies

10. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies
Login or Register to Ask a Question