Bash: Wrap Long Lines Inside the Same Column
Actually, the util-linux 'column' command can do it. It is very versatile.
#!/bin/bash
cat <<- EOF | column --separator '|' \
--table \
--output-width 30 \
--table-noheadings \
--table-columns C1,C2 \
--table-wrap C2
key1|Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
key2|blahhhhhhhhhhhhhhhhhhhhhhhhhhhhzzz
EOF
This gives :
key1 Lorem ipsum dolor sit am
et, consectetur adipisci
ng elit, sed do eiusmod
tempor incididunt ut lab
ore et dolore magna aliq
ua.
key2 blahhhhhhhhhhhhhhhhhhhhh
hhhhhhhzzz
--output-width
: give desired size (can use 'tput cols' as described above)
--table-columns C1,C2
: give names to columns to be used with other options
--table-wrap C2
: wrap column 2
Column version :
# column -V
column from util-linux 2.33.1
Wrap a single oversize column with awk / bash (pretty print)
First build a test file (called file.txt
):
echo "AA BBBB CCC
01 Item Description here
02 Meti A very very veeeery long description which will easily extend the recommended output width of 80 characters.
03 Etim Last description" > file.txt
Now the script (called ./split-columns.sh
):
#!/bin/bash
FILE=$1
#find position of 3rd column (starting with 'CCC')
padding=`cat $FILE | head -n1 | grep -aob 'CCC' | grep -oE '[0-9]+'`
paddingstr=`printf "%-${padding}s" ' '`
#set max length
maxcolsize=50
maxlen=$(($padding + $maxcolsize))
cat $FILE | while read line; do
#split the line only if it exceeds the desired length
if [[ ${#line} -gt $maxlen ]] ; then
echo "$line" | fmt -s -w$maxcolsize - | head -n1
echo "$line" | fmt -s -w$maxcolsize - | tail -n+2 | sed "s/^/$paddingstr/"
else
echo "$line";
fi;
done;
Finally run it with the file as a single argument
./split-columns.sh file.txt > fixed-width-file.txt
Output will be:
AA BBBB CCC
01 Item Description here
02 Meti A very very veeeery long description
which will easily extend the recommended output
width of 80 characters.
03 Etim Last description
cat file with no line wrap
Note that cut
accepts a filename as an argument.
This seems to work for me:
watch 'bash -c "cut -c -$COLUMNS file"'
For testing, I added a right margin:
watch 'bash -c "cut -c -$(($COLUMNS-10)) file"'
When I resized my terminal, the truncation was updated to match.
vim command to restructure/force text to 80 columns
Set textwidth
to 80 (:set textwidth=80
), move to the start of the file (can be done with Ctrl-Home or gg
), and type gqG
.
gqG
formats the text starting from the current position and to the end of the file. It will automatically join consecutive lines when possible. You can place a blank line between two lines if you don't want those two to be joined together.
Related Topics
Using a Glob Expression Passed as a Bash Script Argument
Mercurial Hg No Changes Found - Can't Hg Push Out
Is There a Command to List All Unix Group Names
Need Explanations for Linux Bash Builtin Exec Command Behavior
Permanently Change Disassembly Flavor in Gdb
Using Bash Script to Find Line Number of String in File
Which Context Are Softirq and Tasklet In
How to Retain Docker Alpine Container After "Exit" Is Used
Splitting Gzipped Logfiles Without Storing the Ungzipped Splits on Disk
Have One Folder with Files That Have the Same Name But Different File
How to Tell Qt to Use Different Openssl
How to Remove Specific Rules from Iptables
How to Check in Bash Whether a File Was Created More Than X Time Ago
Linux Shell to Restrict Sftp Users to Their Home Directories