LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-02-2021, 01:39 AM   #16
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled

I have another adaptation

Code:
capture-org ()
{
 local efile="$1"
 awk '/## mode/{flag=1;next} /## # End/{flag=0} flag' "$efile" |
  tr -d /#/ | awk '$1=$1'
}
But I am getting more lines being printed when I pass the same file where I have the code and thus the function `capture-org`. This is because the script gets a match with `## mode` in the code part (i.e. the matches are matching occurrence anywhere on the line).

I need to match exactly as with the patterns ^[[:space:]]*## mode: org$ and ^[[:space:]]*## # End of org$

Last edited by Faki; 11-02-2021 at 02:09 AM.
 
Old 11-02-2021, 02:30 AM   #17
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
I need to match the exact patterns '^[[:space:]]*## mode: org$' and '^[[:space:]]*## # End of org$', rather than matching anywhere in the lines.
 
Old 11-02-2021, 03:09 AM   #18
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
AWK statements are just if-then clauses written in a compact manner with several built-in abbreviations.

If you write what you have a little differently,
Code:
capture-org ()
{
 local efile="$1"
 awk  '/## mode/ { flag=1; next } 
       /## # End/ { flag=0; } 
       flag { sub(/^[[:space:]]*#[[:space:]]*/,"); print }' \
       "$efile"'
}
then can you spot where the new patterns go?
 
Old 11-02-2021, 05:57 AM   #19
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Have made these changes.


Code:
  
awk '/^[[:space:]]*## mode: rec$/    { flag=1; next } 
     /^[[:space:]]*## # End of rec$/ { flag=0; } 
     flag { sub(/^[[:space:]]*#[[:space:]]*/,""); print }' "$efile"
But I am getting the following but need the starting "# " removed.

Code:
# HDG: Handling function argument parsing with getopts
# Rmk: No call to {shift} is required with getopts
# Rmk: No hyphen {-} required when searching option names
# + Case patterns do not start with option hyphen {-} because
# + getopts strips off the hyphen and makes the value of {arg}
# + to be just the option letter.
# Rmk: Separating options from non-options with --
# + {getopts} stops processing options when the argument is not
# + defined as an option in {shortopts}; or if the argument is
# + "--", which explicitly terminates the list of options.
# Rmk: Using -- as value to an option
# + An option value can be -- without it being considered a
# + separator between options and non-options.
# + Example { edvart-getopts -g "--" }.
# Rmk: Explicitly testing for {--}
# + There is no need to test for {--} when using {getopts}.
# HDG: Silent Error Reporting Mode (SERM) in getopts
# Rmk: Detects warnings without printing built-in messages.
# Rmk: Enabled by colon {:} as first character in shortopts.
 
Old 11-02-2021, 06:07 AM   #20
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
ok, so you need to formulate that sub command (or use more than one) to remove unwanted parts of the line.
 
Old 11-02-2021, 06:28 AM   #21
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Disregard

Last edited by Faki; 11-02-2021 at 06:53 AM.
 
Old 11-02-2021, 06:32 AM   #22
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
You're close. The method for that is a little non-obvious:

Code:
 
 begrec='^[[:space:]]*## mode: rec$'
 endrec='^[[:space:]]*## # End of rec$'
 awk -v begr="$begrec" -v endr="$endrec" \
   '$0~begr { flag=1; next } 
    $0~endr { flag=0; } 
    flag { sub(/^[[:space:]]*#[[:space:]]*/,""); print }' "$efile"
That comes down to the shortcuts and abbreviations that AWK allows. The short annotation for $0~/foo/ is simply /foo/. Be sure to check the reference manual for the AWK scripting language periodically during this task: "man awk"

See also : https://www.grymoire.com/Unix/Awk.html
 
  


Reply

Tags
awk, bash, sed



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] Pull sections of text from a file and create a new file? soupmagnet Linux - Newbie 3 12-27-2012 04:46 PM
Remove sections of a xml file with sed viniciusandre Linux - Software 2 04-20-2009 01:18 PM
Shell Script or perl help. to write sections of a log to a tmp file for mailing pobman Programming 2 02-02-2009 03:30 PM
Capturing video with Cinelerra works, capturing video with Xawtv doesn't! seaelf Slackware 0 06-27-2004 05:18 PM

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

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