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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Follow up: Replace space with new line.

Status
Not open for further replies.

deepagovind

Technical User
Jan 26, 2004
4
0
0
US
Thanks for the valuable prompt replies.

Is it possible to search and replace each string with some prefix using awk?

For e.g.:

\X<7> with ".\X<7> (X[7])",

where X could be any word in the file?

Thanks, I once again appreciate your inputs!
 
Please give some more examples. It looks like you are not only looking for "X", but for "X" followed by a number between "<" and ">".
 
The sed way:
sed -e 's!\\\([-A-Za-z0-9_]*\)<7>!.\\\1<7> (\1[7])!' </path/to/input >output

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Actually it is any word and any number

i.e the word to replace is *<#>
and teh replace string is .*<#> (*[#]),

# could be any number and * could be any word.

Thanks!
 
Something like this ?
sed -e 's!\\\([-A-Za-z0-9_]*\)<\([0-9]*\)>!.\\\1<\2> (\1[\2]),!' </path/to/input >output

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
# Assuming you want to look for
# "X<7>" and replace it with ".X<7> (X[7])",
# where X can be any sequence of letters and
# 7 can be any sequence of numerals.

[tt]
match($0, /\\[a-zA-Z]+<[0-9]+>/ ) { s = substr($0,RSTART,RLENGTH)
left = substr($0,1,RSTART-1)
right = substr($0,RSTART+RLENGTH)
split(s,arr,/[\\<>]/)
s = ".\\" arr[2] "<" arr[3] "> (" arr[2] "[" arr[3] "])"
print left s right
next
}
1
[/tt]
 
Ignore previous post; it was not the latest version of the code. This should work:
[tt]
match($0, /[a-zA-Z]+<[0-9]+>/ ) { s = substr($0,RSTART,RLENGTH)
left = substr($0,1,RSTART-1)
right = substr($0,RSTART+RLENGTH)
split(s,arr,/[<>]/)
s = "." arr[1] "<" arr[2] "> (" arr[1] "[" arr[2] "])"
print left s right
next
}
1
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top