I have command line this:
xclip -o -sel clip | sed -e 's/^ *"\(.*\)"+\?/\1/g' | tr -d '\n' |
sed -e 's/.\{,55\}/"&"+\n/g' |
sed -e "2,$ s/^/$(seq 22 | xargs -I{} echo -n ' ')/" |
sed -e '$ s/+$//' | xclip -sel clip
which format string in source code (I use it to format long string in javascript file – 55 and 22 are harcoded width and indent)
var foo = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"+
". Nulla sed dolor nisl, in suscipit justo. Donec a enim"+
" et est porttitor semper at vitae augue. Proin at nulla"+
" at dui mattis mattis. Nam a volutpat ante. Aliquam con"+
"sequat dui eu sem convallis ullamcorper."
but when I copy past code inside parentheses var foo = foo.bar() I got this
var foo = foo.bar("Lorem ipsum dolor sit amet, consectetur adipiscing elit"+
". Nulla sed dolor nisl, in suscipit justo. Donec a enim"+
" et est porttitor semper at vitae augue. Proin at nulla"+
" at dui mattis mattis. Nam a volutpat ante. Aliquam con"+
"sequat dui eu sem convallis ullamcorper."
)
New line after last quote, not the big deal but anybody know how can I remove that last new line?
Here’s one way of doing it, using
awk. SettingRS(record separator) to the empty string tellsawkto treat blank lines (i.e. two consecutive\n) as record separators. SettingFSto\ncausesawkto take\nas field separators instead of white-space. (SettingRSto the empty string also causes\nto be used as a field separator, but we want to avoid having other whitespace recognized.) Finally, we setOFS(output field separator) to be the string we want between lines (closing quote, plus, linefeed, 22 spaces, opening quote) and settingORS(output record separator) to an empty string causesawkto not write any linefeed at the end.I use
foldto split the lines into 55-byte chunks instead of sed, partly because it seems tidier, and partly because, unlikesed, it doesn’t add a new line at the end of the input if there wasn’t one to start with.The seemingly redundant
$1=$1causesawkto recreate$0, which has the effect of usingOFSas the output field separator between the fields. Otherwise, it is only used to separate separate print arguments.The first and last line are unchanged from the OP.