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 '
:
'
End first quotation which uses single quotes."
Start second quotation, using double-quotes.'
Quoted character."
End second quotation, using double-quotes.'
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
Trailing Arguments with Find -Exec {} +
How to Group by Column Values into Row and Column Header and Then Sum the Value
Is There a Winscp Equivalent for Linux
Bash Variable Assignment Not Working Expected
How to Check All Columns Data Types of Table Using Awk Script
How to Delete History of Last 10 Commands in Shell
How to Recompile Just a Single Kernel Module
X11/Xlib.H Not Found in Ubuntu
Shell Init Issue When Click Tab, What's Wrong with Getcwd
How to Tell Which Unix Shell I am Using
Surround All Lines in a Text File with Quotes ('Something')
What Is .D File After Building with Make
What Happens After a Packet Is Captured
Mongodb Service Will Not Start After Initial Setup