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-03-2021, 10:33 AM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Setting variables in awk according to matching pattern


How can I construct a pattern in `awk` then seh a value for a variable.

I would like to match the following patterns

Code:
^[[:space:]]*(#;!)+[[:space:]]*Mode: org$

^[[:space:]]*(//|@c)[[:space:]]*Mode: org$
 
Old 11-03-2021, 10:34 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
why do you open a new thread for this?
Code:
/pattern/ { var=value }
 
Old 11-03-2021, 11:15 AM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Basically to make the task simpler rather than reading through a lot of things.
 
Old 11-03-2021, 01:07 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,332
Blog Entries: 3

Rep: Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727
Or throw in a logical OR using the || operator.

Code:
/^[[:space:]]*(#;!)+[[:space:]]*Mode: org$/ \
|| /^[[:space:]]*(\/\/|@c)[[:space:]]*Mode: org$/ \
{ var=value }
Note that inside the pattern the slashes must be escaped.
 
Old 11-03-2021, 10:57 PM   #5
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Why does it not work like this?

Code:
 charcl='^[[:space:]]*([#;!]+|@c|\/\/)[[:space:]]*' 
 awk -v ccls="$charcl" \
    found { sub(/ccls/,""); print }' "$efile"
But I was unable to remove the comment characters.

Last edited by Faki; 11-04-2021 at 12:19 AM.
 
Old 11-03-2021, 11:08 PM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,332
Blog Entries: 3

Rep: Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727
Please show the AWK script in which that line is to be found so that the context is known.
 
Old 11-04-2021, 12:18 AM   #7
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Code:
capture ()
{
 local efile="$1"
 
 local begorg endorg charcl

 charcl='^[[:space:]]*([#;!]+|@c|\/\/)[[:space:]]*' 
 begorg="${charcl}"'Mode: org$'
 endorg="${charcl}"'# End of org$'
 
 awk -v ccls="$charcl" -v bego="$begorg" -v endo="$endorg" \
   '$0 ~ bego { found=1; next }
    $0 ~ endo { found=0; }
    found { sub(/ccls/,""); print }' "$efile"
}
Have also used

Code:
charcl='^[[:space:]]*([#;!]+)[[:space:]]*'
But the comment characters are not being removed.

Last edited by Faki; 11-04-2021 at 12:32 AM.
 
Old 11-04-2021, 01:18 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
when you use sub with a variable you must not use / :
Code:
found { sub(ccls,""); print }
 
Old 11-04-2021, 02:03 AM   #9
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Works very well, thank so very much. Is there a way to add an empty line between blocks (one block is identified by the lines enclosed within `begorg` and`endorg`)?

Last edited by Faki; 11-04-2021 at 02:05 AM.
 
Old 11-04-2021, 02:16 AM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,332
Blog Entries: 3

Rep: Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727Reputation: 3727
Yes, it would be with a print() or printf() function. Add that to the appropriate stanza.
 
Old 11-04-2021, 05:26 AM   #11
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Have used `print ""` to print a blank line. Introduced it on the line checking `endrsc`.

Code:
$0 ~ endrsc { insc=0; print "" }
 
  


Reply

Tags
awk, bash



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
vi search multiple pattern and remove only exact matching pattern amateurscripter Linux - Newbie 4 05-07-2018 01:19 PM
[SOLVED] Magical mystery file on ext3 fs: file is 3.8Gb according to ls, 61M according to du Chojin Linux - General 2 04-24-2015 03:54 AM
Matching patterns or partial pattern matching yaplej Programming 6 12-16-2012 10:21 AM
how to delete lines according to a pattern in awk Massimo34 Programming 4 01-26-2012 03:19 PM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM

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

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