Insert Inverted Commas Around Numeric Values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert Inverted Commas Around Numeric Values
# 1  
Old 12-13-2011
Insert Inverted Commas Around Numeric Values

Hi,

I am trying to insert Inverted Commas around all the numeric values within a comma seperated string / variable.
Code:
1111,2222,3333,4444

I would like it to be:
Code:
'1111','2222','3333','4444'

Note - This string could have a differing amount of numeric values each time the variable is created.

e.g.
Code:
Instance 1 - 1111,2222,3333,4444
Instance 2 - 2222,3333

Any help would be much appreciated.

Cheers


Moderator's Comments:
Mod Comment Please use code tags!

Last edited by zaxxon; 12-13-2011 at 12:17 PM.. Reason: code tags, see PM
# 2  
Old 12-13-2011
Code:
$> V="'"
$> echo 1111,2222,3333,4444| sed 's/,/'$V','$V'/g; s/^/'$V'/; s/$/'$V'/'
'1111','2222','3333','4444'

# 3  
Old 12-13-2011
Hi RichZR,

Using Perl:
Code:
$ cat script.pl
use warnings;                                                                                                                                                                                                                                
use strict;                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                             
while ( <DATA> ) {                                                                                                                                                                                                                           
        chomp;                                                                                                                                                                                                                               
        s/^|(?=,)|(?<=,)|$/'/g;                                                                                                                                                                                                              
        printf qq[$_\n];                                                                                                                                                                                                                     
}                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                             
__DATA__                                                                                                                                                                                                                                     
1111,2222,3333,4444                                                                                                                                                                                                                          
2222,3333
$ perl script.pl
'1111','2222','3333','4444'                                                                                                                                                                                                                  
'2222','3333'

Regards,
Birei
# 4  
Old 12-13-2011
Hi.
Code:
echo 1111,2222,3333,4444 | sed "s/\([0-9][0-9]*\)/'\1'/g"

producing:
Code:
'1111','2222','3333','4444'

