Linux Commands - unfold (unwrap text)

by
Jeremy Canfield |
Updated: October 13 2021
| Linux Commands articles
Let's say foo.txt contains a very long string, like this.
asdfakjdslkfjkllk343lk434l3klkjvsdfsiwqepoqwpisdfsddslkjpizndf3498sdf
The fold command with the -w or --width option can be used to wrap the content of foo.txt. In this example, the string will be wrapped at every 10th character.
fold --width 10 foo.txt
asdfakjdsl
kfjkllk343
lk434l3klk
jvsdfsiwqe
poqwpisdfs
ddslkjpizn
df3498sdf
There is not a command such as unfold. However, you can use the sed command to remove new lines, which should effectively un-fold.
cat foo.txt | sed ':label; N; $! b label; s|\n||g'
Note that these commands will not actual update the file in question. Redirection can be used.
cat foo.txt | sed ':label; N; $! b label; s|\n||g' > foo.txt
Did you find this article helpful?
If so, consider buying me a coffee over at
Comments
November 28 2022 by nyh
Do you try the code you post?
Specifically
cat foo.txt | sed ':label; N; $! b label; s|\n||g' > foo.txt
I dont know what shells you have been using but one simply does not read and write from the same file in a single pipeline.
You also have an excessive `cat` in there - serevs no purpose
Drop the `cat` and run `sed` with the `-i` flag without any redirection if you want an in-place `sed` operation