Skip to main content
Code Review

Return to Answer

Minor spelling and grammar; deprettify the non-code
Source Link
Toby Speight
  • 87.7k
  • 14
  • 104
  • 325

You never need sed when you're using awk, pipes. Pipes of sed-or-grep-or awk-awk to sed-or-grep-or-awk are an antipattern (see https://porkmail.org/era/unix/award#grep), and if fold doesn't do what you want then it's pretty simple to implement the line wrapping functionality you're asking for in awk too.

$ ./foldLines.sh file
 Lorem--ipsum /dolor/ sit† amet, consectetur adipiscing elit, sed do
euismo tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum
dolor sit amet.
 Lorem ipsum dolor sit amet.
 Lorem ipsum dolor sit amet.
 Lorem ipsum dolor sit amet.
 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do euismo
tempor incididunt ut labore et dolore magna aliqua.
 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do euismo
tempor incididunt ut labore et dolore magna aliqua.
 † The word "sit" consists of three letters.
$ ./foldLines.sh file
 Lorem--ipsum /dolor/ sit† amet, consectetur adipiscing elit, sed do
euismo tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum
dolor sit amet.
 Lorem ipsum dolor sit amet.
 Lorem ipsum dolor sit amet.
 Lorem ipsum dolor sit amet.
 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do euismo
tempor incididunt ut labore et dolore magna aliqua.
 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do euismo
tempor incididunt ut labore et dolore magna aliqua.
 † The word "sit" consists of three letters.

You never need sed when you're using awk, pipes of sed-or-grep-or awk to sed-or-grep-or-awk are an antipattern (see https://porkmail.org/era/unix/award#grep), and if fold doesn't do what you want then it's pretty simple to implement the line wrapping functionality you're asking for in awk too.

$ ./foldLines.sh file
 Lorem--ipsum /dolor/ sit† amet, consectetur adipiscing elit, sed do
euismo tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum
dolor sit amet.
 Lorem ipsum dolor sit amet.
 Lorem ipsum dolor sit amet.
 Lorem ipsum dolor sit amet.
 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do euismo
tempor incididunt ut labore et dolore magna aliqua.
 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do euismo
tempor incididunt ut labore et dolore magna aliqua.
 † The word "sit" consists of three letters.

You never need sed when you're using awk. Pipes of sed-or-grep-or-awk to sed-or-grep-or-awk are an antipattern (see https://porkmail.org/era/unix/award#grep), and if fold doesn't do what you want then it's pretty simple to implement the line wrapping functionality you're asking for in awk too.

$ ./foldLines.sh file
 Lorem--ipsum /dolor/ sit† amet, consectetur adipiscing elit, sed do
euismo tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum
dolor sit amet.
 Lorem ipsum dolor sit amet.
 Lorem ipsum dolor sit amet.
 Lorem ipsum dolor sit amet.
 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do euismo
tempor incididunt ut labore et dolore magna aliqua.
 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do euismo
tempor incididunt ut labore et dolore magna aliqua.
 † The word "sit" consists of three letters.
added 39 characters in body
Source Link
Ed Morton
  • 451
  • 2
  • 7
$ cat ./foldLines.sh
#!/usr/bin/env bash
awk -v maxLen=72 '
 { sub(/\\+[[:space:]]*$/, "") }
 NF {
 gsub(/ - /, "--")
 gsub(/ {0,1}[—–] {0,1}/, "--")
 gsub(/.../, "...")
 gsub(/\\\*/, "†")
 gsub(/\*/, "/")
 gsub(/\\\./, ".")
 sub(/[[:space:]\\]+$]]+$/,"")
 out = " "
 sep = ""
 for ( i=1; i<=NF; i++ ) {
 nextOut = out sep $i
 if ( length(nextOut) > maxLen ) {
 print out
 out = $i
 }
 else {
 out = nextOut
 sep = FS
 }
 }
 print out
 numEmpty = 0
 next
 }
 ++numEmpty == 2 {
 print ""
 }
' "${@:--}"
$ cat ./foldLines.sh
#!/usr/bin/env bash
awk -v maxLen=72 '
 NF {
 gsub(/ - /, "--")
 gsub(/ {0,1}[—–] {0,1}/, "--")
 gsub(/.../, "...")
 gsub(/\\\*/, "†")
 gsub(/\*/, "/")
 gsub(/\\\./, ".")
 sub(/[[:space:]\\]+$/,"")
 out = " "
 sep = ""
 for ( i=1; i<=NF; i++ ) {
 nextOut = out sep $i
 if ( length(nextOut) > maxLen ) {
 print out
 out = $i
 }
 else {
 out = nextOut
 sep = FS
 }
 }
 print out
 numEmpty = 0
 next
 }
 ++numEmpty == 2 {
 print ""
 }
' "${@:--}"
$ cat ./foldLines.sh
#!/usr/bin/env bash
awk -v maxLen=72 '
 { sub(/\\+[[:space:]]*$/, "") }
 NF {
 gsub(/ - /, "--")
 gsub(/ {0,1}[—–] {0,1}/, "--")
 gsub(/.../, "...")
 gsub(/\\\*/, "†")
 gsub(/\*/, "/")
 gsub(/\\\./, ".")
 sub(/[[:space:]]+$/,"")
 out = " "
 sep = ""
 for ( i=1; i<=NF; i++ ) {
 nextOut = out sep $i
 if ( length(nextOut) > maxLen ) {
 print out
 out = $i
 }
 else {
 out = nextOut
 sep = FS
 }
 }
 print out
 numEmpty = 0
 next
 }
 ++numEmpty == 2 {
 print ""
 }
' "${@:--}"
added 31 characters in body
Source Link
Ed Morton
  • 451
  • 2
  • 7

You never need sed when you're using awk, pipes of sed-or-grep-or awk to sed-or-grep-or-awk are an antipattern (see https://porkmail.org/era/unix/award#grep), and if fold doesn't do what you want then it's pretty simple to implement thatthe line wrapping functionality you're asking for in awk too.

You never need sed when you're using awk, pipes of sed-or-grep-or awk to sed-or-grep-or-awk are an antipattern (see https://porkmail.org/era/unix/award#grep), and if fold doesn't do what you want then it's pretty simple to implement that functionality in awk too.

You never need sed when you're using awk, pipes of sed-or-grep-or awk to sed-or-grep-or-awk are an antipattern (see https://porkmail.org/era/unix/award#grep), and if fold doesn't do what you want then it's pretty simple to implement the line wrapping functionality you're asking for in awk too.

added 275 characters in body
Source Link
Ed Morton
  • 451
  • 2
  • 7
Loading
deleted 12 characters in body
Source Link
Ed Morton
  • 451
  • 2
  • 7
Loading
added 158 characters in body
Source Link
Ed Morton
  • 451
  • 2
  • 7
Loading
deleted 551 characters in body
Source Link
Ed Morton
  • 451
  • 2
  • 7
Loading
deleted 1 character in body
Source Link
Ed Morton
  • 451
  • 2
  • 7
Loading
added 10 characters in body
Source Link
Ed Morton
  • 451
  • 2
  • 7
Loading
added 53 characters in body
Source Link
Ed Morton
  • 451
  • 2
  • 7
Loading
Source Link
Ed Morton
  • 451
  • 2
  • 7
Loading
default

AltStyle によって変換されたページ (->オリジナル) /