D. Ben Knoble
D. Ben Knoble

Reputation: 4683

Extract text between two lines

I am building a bash-script to visualize the various fonts available for figlet using figlist.

figlist provides output like this:

Default font: standard
Font directory: /usr/local/Cellar/figlet/2.2.5/share/figlet/fonts
Figlet fonts in this directory:
3-d
3x5
5lineoblique
[...]
twopoint
univers
usaflag
weird
whimsy
Figlet control files in this directory:
646-ca
646-ca2
646-cn
646-cu
[...]
tsalagi
upper
ushebrew
uskata
utf8

The [...]s represent snipped output. My desired output is the following:

3-d
3x5
5lineoblique
[...]
twopoint
univers
usaflag
weird
whimsy

That is, I want the font names. I cannot guarantee the output format, but I don't want any control files, and I don't want the informational lines. I'm not sure, but I suspect all fonts must have one word names, so a regex solution might be possible. However, the control files have a similar format.

Current (hard-coded) solution:

read -a fonts <<<$(figlist | tail -n +4 | head -n 163)

This provides what I want, but requires that the length of the font list never changes, which I don't want.

I would prefer a solution in bash/standard commands/builtins, as that is the language in which I am writing the script, but if it can be obtained via a python one-liner or something similar (e.g. python -c <some command>) then that is acceptable as well.

Upvotes: 1

Views: 381

Answers (3)

hek2mgl
hek2mgl

Reputation: 158100

Update:

A shorter and preferable awk alternative would be:

figlist | awk '/Figlet/{p=!p;next}p'

I recommend to use in favour of the below sed command.


Original answer:

You can use sed:

figlist | sed -n '/Figlet fonts/,/Figlet/{//!p;}'

Example:

Print the current user name with each installed figlet font:

figlist \
  | sed -n '/Figlet fonts/,/Figlet/{//!p;}' \
  | while read -r font ; do
        echo "font: ${font}"
        figlet -f"${font}" "$(whoami)"
    done

Upvotes: 1

Scheff&#39;s Cat
Scheff&#39;s Cat

Reputation: 20141

As this question was tagged also, I feel free to add an awk solution:

/^Figlet fonts/ { on = 1 ; next }
/^Figlet control/ { on = 0 ; next }
on { print $0 }

Test (on bash, cygwin, Windows 10):

$ echo 'Default font: standard
> Font directory: /usr/local/Cellar/figlet/2.2.5/share/figlet/fonts
> Figlet fonts in this directory:
> 3-d
> 3x5
> 5lineoblique
> [...]
> twopoint
> univers
> usaflag
> weird
> whimsy
> Figlet control files in this directory:
> 646-ca
> 646-ca2
> 646-cn
> 646-cu
> [...]
> tsalagi
> upper
> ushebrew
> uskata
> utf8
> ' | awk '/^Figlet fonts/ { on = 1 ; next } /^Figlet control/ { on = 0 ; next } on { print $0 }'
3-d
3x5
5lineoblique
[...]
twopoint
univers
usaflag
weird
whimsy

$

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Python one-liner:

figlist | python -c "import sys,re; fonts=re.search(r'Figlet fonts.+?:(.*)(?=Figlet control)',sys.stdin.read(), re.DOTALL); print(fonts.group(1).strip())"

The output:

3-d
3x5
5lineoblique
[...]
twopoint
univers
usaflag
weird
whimsy

That's all, folks

Upvotes: 2

Related Questions