How to insert a single quote to each record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert a single quote to each record
# 8  
Old 09-13-2011
Don't know about vi, but this works in vim:

Code:
:% s/\(.*\)/'\1'/

tyler_durden
# 9  
Old 09-13-2011
Sweet, that works in my version of vi after removing the space between the '%' and the 's':
Code:
:%s/\(.*\)/'\1'/

Could you explain the expression used?
# 10  
Old 09-13-2011
or
Code:
:%s/.*/'&'/

# 11  
Old 09-13-2011
Sweet also! Please can you explain the expression used?

Is it the '.*' matches the whole line in the 'search for' side and in the replace side the '&' matches what was matched in the search side?

I hope that makes sense :-/
# 12  
Old 09-13-2011
Quote:
Originally Posted by gary_w
Sweet, that works in my version of vi after removing the space between the '%' and the 's':
Code:
:%s/\(.*\)/'\1'/

Could you explain the expression used?
Code:
:%s/\(.*\)/'\1'/ =>
%    =  for all lines in the current vim window
s    =  substitute
/    =  start with the search expression hereafter (s/// syntax)
\(   =  escape character + begin parenthesis to remember the result of the regex that follows
.*   =  do a greedy search of zero or more ASCII characters
\)   =  escape character + end parenthesis. Push the result of the regex match into the variable "\1". In this case, the entire line is pushed into "\1"
/    =  end the search expression and start the replace expression hereafter (s/// syntax)
'\1' =  set the replace expression to single-quote + "\1" variable i.e. entire line + single-quote
/    =  end the replace expression (s/// syntax)

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 13  
Old 09-13-2011
Quote:
Originally Posted by gary_w
Sweet also! Please can you explain the expression used?

Is it the '.*' matches the whole line in the 'search for' side and in the replace side the '&' matches what was matched in the search side?

I hope that makes sense :-/
exactly!
This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Single quote _error_.

Hi all... (This is Don's domain.) I have come across an anomaly in sh and dash compared to bash. It involves echoing a character set to a file in sh and dash compared to bash. It is probably easier to show the code and results first. #!/usr/local/bin/dash #!/bin/sh #!/bin/bash echo... (4 Replies)
Discussion started by: wisecracker
4 Replies

2. Shell Programming and Scripting

Insert a single quote in front of a line in vi editor

Hello Gurus, I wanted to put a single quote in every where starting with /oradata, and at the end with .dbf. For example I have one line as below: alter database rename datafile /oradata/test.dbf to /oradata_new/test.dbf I wanted as below alter database rename datafile '/oradata/test.dbf' to... (3 Replies)
Discussion started by: pokhraj_d
3 Replies

3. Shell Programming and Scripting

Insert single quote on every word separated by comma

Hello, I have a text file as:-ABC BCD CDF DEF EFGI need to convert as 'ABC', 'BCD', 'CDF', 'DEF', 'EFG' using a unix command anybody can help me out on this. Regards, Jas Please wrap all code, files, input & output/errors in CODE tags. It makes them easier to read and preserves... (12 Replies)
Discussion started by: jassi10781
12 Replies

4. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

5. 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

6. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

7. Shell Programming and Scripting

single quote replacement

hi all, i have a data in the file which of the formate : 100,102,103 and the required formate is \'100\',\'102\',\'103 Idealy we need to replace , with \',\' Regards arkesh (2 Replies)
Discussion started by: arkeshtk
2 Replies

8. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

9. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

10. Shell Programming and Scripting

single quote

Hi I have a shell script with many lines as below: comment on column dcases.proj_seq_num is dcases_1sq; .... .... I want the above script to be as below: comment on column dcases.proj_seq_num is 'dcases_1sq'; I want to have single quotes like that as above for the entire shell... (2 Replies)
Discussion started by: dreams5617
2 Replies
Login or Register to Ask a Question