LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-22-2016, 02:02 AM   #1
simplybharath
LQ Newbie
 
Registered: Apr 2016
Posts: 6

Rep: Reputation: Disabled
sed/awk help in removing the section of file if it contains regex


Hi i need to remove these contents from a file only if it contains line related_pin : "sbss_pclk_div4_5_SUBRATE_20checkpin1"

timing () {
related_pin : "sbss_pclk_div4_5_SUBRATE_20checkpin1" ;
timing_type : min_pulse_width ;
mode(etm_modes, "FUNC_SUB_20");
/* comment : from another design; */
fall_constraint( scalar ){
values ( "0.024838");
}

} /* end of arc sbss_pclk_div4_5_SUBRATE_20checkpin1


how to delete this section only if body of the section contains such line ?
 
Old 04-22-2016, 03:00 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please show what you have tried?
 
Old 04-22-2016, 03:27 AM   #3
simplybharath
LQ Newbie
 
Registered: Apr 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Angry

Hi Grail

i was doing it in vim , but there are lot of files and lot of regex expressions which was time consuming , there are multiple timing () lines in my file so i tried using tac filename | sed /} \/\* end of arc sbss_pclk_div4_5_SUBRATE_20checkpin1/timing \(\) {/d' | tac , which seems to be non-working
 
Old 04-22-2016, 05:30 AM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Ok. Consider this as infile:

Code:
timing () {
related_pin : sbss_pclk_div4_1_SUBRATE_20checkpin1 ;
timing_type : min_pulse_width ;
mode(etm_modes, FUNC_SUB_20);
/* comment : from another design; */
fall_constraint( scalar ){
values ( 0.024838);
}

} /* end of arc sbss_pclk_div4_1_SUBRATE_20checkpin1

timing () {
related_pin : sbss_pclk_div4_5_SUBRATE_20checkpin1 ;
timing_type : min_pulse_width ;
mode(etm_modes, FUNC_SUB_20);
/* comment : from another design; */
fall_constraint( scalar ){
values ( 0.024838);
}

} /* end of arc sbss_pclk_div4_5_SUBRATE_20checkpin1



timing () {
related_pin : sbss_pclk_div4_5_SUBRATE_10checkpin1 ;
timing_type : min_pulse_width ;
mode(etm_modes, FUNC_SUB_20);
/* comment : from another design; */
fall_constraint( scalar ){
values ( 0.024838);
}

} /* end of arc sbss_pclk_div4_5_SUBRATE_10checkpin1
What we want to remove is in bold.

After i run this little script (simplyb.sh, 5 lines) I get this result:
Code:
./simplyb.sh 
timing () {
related_pin : sbss_pclk_div4_1_SUBRATE_20checkpin1 ;
timing_type : min_pulse_width ;
mode(etm_modes, FUNC_SUB_20);
/* comment : from another design; */
fall_constraint( scalar ){
values ( 0.024838);
}

} /* end of arc sbss_pclk_div4_1_SUBRATE_20checkpin1




timing () {
related_pin : sbss_pclk_div4_5_SUBRATE_10checkpin1 ;
timing_type : min_pulse_width ;
mode(etm_modes, FUNC_SUB_20);
/* comment : from another design; */
fall_constraint( scalar ){
values ( 0.024838);
}

} /* end of arc sbss_pclk_div4_5_SUBRATE_10checkpin1
The block with "sbss_pclk_div4_5_SUBRATE_20checkpin1" is gone.
But... it would be helpful to see more of the actual file.

I used grep and sed mainly.

Best regards,
HMW
 
Old 04-22-2016, 05:54 AM   #5
simplybharath
LQ Newbie
 
Registered: Apr 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi HMW

your assumptions are right , my basic problem is how to remove the section of text from a file delineated by some start and end regex only if a line inside that section of text matches another regex , so i want to delete below section only if this line matches related_pin : sbss_pclk_div4_1_SUBRATE_20checkpin1 ; , i was trying to find a generic solution for such thing

timing () {
related_pin : sbss_pclk_div4_1_SUBRATE_20checkpin1 ;
timing_type : min_pulse_width ;
mode(etm_modes, FUNC_SUB_20);
/* comment : from another design; */
fall_constraint( scalar ){
values ( 0.024838);
}

} /* end of arc sbss_pclk_div4_1_SUBRATE_20checkpin1
 
Old 04-22-2016, 06:12 AM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by simplybharath View Post
Hi HMW

your assumptions are right , my basic problem is how to remove the section of text from a file delineated by some start and end regex only if a line inside that section of text matches another regex , so i want to delete below section only if this line matches related_pin : sbss_pclk_div4_1_SUBRATE_20checkpin1 ; , i was trying to find a generic solution for such thing

timing () {
related_pin : sbss_pclk_div4_1_SUBRATE_20checkpin1 ;
timing_type : min_pulse_width ;
mode(etm_modes, FUNC_SUB_20);
/* comment : from another design; */
fall_constraint( scalar ){
values ( 0.024838);
}

} /* end of arc sbss_pclk_div4_1_SUBRATE_20checkpin1
Ok. If the text looks EXACTLY like above, then my script obviously does the job.

I used grep to get the starting line number (hint: ((startline=startline-1))), and then sed to remove lines beginning from there until the end of the block.

Best regards,
HMW
 
Old 04-22-2016, 08:18 AM   #7
simplybharath
LQ Newbie
 
Registered: Apr 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi HMW

can you let me know the contents of the script ?

Regards
Bharath
 
Old 04-22-2016, 09:17 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You have been given suggestions, so at least make an attempt and I am sure HMW will gladly show you how he would have done it.
 
1 members found this post helpful.
Old 04-22-2016, 09:52 AM   #9
simplybharath
LQ Newbie
 
Registered: Apr 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
i tried getting the line no using grep -n , but can sed delete can be used like this sed /<line no>/,/end line regex/ ? if not how to get the end line no assuming there are multiple matches for that particular regex as such line no is relative to start line ?

Regards
Bharath
 
1 members found this post helpful.
Old 04-22-2016, 10:13 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
the usual way (at least for me) to set the delimiter as record separator and you only need to keep / drop the specified records. This can be easily implemented in awk:
Code:
awk -v RS=timing '/pattern/' inputfile
you only need to adjust it a bit....
 
Old 04-22-2016, 11:52 AM   #11
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by simplybharath View Post
i tried getting the line no using grep -n , but can sed delete can be used like this sed /<line no>/,/end line regex/ ? if not how to get the end line no assuming there are multiple matches for that particular regex as such line no is relative to start line ?

Regards
Bharath
I'm sorry, but I simply don't understand what you mean. It is probably the language barrier, but I cannot make head or tails out of this.

I'll try to break down my method for you anyway:
  • Find the line with the string you search for
  • Based on that line, set a starting point
  • From the starting point, remove the lines you want to remove

That's how I did it, and it works with your example. You still, however, have not showed the content of the actual file - only what you want to remove from said file - so my method may or may not work properly in real life.

Best regards,
HMW
 
  


Reply



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] sed regex and removing 'whitespace' uncle-c Linux - Newbie 5 04-23-2012 08:25 AM
[SOLVED] Any grep, sed or awk gurus with regex familiarity? I need some help. bcrawl Linux - Newbie 19 01-19-2011 07:52 PM
[SOLVED] sed/awk : remove section from file vrusu Linux - Newbie 3 10-26-2010 08:49 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Newbie SED / AWK / Regex command help request Critcho Linux - Newbie 10 03-19-2007 11:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:07 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