- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Using SED with backslashes in string
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
тАО09-20-2010 07:12 AM
тАО09-20-2010 07:12 AM
I need to search a file for a computer name and replace it with a DFS path, for instance, search for all instances of
caetwk-srv3
and replace with
\\us\name1\name2\name3
How would this replace command look in sed??
Thanks in advance!
Solved! Go to Solution.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2010 07:19 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2010 08:34 AM
тАО09-20-2010 08:34 AM
Re: Using SED with backslashes in string
If you use Perl, you can greatly improve the readability and easily perform the desired substitution:
# echo "caetwk-srv3"|perl -pe 's{caetwk-srv3}{\\\\us\\name1\\name2\\name3}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2010 09:24 AM
тАО09-20-2010 09:24 AM
Re: Using SED with backslashes in string
My command looks like this and works fine
sed -e s/caetwk-srv3/us\\\name1\\\name2\\\name3/g inputfile >outputfile
Thank you very much!
P.S. There was already a double-backslash in front of caetwk-srv3, so I just left that alone and didn't have to insert any before "us"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-20-2010 09:53 AM
тАО09-20-2010 09:53 AM
Re: Using SED with backslashes in string
NO POINTS FOR THIS, but I should have escaped the 'sed' program as:
# echo "caetwk-srv3"|sed -e 's/caetwk-srv3/\\\\us\\name1\\name2\\name3/'
Doing so would have negated the need for doubling the backslashes as I first did!
NO POINTS FOR THIS.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2010 08:37 AM
тАО09-21-2010 08:37 AM
Re: Using SED with backslashes in string
But in this case you need to escape the characters because \name1 would be meant as a newline character with the string "ame1".
To clarify it, these also work:
# echo test | sed 's.test.TEST.'
TEST
and
# echo test | sed 's#test#TEST#'
TEST
But, as I said, in this case the special meaning of \n causes you another problem:
# echo caetwk-srv3 | sed 's.caetwk-srv3.\\us\name1\name2\name3.'
\us
ame1
ame2
ame3
#
Unix operates with beer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-21-2010 02:56 PM
тАО09-21-2010 02:56 PM
Re: Using SED with backslashes in string
Well in this case, "/" wasn't being used.
>But in this case you need to escape the characters because \name1 would be meant
You almost always need to escape "\" if you want a backslash. It doesn't matter if there was a "n".
Since it is in '', the shell doesn't see it but sed(1) has its own rules about "\".