- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Replace string in file
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
Forums
Discussions
Discussions
Discussions
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
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
08-13-2008 10:57 PM
08-13-2008 10:57 PM
Replace string in file
My file "test" contains /abc/efg/qwerty
and my variables a and b contains
a=/abc/efg
b=/qwerty
how can i repace the contents of my file
"testt" with #/abc/efg/qwerty using a and b variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 11:44 PM
08-13-2008 11:44 PM
Re: Replace string in file
sed is a good tool
http://www.linuxquestions.org/questions/linux-newbie-8/does-anyone-know-of-a-bash-script-can-search-and-replace-txt-in-a-file.-278332/
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=57865
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 11:46 PM
08-13-2008 11:46 PM
Re: Replace string in file
a=/abc/efg
b=/qwerty
sed -e "s:\\($a$b\\):#\\1:" test > test.new
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 11:53 PM
08-13-2008 11:53 PM
Re: Replace string in file
Appreciate your early help.
Its working fine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2008 11:57 PM
08-13-2008 11:57 PM
Re: Replace string in file
wat is the use of \\ in
sed -e "s:\\($a$b\\):#\\1:" test > test.new
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2008 12:07 AM
08-14-2008 12:07 AM
Re: Replace string in file
If our answers were helpful, please read the following about assigning points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33
>what is the use of \\ in:
sed -e "s:\\($a$b\\):#\\1:" test > test.new
I assumed you didn't want to repeat $a$b twice and since you can use regular expressions to select pieces from the original string to add back in the replacement string, I used it. Ordinarily you would like to use '' for sed and not "". But because you want to use $a inside, you need "". If you need to use "\", you need to double them.
So your Regular Expression is:
s:\(stuff\):#\1:
This puts "stuff", delimited by \( and \), back where you have the \1, preceded by a "#". You can select multiple pieces and use \1, \2, to put them back, in any order you like.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2008 12:20 AM
08-14-2008 12:20 AM
Re: Replace string in file
Regards,
gowda
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2008 12:28 AM
08-14-2008 12:28 AM
Re: Replace string in file
Since it is too hard to use "" with awk, you use -v to pass in a variable:
awk -v STR="$a$b" '
{
sub(STR, "#" STR)
print $0
}' text