sub except the first pattern that match

Lucil120

I know how to sub in awk, gred, sed but don't know how to sub except the first match. I searched in doc but can't find the right command to do it. i have a simple file like this:

tongtha AL_Vcs abcdef
tongtha AL_Vcs abcdef
tongtha AL_Vcs abcdef

tuanngu AL_Ptime HEO
tuanngu AL_Ptime HEO

vuau AL_Ptime E2x
vuau AL_verdi_b E2x
vuau AL_TShell EU2HL

and this is my desire

tongtha AL_Vcs abcdef
        AL_Vcs abcdef
        AL_Vcs abcdef

tuanngu AL_Ptime HEO
        AL_Ptime HEO

vuau AL_Ptime E2x
     AL_verdi_b E2x
     AL_TShell EU2HL

and it would better if even i can sub it in $2 not only $1 like this:

tongtha AL_Vcs abcdef
               abcdef
               abcdef

tuanngu AL_Ptime HEO
                 HEO

vuau AL_Ptime E2x
     AL_verdi_b E2x
     AL_TShell EU2HL
John1024

Replacing the first column with spaces but not on the first match

$ awk '{save=$1} $1==last{gsub(/./," ",$1)} {last=save} 1' file
tongtha AL_Vcs abcdef
        AL_Vcs abcdef
        AL_Vcs abcdef

tuanngu AL_Ptime HEO
        AL_Ptime HEO

vuau AL_Ptime E2x
     AL_verdi_b E2x
     AL_TShell EU2HL

How it works

  • save=$1

    This saves the value of the first column in variable save.

  • $1==last{gsub(/./," ",$1)}

    If the first column is the same as the variable last, then we replace the first column with an equal number of blanks.

  • last=save

    This updates the variable last with the value of that the first column had before any substitutions.

  • 1

    This is awk's cryptic shorthand for print-the-line.

Replacing the first two columns but not on the first match

To do the same for the first two columns:

$ awk '{save=$1} $1==last{gsub(/./," ",$1); gsub(/./," ",$2)} {last=save} 1' file
tongtha AL_Vcs abcdef
               abcdef
               abcdef

tuanngu AL_Ptime HEO
                 HEO

vuau AL_Ptime E2x
                E2x
               EU2HL

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to match the first occurrence of a pattern (or character) except for lines where that pattern is at the beginning of the line?

Regex match all except a pattern

Match first pattern in a file

Using Karma, how do I exclude all files that match a pattern except for those within a specific sub-folder?

Match everything except my pattern (RegEx)

Regex to match everything except trailing pattern

Match pattern except under one condition Regex

Regex - how to match everything except a particular pattern

Regex: Match patterns except with pattern preceding

Match everything except a pattern and replace matched with string

Pattern to match everything except a string of 5 digits

Sed: delete lines without pattern except first

Forcing evaluation first in a pattern match?

CLIPS match first field of a pattern

Match the first occurrence of an optional pattern

PostgreSQL - Pattern Match - String to Sub-string

sed pattern match until first match only

Regular expression match all except first occurence

Match two chars except first and last

Regex match all except first instance

Lines from first match (pattern 1) to last match (pattern 2)

R sub/gsub replacing first occurence of match

sub function in r does not replace the first match

RegEx : Match all lines except for a specific sub-string

Match everything after particular pattern except whitespace till next letter

Google sheet : REGEXREPLACE match everything except a particular pattern

Sed and regex to match all number except as part of another pattern

Match a string when the pattern exists except when starting with it

Regex Pattern to Match, Excluding when... / Except between