How to insert a single quote to each record

I have a file as:

1
New
used
1
used
New

I need o/p as:

'1'
'New'
'used'
'1'
'used'
'New'

How can I get this??

Thanks!!!

while read LINE
do
        echo "'${LINE}'"
done < infile > outfile

If you want to use "sed":

sed "s/^/'/;s/$/'/" Inp_File

Thanks a ton guys!!!
:b:

awk '{print "'"$0"'"}' input > output.txt
1 Like
perl -plne "s/^/'/; s/$/'/" your_file
 
perl -plne "s/^(.*)$/'\$1'/" your_file

tyler_durden

For the sake of learning and argument, can one combine the search and replaces in vi into one statement?

Put a single quote at the beginning of the line:

:%s/^/'

Put a single quote at the end of the line:

:%s/$/'

How to do this in one search and replace in vi?

Don't know about vi, but this works in vim:

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

tyler_durden

Sweet, that works in my version of vi after removing the space between the '%' and the 's':

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

Could you explain the expression used?

or

:%s/.*/'&'/

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 :-/

:%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

1 Like

exactly!

1 Like