Where ['] is a straight single-quote, an apostrophe, Dec 39:
Code:
Dec Hex    Dec Hex    Dec Hex  Dec Hex  Dec Hex  Dec Hex   Dec Hex   Dec Hex  
  0 00 NUL  16 10 DLE  32 20    48 30 0  64 40 @  80 50 P   96 60 `  112 70 p
  1 01 SOH  17 11 DC1  33 21 !  49 31 1  65 41 A  81 51 Q   97 61 a  113 71 q
  2 02 STX  18 12 DC2  34 22 "  50 32 2  66 42 B  82 52 R   98 62 b  114 72 r
  3 03 ETX  19 13 DC3  35 23 #  51 33 3  67 43 C  83 53 S   99 63 c  115 73 s
  4 04 EOT  20 14 DC4  36 24 $  52 34 4  68 44 D  84 54 T  100 64 d  116 74 t
  5 05 ENQ  21 15 NAK  37 25 %  53 35 5  69 45 E  85 55 U  101 65 e  117 75 u
  6 06 ACK  22 16 SYN  38 26 &  54 36 6  70 46 F  86 56 V  102 66 f  118 76 v
  7 07 BEL  23 17 ETB  39 27 '  55 37 7  71 47 G  87 57 W  103 67 g  119 77 w
  8 08 BS   24 18 CAN  40 28 (  56 38 8  72 48 H  88 58 X  104 68 h  120 78 x
  9 09 HT   25 19 EM   41 29 )  57 39 9  73 49 I  89 59 Y  105 69 i  121 79 y
 10 0A LF   26 1A SUB  42 2A *  58 3A :  74 4A J  90 5A Z  106 6A j  122 7A z
 11 0B VT   27 1B ESC  43 2B +  59 3B ;  75 4B K  91 5B [  107 6B k  123 7B {
 12 0C FF   28 1C FS   44 2C ,  60 3C <  76 4C L  92 5C \  108 6C l  124 7C |
 13 0D CR   29 1D GS   45 2D -  61 3D =  77 4D M  93 5D ]  109 6D m  125 7D }
 14 0E SO   30 1E RS   46 2E .  62 3E >  78 4E N  94 5E ^  110 6E n  126 7E ~
 15 0F SI   31 1F US   47 2F /  63 3F ?  79 4F O  95 5F _  111 6F o  127 7F DEL

The curly version is ['] , pasted from http://en.wikipedia.org/wiki/Quotation_mark

Best wishes ... cheers, drl

Last edited by drl; 12-13-2011 at 02:44 PM..
# 5  
Old 12-13-2011
Code:
 
cat /tmp/xx
 
111,222,3333,8484848,333,1111
111,222,3333,8484848,333,1111

 
cat yy.ksh
 
awk -v QUOTE="'" '
BEGIN{FS=","}
{
  for(i=1;i<=NF;i++)
  {
    if (i == NF)
      printf("%s%s%s\n",QUOTE,$(i),QUOTE);
    else
      printf("%s%s%s,",QUOTE,$(i),QUOTE);
  }
}' /tmp/xx

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 a numeric values in a certain column

Hi All, I am trying to replace a certain value from one place in a file . In the below file at position 35 I will have 8 I need to modify all 8 in that position to 7 I tried awk '{gsub("8","7",$35)}1' infile > outfile ----> not working sed -i 's/8/7'g' infile --- it is replacing all... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. UNIX for Beginners Questions & Answers

Insert commas

Original content: ACACCTCAT 129 | ACACCTCAT 0 ACACCTCATX 171 | ACACCTCATX 0 ACACRESRT 0 ACACRESRT 0 ACACRESRTX 0 ... (4 Replies)
Discussion started by: loktamann
4 Replies

3. UNIX for Dummies Questions & Answers

Insert single quote and commas

Hi All, I have a set of data as below : XS012371378 Raj 23-09-12 SH128238948 Andrew 24-08-12 CH273712399 Walsh 12-10-12 JK7249923893 Nick 10-02-13 JP6383791389 Braslin 30-12-13 I want the first column to be extracted separately. I can get this using awk. awk '{print $1}' file_name ... (3 Replies)
Discussion started by: Nand Kishor
3 Replies

4. Shell Programming and Scripting

Inverted commas replace

Hi, My input file is like this chr1 + "NM_1234" chr1 - "NM_1234" If I want my third column to contain both the first and second column values, how do I do it? I know how to do it by including the column numbers, but I want the values before the inverted commas. So, my output would... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

5. Shell Programming and Scripting

CSV with commas in field values, remove duplicates, cut columns

Hi Description of input file I have: ------------------------- 1) CSV with double quotes for string fields. 2) Some string fields have Comma as part of field value. 3) Have Duplicate lines 4) Have 200 columns/fields 5) File size is more than 10GB Description of output file I need:... (4 Replies)
Discussion started by: krishnix
4 Replies

6. UNIX for Dummies Questions & Answers

Insert Commas at fixed positions

Hi I have a text file which is position delimited, i.e. Code1 Description1 Value1 Code2 Description2 Value2 etc. I want to import this file into MySQL so I want to convert the file into Comma delimited format. To do this I want to insert commas at fixed positions on... (7 Replies)
Discussion started by: arossco
7 Replies

7. Programming

numeric values ending in 'U'

I am getting back on the C++ programming after many years away. I recently received an SDK that has code like this where numeric values end in 'U'. What does this mean? if ((ptr % 16U) == 0U) return buffer; (3 Replies)
Discussion started by: sneakyimp
3 Replies

8. Shell Programming and Scripting

difference between double inverted coma and single inverted comma

Whats the basic difference between double inverted comma and single inverted comma and no comma applied at all? Eg1 if Eg2 if iEg3 f (1 Reply)
Discussion started by: abhisekh_ban
1 Replies

9. UNIX for Dummies Questions & Answers

How do I insert commas/delimiters in File

Hi, Newbie here. Need to convert a txt file to .csv format. There's no character to replace so not sure if I can use sed :confused: . The comma is to be inserted after every certain number of characters in each line... Help! Thanks. (4 Replies)
Discussion started by: mbelen
4 Replies

10. Shell Programming and Scripting

Replace spaces with 0's having numeric values.

What could be the regular expression with gsub function in awk to replace all numerics having spaces before to be replaced with 0s? (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question