Home > Enterprise >  Bash sed replace everything in a textfile until the apperance of the first alphabetic character
Bash sed replace everything in a textfile until the apperance of the first alphabetic character

Time:05-11

Can anybody help me with this? I have a bunch of text-files, that I want to alter. They all start with something else but a normal alphabetical character. I now want to delete everything before the appearnace of a alphabetical character, so that they start with their title,keeping everything else following intact.

here's an example:

=================================================================================

TITLE OF SONG

Text text text
Text text text

ReFrain:
Text text text

written 16.08.1998

And I want it to look like this

TITLE OF SONG

Text text text
Text text text

ReFrain:
Text text text

written 16.08.1998

I tried doing it like this:

for f in *.txt; do sed 's/^.*[a-zA-Z]/^.[a-zA-Z]/g'; done

It is not working

I'm fairly new to this, so please bear with me.

best Stef

CodePudding user response:

Using sed

$ sed 'N;N;s/\("\).*\n\([[:alpha:]]\ \)/\1\2/' input_file
"TITLE OF SONG

Text text text
Text text text

ReFrain:
Text text text

written 16.08.1998"

or

$ sed ':a;N;s/\("\)[^[:alpha:]]*\n\([[:alpha:]]\)/\1\2/;ba' input_file
"TITLE OF SONG

Text text text
Text text text

ReFrain:
Text text text

written 16.08.1998"

CodePudding user response:

Perl is handy here:

perl -0777 -i -pe 's/^[^[:alpha:]] //s' file ...

The -0777 option causes perl to slurp the entire file into the default variable,
and the s/// command removes all leading non-alpha characters.

  • Related