- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- replacing a charecter
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2007 02:00 AM
тАО10-08-2007 02:00 AM
replacing a charecter
I have a variable with some value. eg,
TestVar="abcdefg.dat"
Using code i want to replace .dat to .txt
after replacement operation , the value of TestVar should be abcdefg.txt.
Please help me to achieve this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2007 02:09 AM
тАО10-08-2007 02:09 AM
Re: replacing a charecter
# echo $TestVar|sed -e 's/\.dat$/\.txt/'
Note that you must escape (backslash) the dot character in order for it to represent a dot and not any character.
The dollar sign ($) character anchors the string to the end of the line for exact matching.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2007 02:10 AM
тАО10-08-2007 02:10 AM
Re: replacing a charecter
echo abcdefg.dat | awk '{sub(/.dat/,".txt"); print}'
abcdefg.txt
In this case I substituted .dat with .txt
regards,
ivan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2007 02:11 AM
тАО10-08-2007 02:11 AM
Re: replacing a charecter
Oh, and since you want a destructive update of the variable:
# TestVar=$(echo $TestVar|sed -e 's/\.dat$/\.txt/')
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2007 02:13 AM
тАО10-08-2007 02:13 AM
Re: replacing a charecter
TestVar="abcdefg.dat"
NewVar=$(echo "${TestVar}" | awk '{gsub(".dat$",".txt"); print $0}')
echo "NewVar = ${NewVar}"
Note the '$' at the end of the pattern match; this means that .dat will only be matched at the end of the string.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2007 02:15 AM
тАО10-08-2007 02:15 AM
Re: replacing a charecter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2007 02:22 AM
тАО10-08-2007 02:22 AM
Re: replacing a charecter
Welcome to the ITRC Forum! As a new member, please see:
https://forums1.itrc.hp.com/service/forums/helptips.do?#28
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-08-2007 02:33 AM
тАО10-08-2007 02:33 AM
Re: replacing a charecter
newvar="$(basename $testvar dat)txt"
or
newvar-"${testvar%dat}txt"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-09-2007 12:21 AM
тАО10-09-2007 12:21 AM
Re: replacing a charecter
But I like mystery. Did the answer really help? Or will it destroy the computer? The suspense is killing me.
(sigh).
Try the tr command too. tr for transliterate.
I use this to replace hex numbers with stuff I can see.
Of course you can always use vi on a text file. Run :%s/fromtext/totext/g to replace all "fromtext" phrases to "totext" on every occurance. On every line.