How can I remove the leading “Modeline” text with sed?

0
0
Asked By QuietMaple47 On

I'm generating an X11 modeline in Bash with gtf and trying to process its output. The line looks like this:

Modeline "1440x800_60.00" 93.80 1440 1512 1664 1888 800 801 804 828 -HSync +Vsync

I extract the text between quotes into MODENAME, then run sed on it:

MODEName=$(echo "$LINE" | awk -F'"' '{print $2}')
MODELINE=$(echo "$MODENAME" | sed 's/^s*Modeline.+$//')

However, the text is not being removed. What is wrong with the regular expression or the command?

3 Answers

Answered By SilverPine22 On

Your `MODENAME` variable contains only the text inside the quotes, such as `1440x800_60.00`. It no longer contains `Modeline`, so there is nothing for the sed command to remove. If you want the mode name, your existing awk extraction is enough:

`MODENAME=$(awk -F'"' '{print $2}' <<< "$LINE")`

If you want to strip `Modeline` from the full gtf output, apply sed to `LINE` instead.

Answered By CopperLark8 On

There are two separate issues. First, standard sed uses basic regular expressions, where `+` is normally a literal plus sign. Use `.*` or enable extended regular expressions with `sed -E`:

`sed -E 's/^[[:space:]]*Modeline.+$//'`

But this expression removes the entire matching line, not just the word `Modeline`. To remove the prefix and keep the rest, use:

`sed -E 's/^[[:space:]]*Modeline[[:space:]]*//'`

Also, `s` is not portable in sed; use `[[:space:]]` instead.

Answered By BrightOtter6 On

A compact approach is to extract the quoted mode name directly from gtf output:

`MODENAME=$(gtf "$WIDTH" "$HEIGHT" "$RATE" | sed -nE 's/^[[:space:]]*Modeline[[:space:]]+"([^"]+)".*$/1/p')`

This matches the Modeline line and prints only the value inside the quotes. Also quote variables when expanding them, and prefer `printf` over `echo` when predictable output matters.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.