- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Change occurrence of a string in shell
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-07-2004 02:40 AM
тАО09-07-2004 02:40 AM
Re: Change occurrence of a string in shell
#!/bin/sh
awk '{
if(substr($0,4,3)=="XXX") {
printf("%s%s%s\n",substr($0,1,3),"YYY",substr($0,7));
} else print;}'
or shorter
awk '{ if(substr($0,4,3)=="XXX") printf("%s%s%s\n",substr($0,1,3),"YYY",substr($
0,7)); else print; }'
Regards,
Jean-Luc
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 02:46 AM
тАО09-07-2004 02:46 AM
Re: Change occurrence of a string in shell
Cool- I didn't even think about substr being usable as an lvalue...
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 05:06 AM
тАО09-07-2004 05:06 AM
Re: Change occurrence of a string in shell
echo "123XXX789XXX" | sed -e 's/XXX/YYY/1'
or
echo "123XXX789XXX" | sed -e 's/XXX/YYY/'
It will only change the first XXX with YYY
To change the next occurence with YYY then,
echo "123XXX789XXX" | sed -e 's/XXX/YYY/2'
To all,
echo "123XXX789XXX" | sed -e 's/XXX/YYY/g'
Regards
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 06:50 PM
тАО09-07-2004 06:50 PM
Re: Change occurrence of a string in shell
[[ "$(echo "123XXX789XXX" | cut -c 4-6)" = "XXX" ]] && echo "123XXX789XXX" | perl -pe 's/^(...)XXX/$1YYY/'
or as,
VAR="123XXX789XXX"
[[ "$(echo $VAR | cut -c 4-6)" = "XXX" ]] && VAR=$(echo $VAR | perl -pe 's/^(...)XXX/$1YYY/')
echo $VAR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2004 07:07 PM
тАО09-07-2004 07:07 PM
Re: Change occurrence of a string in shell
sed 's|^\(...\)XXX|\1YYY|' file
As for the remark 'can be the first occurrence or not' I think Diego meant that it is possible that there are X characters in the first 3 characters of the line, resulting in problems with the first two solutions.
- « Previous
-
- 1
- 2
- Next »