HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: sed and replace character
Operating System - Linux
1830165
Members
2355
Online
109999
Solutions
Forums
Categories
Company
Local Language
back
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
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- 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
05-25-2007 05:58 AM
05-25-2007 05:58 AM
sed and replace character
hi all
i have problem with sed command to replace space with _.
i have file formated as follow:
f_name = [kamal ahmed kamal] job = [sysadmin]
address = [30 A anything Street] ....etc
i want to replace only spaces between [ ].
so the file become:
f_name = [kamal_ahmed_kamal] job = [sysadmin]
address = [30_A_anything Street] ....etc
can any one help me ?
many thanks
kamal
i have problem with sed command to replace space with _.
i have file formated as follow:
f_name = [kamal ahmed kamal] job = [sysadmin]
address = [30 A anything Street] ....etc
i want to replace only spaces between [ ].
so the file become:
f_name = [kamal_ahmed_kamal] job = [sysadmin]
address = [30_A_anything Street] ....etc
can any one help me ?
many thanks
kamal
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2007 07:27 AM
05-25-2007 07:27 AM
Re: sed and replace character
Hello kamal,
$ cat tr_sp.pl
#!/usr/bin/perl -wn
foreach $byte (split //,$_)
{
$byte =~ /\[/ and $br=1;
$byte =~ /\]/ and $br=0;
$br and $byte =~ s/ /_/;
print $byte;
}
$ cat recs
f_name = [kamal ahmed kamal] job = [sysadmin]
address = [30 A anything Street] ....etc
$ perl tr_sp.pl recs
f_name = [kamal_ahmed_kamal] job = [sysadmin]
address = [30_A_anything_Street] ....etc
PCS
$ cat tr_sp.pl
#!/usr/bin/perl -wn
foreach $byte (split //,$_)
{
$byte =~ /\[/ and $br=1;
$byte =~ /\]/ and $br=0;
$br and $byte =~ s/ /_/;
print $byte;
}
$ cat recs
f_name = [kamal ahmed kamal] job = [sysadmin]
address = [30 A anything Street] ....etc
$ perl tr_sp.pl recs
f_name = [kamal_ahmed_kamal] job = [sysadmin]
address = [30_A_anything_Street] ....etc
PCS
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2007 02:18 PM
05-25-2007 02:18 PM
Re: sed and replace character
This ugly piece of perl seems to do it in one line.
I'm sure it can be writting better, but here it goes...
perl -pe '1 while (s/(\[\S*?)\s([^\[]+\])/\1_\2/)' x
Using s/x/y/g did only replace one space.
The while forces the repeated attempts.
(\[\S*?) looks for, and remembers in \1 an opening box and any number of non-whitespace
\s followed by whitespace
([^\[]+\]) followed by any number non-open-box, followed by a closing box, which is all remembered in \2.
Just using [^x]+ but the x is a [ and needs to be escaped giving [^\[]+
Replace the lot by \1, an _ and \2.
fwiw,
Hein.
I'm sure it can be writting better, but here it goes...
perl -pe '1 while (s/(\[\S*?)\s([^\[]+\])/\1_\2/)' x
Using s/x/y/g did only replace one space.
The while forces the repeated attempts.
(\[\S*?) looks for, and remembers in \1 an opening box and any number of non-whitespace
\s followed by whitespace
([^\[]+\]) followed by any number non-open-box, followed by a closing box, which is all remembered in \2.
Just using [^x]+ but the x is a [ and needs to be escaped giving [^\[]+
Replace the lot by \1, an _ and \2.
fwiw,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2007 07:35 PM
05-27-2007 07:35 PM
Re: sed and replace character
With sed command use
sed 's/ /_/g;s/_=_/ = /g;s/\]_/\] /g' input_file
OUTPUT:
f_name = [kamal_ahmed_kamal] job = [sysadmin]
address = [30_A_anything_Street]
sed 's/ /_/g;s/_=_/ = /g;s/\]_/\] /g' input_file
OUTPUT:
f_name = [kamal_ahmed_kamal] job = [sysadmin]
address = [30_A_anything_Street]
http://www.geocities.com/mukeshgct/
- Tags:
- sed
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP