Surround every line with single quote except empty lines Surround every line with single quote except empty lines unix unix

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'$


One way:

awk '$1{$0 = q $0 q}1' q="'" file

Add quotes only if 1st column($1) has some value. 1 to print every line.


Assuming you want to add the single quotes to lines that contain nothing but whitespace:

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