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
$ 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
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.
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.
Comments