| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| I was complaining about non-maintainable, code and the task that I gave as an example to do was:- | rearrange a small text file, so that: | * all lines of length > 78 are wrapped to len < 75, with breaks at | word-boundries; | * the "> " strings which start each line are replaced to the | beginning of the new shorter lines; And some smart-arse gave:- >this tiny task simply cries out for a "one-liner" shell script! > > sed -s 's/^> //' ${1+"$@"} | fmt -75 | sed -s 's/^/> /' Here's my attempt at starting to decode this: | fmt -75 | Means 'accept the stream and format it into line-len < 76 and pass it on' sed -s 's/^> //' = dud !? sed s 's/^> //' = substitute: newLine, ">" with nil == fails --------- Here's my well commented code so far. The failing part is replacing char(10) with a space. Probably since sed reads one LINE at a time ? I'm aware of potential 'overflow' if it's all one line. -------------- # remove all leading ">" == sed s/^">"//g DataFl > Rm1stArw.1 # replace \n with space #sed s/$/"x"/g Rm1stArw.1 > RmLT.2 #sed s/\n/" "/g Rm1stArw.1 > RmLT.2 #sed s/$/" "/g Rm1stArw.1 > RmLT.2 #sed s/$\n/" "/g Rm1stArw.1 > RmLT.2 #sed '$!N;s/\n/" "/' Rm1stArw.1 > RmLT.2 # sed '$!N;s/\n/ /' Rm1stArw.1 > RmLT.2 ==ok every 2nd line was joined #sed 's/\n/ /g' Rm1stArw.1 > RmLT.2 sed 's/\n/ /'g Rm1stArw.1 > RmLT.2 <-- last try also fails ! cat RmLT.2 | fmt -44 > Linewrap.3 #and remove leading space from fmt ? # try: [ \t]* ==[Spc and?or Tab]Repeated sed s/^" "// Linewrap.3 > NoLdSpc.4 # add leading ">" sed s/^/">"/ NoLdSpc.4 > Done ----------------- end of script == TIA. |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.