Surround All Lines in a Text File with Quotes ('Something')

Surround all lines in a text file with quotes ('something')

Use sed?

sed -e "s/\(.*\)/'\1'/"

Or, as commented below, if the directories might contain apostrophes (nightmare if they do) use this alternate

sed -e "s/'/'\\\\''/g;s/\(.*\)/'\1'/"

Surround every line with single quote except empty lines

.* means zero or more characters, you want 1 or more characters which in any sed would be ..*:

$ sed "s/..*/'&'/" file
'Quote1'
'Quote2'

'Quote3'

You can also write that regexp as .\+ in GNU sed, .\{1,\} in POSIX seds, and .+ in GNU or OSX/BSD sed when invoked with -E.

The above assumes lines of all blanks should be quoted. If that's wrong then:

$ sed "s/.*[^[:blank:]].*/'&'/" file
'Quote1'
'Quote2'

'Quote3'

In any awk assuming lines of all blanks should be quoted:

$ awk '/./{$0="\047" $0 "\047"}1' file
'Quote1'
'Quote2'

'Quote3'

otherwise:

$ awk 'NF{$0="\047" $0 "\047"}1' file
'Quote1'
'Quote2'

'Quote3'

You can see the difference between the above with this:

$ printf '   \n' | sed "s/..*/'&'/"
' '
$ printf ' \n' | sed "s/.*[^[:blank:]].*/'&'/"

$ printf ' \n' | awk '/./{$0="\047" $0 "\047"}1'
' '
$ printf ' \n' | awk 'NF{$0="\047" $0 "\047"}1'

$

How to enclose every line in a file in double quotes with sed?

here it is

sed 's/\(.*\)/"\1"/g'

How to escape single quotes within single quoted strings

If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:

 alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
# ^^^^^ ^^^^^ ^^^^^ ^^^^
# 12345 12345 12345 1234

Explanation of how '"'"' is interpreted as just ':

  1. ' End first quotation which uses single quotes.
  2. " Start second quotation, using double-quotes.
  3. ' Quoted character.
  4. " End second quotation, using double-quotes.
  5. ' Start third quotation, using single quotes.

If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.

What Vim command(s) can be used to quote/unquote words?

surround.vim is going to be your easiest answer. If you are truly set against using it, here are some examples for what you can do. Not necessarily the most efficient, but that's why surround.vim was written.

  • Quote a word, using single quotes

    ciw'Ctrl+r"'
    • ciw - Delete the word the cursor is on, and end up in insert mode.
    • ' - add the first quote.
    • Ctrl+r" - Insert the contents of the " register, aka the last yank/delete.
    • ' - add the closing quote.


  • Unquote a word that's enclosed in single quotes

    di'hPl2x
    • di' - Delete the word enclosed by single quotes.
    • hP - Move the cursor left one place (on top of the opening quote) and put the just deleted text before the quote.
    • l - Move the cursor right one place (on top of the opening quote).
    • 2x - Delete the two quotes.


  • Change single quotes to double quotes

    va':s/\%V'\%V/"/g
    • va' - Visually select the quoted word and the quotes.
    • :s/ - Start a replacement.
    • \%V'\%V - Only match single quotes that are within the visually selected region.
    • /"/g - Replace them all with double quotes.

Python script that surrounds each line in file with apostrophe

You can simply do string formatting while reading the file and write the updated string to the outfile!

with open(inFile,'r') as f, open(outFile,'w') as w:
for line in f.readlines():
w.write("'%s'\n" % line.rstrip('\n'))


Related Topics



Leave a reply



Submit