delete line with conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete line with conditions
# 1  
Old 05-24-2011
delete line with conditions

Hi Dude,
I am struck up with another problem,Pls help me
I am new to perl , I have little knowledge only.

The scenario is
Code:
 
[abc:/def/ghi/jkl/mno]
 ssenthil = rw
 [abc:/def/ghi/jkl/mno/xyz]
 anilkg = rw
 [abc:/def/ghi/jkl/mno/pqr]
 @group1 = rw
 anilkg = rw
ssenthil = rw

i need output as below , I want to find "anilkg" and delete the preceding line that starts with "[" and ends with "]" . the condition is only one user is there. If there are any groups that start with "@" or any other user below the searching string should delete only the "searching string" and the output as below

Code:
 [abc:/def/ghi/jkl/mno]
 ssenthil = rw
 [abc:/def/ghi/jkl/mno/pqr]
 @group1 = rw
ssenthil = rw

will you pls suggest me?

Rgds
Anil.G

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 05-24-2011 at 07:05 AM.. Reason: code tags
# 2  
Old 05-24-2011
Lines don't start with '[' but with space in your examples.
# 3  
Old 05-24-2011
there are no spaces , the line either starts with alphabetic or "[" or "@"
# 4  
Old 05-24-2011
As i understand what you want, you can't be sure of the place of anilkg in a line it's not so easy because you must read 3 lines and delete line 1 and 2 only if line 3 begin with '[' and line 2 begin with anilkg.

So is it ok to do something like that ? :

1) Pass 1 : delete all lines with anilkg
2) Pass 2 : on the resulting output delete all line with '[' if next line also is '['

---------- Post updated at 12:21 PM ---------- Previous update was at 12:18 PM ----------

Quote:
Originally Posted by anil8103
there are no spaces , the line either starts with alphabetic or "[" or "@"
So please paste the real output that must be used, because the output on the first post have spaces at start of some lines.
# 5  
Old 05-24-2011
thats fine,, on the result delete the first line that starts with "[" and not the second line.

---------- Post updated at 03:59 PM ---------- Previous update was at 03:55 PM ----------

Input file

..........................................
[abc:/def/ghi/jkl/mno]
ssenthil = rw
[abc:/def/ghi/jkl/mno/xyz]
anilkg = rw

[abc:/def/ghi/jkl/mno/pqr]
@group1 = rw
anilkg = rw
ssenthil = rw
[abc:/def/ghi/jkl/mno/pqr]
@group1 = rw
ssenthil = rw

output

..............................................
[abc:/def/ghi/jkl/mno]
ssenthil = rw
[abc:/def/ghi/jkl/mno/pqr]
@group1 = rw
ssenthil = rw
[abc:/def/ghi/jkl/mno/pqr]
@group1 = rw
ssenthil = rw
# 6  
Old 05-24-2011
Hi,

Then i'll give a solution but not in perl.

Code:
sed "/anilkg/d" inputfilename | sed '$!N; /^\[.*\n\(\[.*\)/!P; D'

Now if you really need a perl solution it can be done also, but i don't have time do think about one now Smilie
# 7  
Old 05-24-2011
Hi,

This is what I understood, tell me if I'm wrong:

1.- Delete line found with user 'anilkg'.
2.- Delete previous line if the next one begins with '[' and ends with ']', or it is a blank line.
3.- Delete next line if it is blank.

Here you have a 'Perl' script with previous conditions:
Code:
$ cat script.pl
use strict;
use warnings;
use autodie;

@ARGV == 1 or die "Usage: perl $0 input-file\n";
open my $fh, "<", $ARGV[0];
my @prev_lines = ();
my $flag;

while ( <$fh> ) {
    chomp;
    if ( /^(?i:anilkg)/ ) {
        $flag = 1;
        next;
    } else {
        push @prev_lines, $_;
    }  

    if ( $flag && ( /^\[.*\]$/ || /^\s*$/ ) ) {
        pop @prev_lines;
        pop @prev_lines;
        push @prev_lines, $_ unless /^\s*$/;
    }  
    $flag = 0;
}

while ( @prev_lines ) {
    print +(shift @prev_lines), "\n";
}
$ perl script.pl infile
(Output suppressed).

Regards,
Birei

Last edited by birei; 05-24-2011 at 06:31 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Mismatched free() / delete / delete [] line no missing

Could you tell me the possibilities of the reason to get the Mismatched free() / delete / delete . I unable to see the line no in the valgrind report. it displays the function name. with that function name, I am not able to find where exactly the issue is there.I am getting the Mismatched free()... (3 Replies)
Discussion started by: SA_Palani
3 Replies

2. Shell Programming and Scripting

Using sed to delete one regular pattern with conditions

Hi All , I am having a file like this INPUT FILE ############################### addfd_mjlala kksks sksks ks annsns_bbox_2 (sksksk ksks ) adnndn_nsns_bbox_3 (( jsjsdj sjsj ) malm_dkdm lsls lsl mdndk_mkmd_dkd_bbox_4_kdkd ksksk skksk_bbox_jsj_KSK ((jsjsj jsjsj )... (2 Replies)
Discussion started by: kshitij
2 Replies

3. Shell Programming and Scripting

Delete rows with conditions

Hi everyone, I will appreciate a lot if anyone can help me about a simple issue. I have a data file, and I need to remove some rows with a given condition. So here is a part of my data file: 5,14,1,3,3,0,0,-0.29977188269E+01 5,16,1,4,4,0,0,0.30394279900E+02... (4 Replies)
Discussion started by: hayreter
4 Replies

4. Shell Programming and Scripting

Help - delete content inside square brackets under conditions

I have the file sed1.txt and I need to strip the brackets (]) and content inside them only when I have two or three letters followed by a colon. for example,it may be any letter, not just abc ] ] #-- cat sed1.txt 1 ] FISICA 2 ]PORTUGUES 3 ] ]MATEMATICA 4 ]]INGLES ] 5 ]QUIMICA 6... (2 Replies)
Discussion started by: dperboni
2 Replies

5. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

6. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

7. Shell Programming and Scripting

Delete line with match and previous line quoting/escaping problem

Hi folks, I've list of LDAP records in this format: cat cmmac.export.tmp2 dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc cmmac: 00:13:11:36:a5:06 dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc cmmac:... (4 Replies)
Discussion started by: tomas.polak
4 Replies

8. Shell Programming and Scripting

awk line with two conditions

Hi there, I wanna define a variable 'tempbase'. Therefore I read a text file "base.out". "base.out" contains a list with four columns. 'tempbase' is the 4th entry in the line, where the first entry is equal to the predefined variable $orb1 and the second entry is equal to $orb2. I wrote the code... (2 Replies)
Discussion started by: friend
2 Replies

9. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

10. UNIX for Dummies Questions & Answers

creating conditions for making a new line?

Basically I want to change this: a:b c:d:e f:g h:i:j k:l into a:b c d:e f:g h i:j k:l so like if there is two :'s in one line making the first into a new line. If anyone knows how to do this I would be very appreciative! (9 Replies)
Discussion started by: Audra
9 Replies
Login or Register to Ask a Question