LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed: delete blank lines and modify line immediately after such (https://www.linuxquestions.org/questions/programming-9/sed-delete-blank-lines-and-modify-line-immediately-after-such-4175526274/)

porphyry5 11-23-2014 12:06 PM

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/

I got the following output
Code:

drwxr-xr-x 12 root root 4096 Oct 18 09:36 var/

        drwxr-xr-x  3 root root 4096 Oct 18 09:34 boot/

        drwxr-xr-x  2 root root 4096 Jul  4 05:44 mnt/
        dr-xr-xr-x 91 root root    0 Oct 20 07:28 proc/

What am I doing wrong?

pan64 11-23-2014 12:23 PM

/\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.

porphyry5 11-23-2014 03:15 PM

Quote:

Originally Posted by pan64 (Post 5273874)
/\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

The substitution works fine, but not the join

grail 11-23-2014 07:04 PM

How about something like:
Code:

sed -e '/^\s*$/{N;s/^\s*\n/\t/}'
Of course the very first line should be indented??

porphyry5 11-25-2014 11:10 AM

Quote:

Originally Posted by grail (Post 5274028)
How about something like:
Code:

sed -e '/^\s*$/{N;s/^\s*\n/\t/}'
Of course the very first line should be indented??

Thank you, that does the job perfectly. No problem about the first line, subsequent program assumes first line is a new paragraph.

grail 11-25-2014 07:42 PM

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 :)


All times are GMT -5. The time now is 01:22 AM.