LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2014, 12:06 PM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
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?
 
Old 11-23-2014, 12:23 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
/\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.
 
1 members found this post helpful.
Old 11-23-2014, 03:15 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by pan64 View Post
/\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
 
Old 11-23-2014, 07:04 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
How about something like:
Code:
sed -e '/^\s*$/{N;s/^\s*\n/\t/}'
Of course the very first line should be indented??
 
1 members found this post helpful.
Old 11-25-2014, 11:10 AM   #5
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by grail View Post
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.

Last edited by porphyry5; 11-25-2014 at 11:14 AM.
 
Old 11-25-2014, 07:42 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Recognize multi-line pattern and delete certain lines (awk/sed?) legato Programming 4 10-31-2012 04:02 PM
[SOLVED] SED/AWK - Delete all lines until empty line is found after pattern match vikas027 Programming 13 03-28-2012 08:33 AM
Sed. Delete blank lines between two patterns supersoni3 Programming 5 07-29-2010 10:40 AM
sed/awk: Three consecutive blank lines in a file, how to delete two of them? recomboDNA Programming 8 06-17-2010 09:50 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration