DaemonForums  

Go Back   DaemonForums > Miscellaneous > Guides

Guides All Guides and HOWTO's.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 21st August 2022
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,134
Default Hardwrap: wrap lines to specified length

A perl script that wraps lines to a specified length, even if it is in the middle of a word. Just like the Red Queen it just chops it off.
See fmt(1) for a more gentle wrap that respects words by splitting only on white space.

Some examples of how to use hardwrap are described in the script:
Code:
$ echo 123456789 | ./hardwrap 4
1234
5678
9
$ openssl rand -base64 48 | ./hardwrap 15  
2IHvJwLw/F3s07y
QMMHz7rYPo9Ems/
avUD0y4eAd/WMQt
fVzND0UzafsKYAs
+qXm

$ dmesg | ./hardwrap 70 | less 
[snip]
cpu0 at mainbus0: apid 0 (boot processor)
cpu0: Intel(R) Xeon(R) CPU E3-1220 v3 @ 3.10GHz, 3093.23 MHz, 06-3c-03

cpu0: FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PA
T,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTE
S64,MWAIT,DS-CPL,VMX,SMX,EST,TM2,SSSE3,SDBG,FMA3,CX16,xTPR,PDCM,PCID,S
SE4.1,SSE4.2,x2APIC,MOVBE,POPCNT,DEADLINE,AES,XSAVE,AVX,F16C,RDRAND,NX
E,PAGE1GB,RDTSCP,LONG,LAHF,ABM,PERF,ITSC,FSGSBASE,TSC_ADJUST,BMI1,AVX2
,SMEP,BMI2,ERMS,INVPCID,SRBDS_CTRL,MD_CLEAR,IBRS,IBPB,STIBP,L1DF,SSBD,
SENSOR,ARAT,XSAVEOPT,MELTDOWN
cpu0: 256KB 64b/line 8-way L2 

$ ./hardwrap 10 < random.txt
fLS89I27R9
x2fuEkr3oy
MMc0
The script itself is very short:
Code:
#!/usr/bin/perl
# (c) 2022 J65nko - daemonforums.org
# Free to use and distribute under ISC license

use warnings;
use strict;

# read line width parameter
my $LineWidth = $ARGV[0];

# shift ARGV[1] into ARGV[0] (could be the optional file name)
shift;

# read from file or standard input

while (<>) {
    s/(.{$LineWidth})/$1\n/g;
    print; 
}
# --- end of script
A global search and substitute regular expression, collects the number of characters in the variable $1, appends a newline character and prints it.

Because of the "/g" global specifier, it tries to repeat the procedure. If it fails to collect the needed number of characters, it will just give up. The remainder of the characters in the line will just be printed.

A short explanation of the regex magic as described in the script:
Code:
# s             search and substitute 
# /             delimiter for start of search pattern
# (             start capture in variable $1
# .             any character
# {             start quantifier specification
# $lineWidth    number/integer passed as parameter
# }             end quantifier specification
# )             stop capture in variable $1
# /             end delimiter search, begin delimiter for substitution 
# $1            the captured characters
# \n            add a newline 
# /             end delimiter for replace
# g             g(lobal) : repeat for whole line
Attached Files
File Type: txt hardwrap.txt (1.8 KB, 12 views)
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump

Last edited by J65nko; 21st August 2022 at 11:22 PM. Reason: Added attachment for downloading
Reply With Quote
Reply

Tags
fmt(1), format text, truncate text lines, wrap text lines

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenBSD and php header Content-Length jonsec OpenBSD General 2 15th February 2020 04:27 PM
wrap man pages to terminal width hulten OpenBSD General 5 11th October 2015 11:54 PM
ksh: wrap lines? Skinny OpenBSD General 11 3rd May 2013 11:30 PM
PF - packets filtering by length? magnesik OpenBSD Security 3 3rd July 2011 12:46 PM


All times are GMT. The time now is 12:29 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick