Sed/awk to delete single lines that aren't touching other lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed/awk to delete single lines that aren't touching other lines
# 1  
Old 12-20-2011
Sed/awk to delete single lines that aren't touching other lines

Hello,

I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below).

Example:

one
two

three
four

five

six
seven

eight

I want it to look like:

one
two

three
four

six
seven

Where "five" and "eight" were deleted because they were single lines. Any ideas?
# 2  
Old 12-21-2011
Try this...
Code:
tr '\n' '-' < input_file | sed 's/--[^-]*--/--/g;s/-/\n/g'

#or

awk '/^$/{getline x;getline y;if(!y){next}else{print x"\n"y}}1' input_file

Results may vary. If it doesn't work, post a bigger sample.

--ahamed
# 3  
Old 12-21-2011
I don't believe that ahamed's awk will handle two 'singles' in a row. If that's never the case, then no biggie, but for data like this:

Code:
one
two

three
four

five

six
seven

eight

nine
ten 
eleven
twelve

thirteen lone

fourteen lone

fifteen
sixteen

I'd suggest:

Code:
awk '
    NF == 0 {
        if( count >1 )
            printf( "%s\n",  buffer );

        count = 0;
        buffer = "";
        next;
    }

    {
        buffer = buffer $0 "\n";
        count++;
    }
' input-file

Which yields:
Code:
one
two

three
four

six
seven

nine
ten 
eleven
twelve

fifteen
sixteen

This User Gave Thanks to agama For This Post:
# 4  
Old 12-21-2011
Quote:
Originally Posted by agama
I don't believe that ahamed's awk will handle two 'singles' in a row. If that's never the case, then no biggie, but for data like this:
Yes agama, you are right. I figured that out, thats why I put a disclaimer "Results may vary..." Smilie

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 12-21-2011
Works

Works perfectly. Thank you both!

Quote:
Originally Posted by agama
I don't believe that ahamed's awk will handle two 'singles' in a row. If that's never the case, then no biggie, but for data like this:

Code:
one
two

three
four

five

six
seven

eight

nine
ten 
eleven
twelve

thirteen lone

fourteen lone

fifteen
sixteen

I'd suggest:

Code:
awk '
    NF == 0 {
        if( count >1 )
            printf( "%s\n",  buffer );

        count = 0;
        buffer = "";
        next;
    }

    {
        buffer = buffer $0 "\n";
        count++;
    }
' input-file

Which yields:
Code:
one
two

three
four

six
seven

nine
ten 
eleven
twelve

fifteen
sixteen

# 6  
Old 12-21-2011
Quote:
Originally Posted by ahamed101
Yes agama, you are right. I figured that out, thats why I put a disclaimer "Results may vary..." Smilie

--ahamed
That will teach me to look at everything -- not just the code Smilie



As always, nice to see your approach.
# 7  
Old 12-21-2011
You can also give a try to this much more simple one:

Code:
nawk 'NF>1' RS="" FS="\n" ORS="\n\n" yourfile

or

Code:
nawk 'BEGIN {RS="";FS="\n"}NF>1' ORS="\n\n" yourfile

Code:
$ cat f
one
two

three
four

five

six
seven

eight

nine
ten 
eleven
twelve

thirteen lone

fourteen lone

fifteen
sixteen
$ nawk 'NF>1' RS="" FS="\n" ORS="\n\n" f
one
two

three
four

six
seven

nine
ten 
eleven
twelve

fifteen
sixteen

$


Last edited by ctsgnb; 12-22-2011 at 07:09 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed/awk to delete a regex between range of lines

Hi Guys I am looking for a solution to one problem to remove parentheses in a range of lines. Input file module bist_logic_inst(a, ab , dhd, dhdh , djdj, hdh, djjd, jdj, dhd, dhp, dk ); input a; input ab; input dhd; input djdj; input dhd; output hdh; output djjd; output jdj;... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

sed or awk: delete n lines following a formfeed

Hi Members, This is my first post in this forum. I want to do is match form feed lines one by one in a file and delete the next n lines (ex-3 lines) with the form feed character Eg - Files looks like Data 1 Data 2 Data 3 FF Hdr1 Hdr2 Hdr3 Data4 Data5 FF Hdr1 Hdr2 Hdr3 (9 Replies)
Discussion started by: yohan
9 Replies

3. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

4. Shell Programming and Scripting

Delete Lines : after pattern1 and between pattern2 and pattern3 using awk/sed/perl

Hi I need to delete lines from a file which are after pattern1 and between pattern 2 and patter3, as below: aaaaaaaa bbbbbbbb pattern1 cdededed ddededed pattern2 fefefefe <-----Delete this line efefefef <-----Delete this line pattern3 adsffdsd huaserew Please can you suggest... (6 Replies)
Discussion started by: vk2012
6 Replies

5. Shell Programming and Scripting

sed or awk delete character in the lines before and after the matching line

Sample file: This is line one, this is another line, this is the PRIMARY INDEX line l ; This is another line The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line... (5 Replies)
Discussion started by: KC_Rules
5 Replies

6. Shell Programming and Scripting

sed/awk : how to delete lines based on IP pattern ?

Hi, I would like to delete lines in /etc/hosts on few workstations, basically I want to delete all the lines for a list of machines like this : for HOST in $(cat stations.lst |uniq) do # echo -n "$HOST" if ping -c 1 $HOST > /dev/null 2>&1 then HOSTNAME_val=`rsh $HOST "sed... (3 Replies)
Discussion started by: albator1932
3 Replies

7. Shell Programming and Scripting

How to delete lines using sed?

<VirtualHost 192.168.1.158:80> DocumentRoot /home/ten ServerName ten.com </VirtualHost> <VirtualHost 192.168.1.158:80> DocumentRoot /home/sachin ServerName sachin.com </VirtualHost> <VirtualHost 192.168.1.158:80> DocumentRoot /home/yuvraj ServerName yuvraj.com... (5 Replies)
Discussion started by: tkmmelvin
5 Replies

8. Shell Programming and Scripting

Sed to delete lines that with the following

Hi, I'm very new to Sed and I have a very large file that contains data in the following way (*064) 1 4 10 (*064) simulation time = 0.12000E-05 (*064) 1 2 10 (*064) 1 3 10Essentially what I want to do it delete every line that starts with '(*064) 1'I tried the following, ... (2 Replies)
Discussion started by: lost.identity
2 Replies

9. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies

10. Shell Programming and Scripting

sed help - delete last 2 lines.

I have been reading through the sed one liners, trying to understand what is happening. # delete the last 2 lines of a file sed 'N;$!P;$!D;$d' The above will delete the last 2 line of a file. I tried analyzing what happens. And I got lost :( This is what I understood so far from the... (2 Replies)
Discussion started by: vino
2 Replies
Login or Register to Ask a Question