- 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.
$ 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 ""
}
' "${@:--}"
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.