Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

regex - AWK function to grab profiles for gnome-terminal

I want to be able to easily transport certain profiles of my gnome-terminal to another computer by using a sort of regex and filtering for the name. grep doesn't allow searching between newlines and I'm hoping awk (Technically mawk on Ubuntu) has that functionality. A profile looks like this:

[:c812de27-9bc9-414b-a263-4995c1ec775a]
background-color='rgb(31,36,40)'
palette=['rgb(106,115,125)', ...array of colors]
scroll-on-output=true
scrollbar-policy='never'
use-theme-colors=false
use-theme-transparency=false
visible-name='gh_dark'

These profiles are split by newlines when dumped via dconf

So far I've tried grep, but since that doesn't allow matching newlines (I found out after trying grep "[:.*].*visible-name:'gh_dark'"), I can't scope out several. I looked into the -A flag, but since there is no way to know how many lines a specific profile will be, it doesn't seem practical.

What I sort of want, given the name, is output like above where it is easy to see the full profile configuration

I want to know if there's a way to use awk to easily sort through these by grabbing the ids (at the top) along with the names to easily figure out which ones to export individually (which require ids) or just pull them out directly and place them in a file


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use GNU grep like so:

grep -Pzo '(?s)[:.*?visible-name.*?
' in_file

Here, grep uses the following options:
-P : Use Perl regexes.
-o : Print the matches only (1 match per line), not the entire lines.
-z : Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Thus, you can match newlines in the input.

(?:s) : Enable the s pattern-match modifier, to allow . to match a newline.

SEE ALSO:
grep manual
perlre - Perl regular expressions


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...