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!

substitutions

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi everyone,

I have to write a script that changes sentences like:

Grace met S. Rogers, K. Walters and P. Martin.
I know what U.F.O. means.
He finished as nr. 2.

into:

Grace met S| Rogers, K| Walters and P| Martin.
I know what U|F|O| means.
He finished as nr| 2.

I'm using the following script:

{
if($0~/S\./)
gsub(/S\./, "S|")
if($0~/K\./)
gsub(/K\./, "K|")
if($0~etc)
gsub(etc...)
print $0
}

Does anyone know how I can use regexp in this example in order to facilitate the exercise? It would be much appreciated!

Many thanks in advance!

Kim


 
A whimpy way with 2 gsubs is.......

I have to think of the better solution - I don like that one.....

BEGIN {
pat1="[.][^$]"
pat2="[|][.]"
}

{
gsub(pat1,"|&");
gsub(pat2, "|");
print;
}

END {

}
 
much easier to do in sed:

sed -e 's/[.]\([^$]\)/\|\1/g' yourFile

vlad
 
Many thanks vgersh99! The Awk script works perfectly well! There's one problem, however, which makes it more complex. I've overlooked the possibility that two (or more) sentences can occur on the same line (sorry about that).

So:

J. Martin is his name. He's an old man. He finished as nr. 2 in his category. He knows what a U.F.O. is.

Should become:

J| Martin is his name. He's an old man. He finished as nr| 2 in his category. He knows what a U|F|O is.

So if the "stop" (".") is preceded by a word that has less than three letters, it becomes "|"? Still possible to solve this with gsub? Thanks for helping me with this!

Kim

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top