[SOLVED] sed: delete blank lines and modify line immediately after such
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
sed: delete blank lines and modify line immediately after such
sed 4.2.2 on arch 3.18.0
I have many files containing multiple paragraphs of text. In some there are no blank lines and each paragraph begins with a single TAB character. This is the standard I wish to apply to all other files, so I want to remove all blank lines and prepend their following line with a TAB.
I believed the following sed script would do the job
Code:
~ $ sed '/\s*/ {
N
/\n[^\t]/ {
s/.*\n\([^\t].*\)/\t\1/
}
> }' p
but for this input in file p
Code:
drwxr-xr-x 12 root root 4096 Oct 18 09:36 var/
lrwxrwxrwx 1 root root 7 Jul 4 05:44 bin -> usr/bin/
drwxr-xr-x 3 root root 4096 Oct 18 09:34 boot/
lrwxrwxrwx 1 root root 7 Jul 4 05:44 lib -> usr/lib/
drwx------ 2 root root 4096 Oct 18 02:36 lost+found/
drwxr-xr-x 2 root root 4096 Jul 4 05:44 mnt/
drwxrwxrwx 11 g g 4096 Oct 20 10:59 opt/
dr-xr-xr-x 91 root root 0 Oct 20 07:28 proc/
/\s*/ means nothing, it is useless, it matches always, if you want to specify empty lines use /^\s*$/ instead
sed is not able to do multi line search/replace, so using \n will not work
N will join two lines, that is ok, you just need to replace the empty line with a single tab before that.
/\s*/ means nothing, it is useless, it matches always, if you want to specify empty lines use /^\s*$/ instead
sed is not able to do multi line search/replace, so using \n will not work
N will join two lines, that is ok, you just need to replace the empty line with a single tab before that.
Not with any of the following
Code:
sed 's/^\s*$/\t/' N p
sed 's/^\s*$/\t/' { N } p
sed -e 's/^\s*$/\t/' -e '/^\t$/ { N }' p
Not that it is too important, but for future onlookers, the '-e' is not required. In my doodlings I was initially trying multiple sed statements joined together
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.