Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gsub

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,

I want to write a script, using gsub, that says 'a full stop "." becomes a vertical line "|" unless the "." is preceded by a word that has three letters or more'.

So:

"nr." becomes "nr|"
"M." becomes "M|"
"Article." remains "Article."
"person." remains "person."
"U.F.O." becomes "U|F|O|"
"can." remains "can."

Can someone help me with this? Many thanks!

Febri


 
awk ' {
all = substr($0,1,3)
if (all ~ /\./) {
gsub(/\./,"|",$0)
}
print
}'

HTH
MMD
 
Thanks for the trouble but, unfortunately, I'm still getting a wrong output!

This is e.g. correct:

"nr. 2." remains "nr. 2."

But this is e.g. not correct:

"M. Rogers." becomes "M| Rogers|" with the script you've proposed but should become "M| Rogers." The same holds for the following: "Mr. Bond. James Bond." becomes with the script you've proposed "Mr| Bond| James Bond|" but should become "Mr| Bond. James Bond."

So gsub should not change every full stop "." into a vertical line "|" on $0, but ONLY in those fields where the full stop is preceded by a word that has three letters or more. Thanks for your help!

F
 
This works on my box:
I was asleep last night when I wrote my posts here,
so nothing worked...;(

awk ' {
every[c++] = substr($0,1,4)
for (xx in every) {
if (every[xx] ~ /\./) {
orig = every[xx] ;#save the original
gsub(/\./,"|",every[xx]) ; changed
replaced = every[xx] ; #save changed string
}
}

#commit the chnges globally
if ($0 ~ orig) {
gsub(orig,replaced,$0)
}
print
}' file

Notes:
The length of the substring match should be adjusted
for your string length. I'm not sure: you said 3 chars
but you wanted U|F|O|, which is 4 places...
 
Another solution to your problem :

{
strin=$0
strout=""
pos=index(strin,".")
while (pos != 0) {
word=substr(strin,1,pos-1);
strin=substr(strin,pos+1);
strout=strout word
if (word ~ "(^|[^A-z]+)[A-z]{0,2}$")
strout=strout "|"
else
strout=strout "."
pos=index(strin,".")
}
print strout strin
} Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top