Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

8 votes
4 answers
3530 views
Wrap and indent text using coreutils
### Short version I would like to create a tabular display of multiline text, similar to the following: all Build all targets document Create documentation of source files in the subfolders `src` and `script`, and write it to `man` test Run unit tests At the moment, my input for this looks as follow...
### Short version I would like to create a tabular display of multiline text, similar to the following: all Build all targets document Create documentation of source files in the subfolders src and script, and write it to man test Run unit tests At the moment, my input for this looks as follows, but this can of course be changed: all---Build all targets document---Create documentation of source files in the subfolders src and script, and write it to man test---Run unit tests I’ve tried achieving this with a combination of awk and wrap/pr but while the line wrapping works, the indentation doesn’t. Here’s my current approach: … | awk -F '---' "{ printf '%-10s %s\n', $1, $2 }" \ | fold -w $(($COLUMNS - 1)) -s It generates the output all Build all targets document Create documentation of source files in the subfolders src and script, and write it to man test Run unit tests … in other words, the third line isn’t indented as intended. **How can I format the text with a given wrap length and a given hanging indent width?** — Without changing anything else about the text. Bonus: this should work with UTF-8 and escape/control characters. --- ### Background info The goal is to create [self-documenting Makefiles](https://gist.github.com/klmr/575726c7e05d8780505a) . As a consequence, the logic to format and display the code should be small, self-contained, and not rely on separately installed software; ideally, it should work on any system that can execute Makefiles, hence my restriction to (something close to) coreutils. That said, I briefly tried solving the problem using groff but this became too complex very quickly (and OS X groff is and old version that doesn’t seem to support UTF-8). The *original string* that I’m trying to parse and format therefore looks rather as follows: ## Build all targets all: test document ## Run unit tests test: ./run-tests . ## create documentation of source files in the subfolders src and script, ## and write it to man document: ${MAKE} -C src document ${MAKE} -C script document At the moment, this is parsed using a sed script (see link for details) that ignores multiline comments, before being fed to the formatting code posted above.
Konrad Rudolph (3839 rep)
Apr 30, 2016, 12:56 PM • Last activity: Oct 17, 2024, 03:16 AM
0 votes
2 answers
129 views
Use 'fold' to wrap lines at 72 column
Test file: ``` Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmo tempor incididunt ut labore et dolore magna aliqua. Ut enim minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit...
Test file:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmo tempor incididunt ut labore et dolore magna aliqua. Ut enim minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
If using
script.sh input.txt output.txt
sed 's/ - /--/g
     s/ \{0,1\}[—–] \{0,1\}/--/g
     s/…/.../g
     s/\\\*/†/g
     s/*/\//g
     s/\\\././g' $1 |
fold -sw 72 |
sed 's/ *$//g' > $2
the output is ``` ------------------------------------------------------------------------ -s: Fold line after the last blank character within the first width column positions (or bytes). I use fold supplied with macOS, but if using GNU fold, the output file is the same.
jsx97 (1347 rep)
Aug 30, 2024, 06:24 PM • Last activity: Sep 3, 2024, 11:17 PM
10 votes
6 answers
1916 views
unicode safe alternative for fold
I'm using `fold -w 3` to split a line into multiple 3 chars long, however with the GNU implementation, it does not work for text with multi-byte characters it seems. How can I achieve the above with `sed`? I've come up with ` sed -r 's/^(.{0,3})(.*)/\1\n\2/g'` however this only does a single replace...
I'm using fold -w 3 to split a line into multiple 3 chars long, however with the GNU implementation, it does not work for text with multi-byte characters it seems. How can I achieve the above with sed? I've come up with sed -r 's/^(.{0,3})(.*)/\1\n\2/g' however this only does a single replacement: echo "111222333444555666" | sed -r 's/^(.{0,3})(.*)/\1\n\2/g' 111 222333444555666 --- Additional examples: echo "ĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄ" | sed -r 's/^(.{0,3})(.*)/\1\n\2/g' ĄĄĄ ĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄ And fold with it's corrupting behavior: echo "ĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄĄ" | fold -w 3 Ą� �Ą Ą� �Ą Ą�
Chris Stryczynski (6603 rep)
Jul 7, 2018, 05:24 PM • Last activity: Mar 1, 2022, 03:54 PM
0 votes
0 answers
128 views
FOLD not working on very long lines
I have a fold command as below to split a file with very long line into multiple lines. It works if the file is small. But if the length of the file is too long, it stops adding new line character half way. fold -b -w 3000 > Is there an alternate way to do this? I am on RHEL.
I have a fold command as below to split a file with very long line into multiple lines. It works if the file is small. But if the length of the file is too long, it stops adding new line character half way. fold -b -w 3000 > Is there an alternate way to do this? I am on RHEL.
Ram (25 rep)
Feb 24, 2022, 07:37 PM • Last activity: Feb 24, 2022, 07:43 PM
6 votes
3 answers
740 views
Unix fold command behaving strangely
So I have this fasta (biology) file that looks like this: >m64093_191209_130050/133911/ccs_64 TTCAGGCTGTGTTCCATTTGATTTAAAATCAAATAATTTCATTCGCGTCAGAACACCTGGTTTCACGACC ATAAATAATTTACCAGTGAATCGAGGCTCAATTATAGATCCTCGGACGCGAGTTCTCGGTTGACGAGTGG GATTCGAATTATTTTTCACCGAAAATTTTAGTCGACGAGTTCAGATAAATTTGTTCGGGATAAA...
So I have this fasta (biology) file that looks like this: >m64093_191209_130050/133911/ccs_64 TTCAGGCTGTGTTCCATTTGATTTAAAATCAAATAATTTCATTCGCGTCAGAACACCTGGTTTCACGACC ATAAATAATTTACCAGTGAATCGAGGCTCAATTATAGATCCTCGGACGCGAGTTCTCGGTTGACGAGTGG GATTCGAATTATTTTTCACCGAAAATTTTAGTCGACGAGTTCAGATAAATTTGTTCGGGATAAAATCATC TGAGTAGGTCGGGCTTCTGAATTTCGTATTCTTGCGAGCAATGAATTTTAAATAATCATCGGACATACCA ATTTTTGGAACAATAATGTTCCGAACATCCCGAAAATATAGGAAGAGCCCGGATAGATAAAAATAAACAC Each line is max 70 chars long. Usually, if I want to format it to max 50 characters long, I use: > fold -50 input.fasta > output.fasta # also tried -b and -w args But somehow this is not working. The file looks exactly the same as many others I've seen. The output now looks like this: >m64093_191209_130050/133911/ccs_64 TTCAGGCTGTGTTCCATTTGATTTAAAATCAAATAATTTCATTCGCGTCA GAACACCTGGTTTCACGACC ATAAATAATTTACCAGTGAATCGAGGCTCAATTATAGATCCTCGGACGCG AGTTCTCGGTTGACGAGTGG GATTCGAATTATTTTTCACCGAAAATTTTAGTCGACGAGTTCAGATAAAT TTGTTCGGGATAAAATCATC TGAGTAGGTCGGGCTTCTGAATTTCGTATTCTTGCGAGCAATGAATTTTA AATAATCATCGGACATACCA ATTTTTGGAACAATAATGTTCCGAACATCCCGAAAATATAGGAAGAGCCC It cuts overhanging 20 characters and correctly places them bellow, but then it's not joining the next line and cutting it on max 50 chars as it should. I went back to previous fasta files I created and the fold command still works normally. The problem persists if I copy a segment of the new file and past it in another file. I think there might be an encoding problem that I'm not aware of. Can anyone help? Cheers, EDIT: Great answers, thanks!!
Ricardo Guerreiro (163 rep)
Jan 24, 2020, 03:56 PM • Last activity: Jan 24, 2020, 09:22 PM
0 votes
1 answers
596 views
fold: outputs only the first character
I am using `fold` to make an array of characters from a string. INPUT=$1 arr=($(fold -w1 <<< "$INPUT")) echo "$arr" echo "$arr[@]" I use command `./myProgram 1234` to execute the script. Expected output: `1 2 3 4` What I get: `1` and `1[@]`
I am using fold to make an array of characters from a string. INPUT=$1 arr=($(fold -w1 <<< "$INPUT")) echo "$arr" echo "$arr[@]" I use command ./myProgram 1234 to execute the script. Expected output: 1 2 3 4 What I get: 1 and 1[@]
HarshvardhanSharma (289 rep)
Dec 6, 2018, 05:27 PM • Last activity: Dec 6, 2018, 05:36 PM
11 votes
2 answers
10409 views
How to wrap output of watch command?
Consider the following command: watch -d "ps -ef | grep java" It gives following output: kshitiz 11369 11285 0 Oct13 ? 00:06:02 /usr/lib/jvm/jdk1.7.0_40/jre/bin/java -Djava.awt.headless=true -Didea.version==14.1.2 -Xmx512m -Dfile.e The output beyond `-Dfile.e` is truncated or flows off screen. How c...
Consider the following command: watch -d "ps -ef | grep java" It gives following output: kshitiz 11369 11285 0 Oct13 ? 00:06:02 /usr/lib/jvm/jdk1.7.0_40/jre/bin/java -Djava.awt.headless=true -Didea.version==14.1.2 -Xmx512m -Dfile.e The output beyond -Dfile.e is truncated or flows off screen. How can I scroll to see the output or word wrap it? Using fold without watch: enter image description here Using fold with watch: enter image description here
Kshitiz Sharma (9105 rep)
Oct 14, 2015, 06:59 AM • Last activity: Dec 6, 2018, 05:30 PM
Showing page 1 of 7 total questions