Finding a match and deleting the line above to it..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding a match and deleting the line above to it..
# 1  
Old 12-13-2012
Computer Finding a match and deleting the line above to it..

Hi All,
I have a file with the data:
Code:
Sun is Hot
Moon is cool
;
 
-- Mon Sep 10 08:54:10 CDT 2012
-- Mon Sep 11 08:54:10 CDT 2012
-- Mon Sep 12 08:54:10 CDT 2012
revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';

I want to remove the line above the line "revoke connect from SREE;" if the above line starts with only "--" otherwise don't delete. This should continue till all the lines above the revoke line, those start with "--" got deleted. If any space or any other character comes it should stop deleting.

Finally my output should look like:
Code:
Sun is Hot
Moon is cool
;
 
revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';

Please help me with this.

Thanks!!

Sreenivas

Last edited by raosr020; 12-13-2012 at 02:37 AM.. Reason: Minor changes
# 2  
Old 12-13-2012
Try

Code:
$awk '{if($0 ~ /^--/){A[++a]=$0}else if(a && $0 !~ /revoke connect from SREE/){for(i=1;i<=a;i++){print A[a]};a=0}else{print ;a=0}}' file
Sun is Hot
Moon is cool
;

revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';

# 3  
Old 12-13-2012
Code:
awk 'END{if(p)printf "%s",p} /^--/{p=p $0 RS; next} /revoke connect from SREE/{p=x} p{$0=p $0; p=x}1' infile

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-13-2012
Hi Pamu,

Thanks for the reply!!

But with that code any other revoke connect lines also getting deleted (ex: revoke connect from CALU is also getting deleted).

My original file:

Code:
Sun is Hot
Moon is cool
;

-- Mon Sep 10 08:54:10 CDT 2012
-- Mon Sep 11 08:54:10 CDT 2012
-- Mon Sep 12 08:54:10 CDT 2012
revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';
 
-- Mon Sep 12 08:54:10 CDT 2011
revoke connect from CALU;
delete from = 'CALU';
grant connect to CALU with 'calendar';

After running the below command:
Code:
awk '{if($0 ~ /^--/){A[++a]=$0}else if(a && $0 !~ /revoke connect from SREE/){for(i=1;i<=a;i++){print A[a]};a=0}else{print ;a=0}}' file

Output is as follows:
Code:
Sun is Hot
Moon is cool
;

revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';
 
-- Mon Sep 12 08:54:10 CDT 2011
delete from = 'CALU';
grant connect to CALU with 'calendar';

In above ouput
Code:
revoke connect from CALU;

line is missing. That shouldn't happen. Please correct the code.


And i cann't redirect this output to a temporary file. These changes should happen to the original file itself.

Please help me in getting these two requirements solved.

Thanks,
Sreenivas

---------- Post updated at 09:21 PM ---------- Previous update was at 06:50 PM ----------

Hi Scrutinizer

Thanks for the reply!!

Your code is not working. Please suggest me the solution based on the above requirement.

Thanks,
Sreenivas

Last edited by raosr020; 12-13-2012 at 11:46 AM.. Reason: changes made
# 5  
Old 12-13-2012
What isn't working? I get:

Code:
Sun is Hot
Moon is cool
;

revoke connect from SREE;
delete from = 'SREE';
grant connect to SREE with 'fastcar8';
 
-- Mon Sep 12 08:54:10 CDT 2011
revoke connect from CALU;
delete from = 'CALU';
grant connect to CALU with 'calendar';

What is your OS and version?
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 12-14-2012
Hi
Scrutinizer Image Image

Awesome!! Thanks alot!!
Your code is working perfect. Previously i used it wrongly. Now i have corrected my mistake. Its working fine.

But i have a concern about how to make the changes in the original file without redirecting stdout to some temporary new file.

Regards,
Sreenivas
# 7  
Old 12-14-2012
Hi, you are welcome. What is wrong with redirecting to a new file? You can rename it to the old file afterwards when satisfied with the result. It is the safest way..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

2. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

3. UNIX for Dummies Questions & Answers

Deleting rows where the value in a specific column match

Hi, I have a tab delimited text file where I want to delete all rows that have the same string for column 1. How do I go about doing that? Thanks! Example Input: aa 1 aa 2 aa 3 bb 4 bc 5 bb 6 cd 8 Output: bc 5 cd 8 (4 Replies)
Discussion started by: evelibertine
4 Replies

4. UNIX for Dummies Questions & Answers

Deleting files based on Substring match

In folder there are files (eg ABS_18APR2012_XYZ.csv DSE_17APR2012_ABE.csv) . My requirement is to delete all the files except today's timestamp I tried doing this to list all the files not having today's date timestamp #!/bin/ksh DATE=`date +"%d%h%Y"` DIR=/data/rfs/... (9 Replies)
Discussion started by: manushi88
9 Replies

5. Shell Programming and Scripting

help with finding text and deleting line

HI All, I need to search for a particular pattern input by the user in order to delete the line. My username.txt has username@email.com:John:149.0.3.4:1 username1@email.com:Harry:149.0.3.4:1 username1@email.net:Alex:149.0.3.4:1 username1@email.edu:Nemo:149.0.3.4:1 The program i written ... (3 Replies)
Discussion started by: ichar
3 Replies

6. Shell Programming and Scripting

Finding a flatfile & deleting first line

I have a small script where I want to see if a file exists & then delete the first line from it. I have code to help me find if the file exists, but I am unsure as to how to then take in the answer and remove the first line from the flatfile: This is what I have so far just to output if the... (3 Replies)
Discussion started by: fatalxkiss
3 Replies

7. Shell Programming and Scripting

Finding duplicate lines and deleting folders based on them

Hi, I have research data, which is organized to 100 folders numbered 00-99. I have many sets of 100 folders, for different values of initial parameters. For some reason, the computer that ran the program to gather the data, didn't always create a unique seed for each folder. I anticipated that... (1 Reply)
Discussion started by: Jopi
1 Replies

8. Shell Programming and Scripting

Is there an efficient way in finding and deleting files?

Hi I have a script to find and delete the files which are say, noDaysOld, I am interested to find the number of such files I am fniding for deleting and then deleting it. So, the script I wrote, first finds the number of such files and then deletes, clearly this is two different steps. ... (3 Replies)
Discussion started by: guruparan18
3 Replies

9. Shell Programming and Scripting

Deleting / finding files older than X days missess a day

Hi When trying to find and delete files which are, say, 1 day, the find command misses a day. Please refer the following example. xxxd$ find . -type f -ctime +1 -exec ls -ltr {} \; total 64 -rw-rw-r-- 1 oracle xxxd 81 Apr 30 11:25 ./ful_cfg_tmp_20080429_7.dat -rw-rw-r-- 1... (4 Replies)
Discussion started by: guruparan18
4 Replies

10. UNIX for Dummies Questions & Answers

Help with finding certain files, and then deleting

I must really be bad at life today, because I couldn't even find a search option on the forums before asking for help, so I apologize if this is already listed somewhere. I'm pretty new to UNIX, and basically only know enough to be dangerous. I have been appointed a task to basically go to a... (2 Replies)
Discussion started by: Puck
2 Replies
Login or Register to Ask a Question