LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-28-2023, 01:56 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Printing bash function definition until a blank line is encountered


I have a pattern ptrn that matches a bash function definition.

Code:
sed -n -E "$ptrn" "$flnm"
In addition to the function definition, I can print consecutive lines until the first blank line in encountered (in order to capture the a corresponding documentation string), which I can do with

Code:
/${ptrn}/,/^[[:blank:]]*$/p
Here is an example bash function definition with documentation strings.

Code:
firefly-wht ()  # TST  Some Descriptiion
 {
  ## FN firefly-wht  [first docstring]
  ## DS   Print parameters in white colour  [second docstring]

  local -r rst="$( tput sgr0 )"
The difficulty arises when trying to colour the function definition with a blue foreground, and comments with a white foreground. The respective colour escape sequences are stored in "$blu" and "$wht".

Because I cannot do something like the following, what possibilities exist ?

Code:
## Pattern matching function definition till first empty line
rp="/${ptrn}/,/^[[:blank:]]*$/p"

## Pattern motching a comment
km_ere="[[:blank:]]*(##|;;|!!|//|@c)[[:blank:]]+"

## Pattern colouring definition blue, and comments white 
kp="s%${ptrn}%${blu}&%p ; s%${km_ere}%${wht}&%p"

sed -n -E "$rp ; $kp" "$flnm"

Last edited by Faki; 03-28-2023 at 04:47 PM.
 
Old 03-28-2023, 05:31 PM   #2
scottieH
Member
 
Registered: Mar 2021
Posts: 58

Rep: Reputation: Disabled
Why sed and not awk?

I'm curious why you chose sed.
The basic trouble is that you want to change the output, based upon the input. I.e. you have 2 different conditions to look for.
Sed only makes one pass through the file, the "Change To" pattern cannot change during the run.

You need a bit of intelligence to interpret the line and act accordingly. For convenience, you could still use sed to get the initial lines, and awk to output them. Or, awk can do the whole thing.

Ex:
Code:
sed -n -E /${ptrn}/,/^[[:blank:]]*$/p | awk ...
Let's start with this idea.
You can pass variable to awk with the -v option:
Code:
awk -v blu="$blu" -v wht=$wht ...
awk command are enclosed in '{}':
Code:
cat <my_file> | awk '{print $0}'
Or, in this case, a better example would be:
Code:
awk '{print $0}' <my_file>
Inside awk, the arguments $0, $1, $2 represent the input. $0 is the input line, unchanged. $1 is the first 'word', $2 the second, etc. Where a word is split from the input line based on the delimiter. By default it is white space. In your case, I would guess you are only interested in $0.

You can perform pattern matching inside awk. You say your regex is:
Code:
[[:blank:]]*(##|;;|!!|//|@c)[[:blank:]]+
First, confirm that this the correct type of regex for your version of awk (could be awk, gawk, nawk, etc.)

If command in awk are nearly the same as any other language:
Code:
if ($0 ~ /<regex>/) {print blu $0 wht} else {print $0}
Note: the tilde (~) means match the pattern. The slashes (/) represent a pattern, not a literal string. Not match is !~.
To match a literal string, use the double quote (") and double equal (=):
Code:
if ( $0 == "Hello) ...
Not equal would be !=

Important: Read the man pages for awk! There is a lot to it. Esp look at the -v -F and -f options. And the if command, expressions and print commands.


Yo can also make an "awk script" (see the -f option) that gives you a bit more flexibility. With this, you could, conceivably do the entire task in awk.

Code:
{
if ($0 ~ /start_ptrn/) {
  print_flag=1

  ...your color matching stuff here ...
 
 if (print_flag=1) ...print my output here...

 if ($0 ~ /end_ptrn/) print_flag=0

}
If you choose this option, definitely do it in an awk script it will be so much easier to maintain and debug.

Remember, read the man page! I've only outlined a rough template, no guarantees that it will work as I've written it. Hopefully it is clear enough to get you started.
 
Old 03-28-2023, 06:51 PM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
It does help a lot. I already had an implementation to colour two different patterns in sed, and thought I could adapt it. But they were two separate patterns for two different type of lines. Only after capturing a region, did I realise the serious work involved.

Last edited by Faki; 03-28-2023 at 06:56 PM.
 
  


Reply

Tags
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
Latex definition giving LaTeX Error: Environment definition undefined Faki Linux - Newbie 1 04-24-2022 05:23 AM
[SOLVED] where to find function definition from function prototype in header file doughyi8u Programming 1 09-05-2014 01:46 PM
[SOLVED] Display lines before and after string until blank line cosminel Linux - Newbie 38 12-17-2013 09:40 AM
Error: Cannot parse function definition from ‘ hello()’ in Mytest.xs, line 9 nikole Linux - Software 1 04-09-2010 06:56 AM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM

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

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