- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Pattern Finding & replacing tool
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
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
03-21-2004 04:30 AM
03-21-2004 04:30 AM
I know it is a basic question.
I want a tool(command) to find & replace a pattern from a command line(I tried "sed", but the i/p of sed is treated to be a file name)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2004 04:49 AM
03-21-2004 04:49 AM
Re: Pattern Finding & replacing tool
sed, awk, perl, ex, etc can do pattern matching and substitution.
I'm those of the forum can assist if we had more information on what your trying to accomplish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2004 05:01 AM
03-21-2004 05:01 AM
Re: Pattern Finding & replacing tool
what I have is the following :
I have large number of files with names such yhat: NameDate.gz for example :
zamalek040311.gz
zamalek040312.gz
zamalek040313.gz
zamalek040314.gz
zamalek040315.gz
zamalek040316.gz
zamalek040317.gz
zamalek040318.gz
zamalek040319.gz
zamalek040320.gz
and
giza040311.gz
giza040312.gz
giza040313.gz
giza040314.gz
giza040315.gz
giza040317.gz
giza040318.gz
giza040319.gz
giza040320.gz
you see that the 1st part of the name is a certain name(related to data sourec) and the rest is the dae of this file.
what is required ??????
to exchnge the fisrt part ( zamalek or giza) with a corresponding code as
zamalek will be 23ca3
giza will be 25bc6 and so on.
have you any ideas.
Tamer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2004 05:19 AM
03-21-2004 05:19 AM
Solutionolddir=$(pwd)
cd yourdir
name=giza
newname="25bc6"
ls ${name}* |
while read fname1
do
newfile=$(print $fname1 | sed "s|$name|$newname|"
#mv fname1 newfile
done
cd $olddir
instead of using sed you could
awk "{sub($name,$newname);print $0;}"
or perl -ne "s/$name/$newname/;print;"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2004 05:25 AM
03-21-2004 05:25 AM
Re: Pattern Finding & replacing tool
newfile=$(print $fname1 | sed "s|$name|$newname|"
#mv fname1 newfile
should be
newfile=$(print $fname1 | sed "s|$name|$newname|")
#i left off the ) ont he above line
#and the $'s before the filenamess below
print $fname1 $newfile
#mv $fname1 $newfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2004 05:52 AM
03-21-2004 05:52 AM
Re: Pattern Finding & replacing tool
many Thanks for you for helping me !!!!!
what about this :
# ls giza* > list
# for i in $(
> print $i | k=$(sed 's/zamalek/code/')
> mv $i $k
> done
It is easily running
what I raelly was missing is :
print $i
before you replay to me, I used :
> sed 's/zamalek/code/' $i
which gives error ( because sed was replacing inside the file itself not its name !!!!!!!!! )
when I replaced with print $i and piping, it runs properly .
the last question !!!!!
If I have many patterns which must be replaced with another different codes, I need to see your intelligent solution???
I already did it !!!!!
Thanks again.
Tamer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2004 06:50 AM
03-21-2004 06:50 AM
Re: Pattern Finding & replacing tool
set -A names zamalek giza
set -A newNames 23ca3 25bc6
# might want to do some error checking
# on your names here
# like size of names and newNames are the same etc
olddir=$(pwd)
cd yourdir
x=0
while (( $x < ${#names[@]} ))
do
ls "${names[$x]}*" |
while read fname
do
newfname=$(print $fname | sed "s|${names[$x]}|${newNames[$x]}|"
mv $fname $newfname
done
done
cd $olddir
you might try a bit different algorithm that doesn't so an ls for every name, that would make things faster.
you could do this also all within awk or perl. And, with associative arrays it would be a bit easier to do.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2004 06:53 AM
03-21-2004 06:53 AM
Re: Pattern Finding & replacing tool
done
x=$(( $x + 1 ))
done
cd $olddir
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2004 07:37 AM
03-21-2004 07:37 AM
Re: Pattern Finding & replacing tool
# ls giza* > list
# for i in $(
> what about this :
# ls giza* > list
# for i in $(
> print $i | k=$(sed 's/zamalek/code/')
> mv $i $k
> done
> mv $i $k
> done
well generally it is unnecessary to create a list. of course for troubleshooting or auditing it can be helpful to do such checkpointing.
for i in $(ls giza*)
will run faster, because your not creating a file. and you don't have to worry about coordinating with other programs that could write to "list" and corrupt your data.
and only excepts a finite amount of data, i.e. there is a limit in how many character "ls giza*" can return. it is a large amount, but when you don't know how much data is going to be returned reading from a pipe is preferred. (at least by me it is). so i'd do something like this instead
ls giza* |
while read fname
do
and i think instead of
> print $i | k=$(sed 's/zamalek/code/')
you must of meant
k=$(print $i | sed 's/giza/code/')
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2004 09:10 PM
03-21-2004 09:10 PM
Re: Pattern Finding & replacing tool
Good work & experience !!!!!!
Really, I am beginner in HP-UX and in even ITRC at all.
what about this :
I created a text file with two fields, first is the name & the second is the corresponding code, as follows :
.
.
.
opra 6055
pyre 6056
pyre2 22161
pyrw 6057
rams 6058
remaya 22160
tagamoa 25206
tora 25209
zamalek 23569
.
.
. and so on,
I called this file swcodes
this is my script (really, first script in unix) :
#while read x y
> do
> ls $x*.gz > list
> for i in $(
> print $i | k=$(sed 's/'$x'/'$y'/')
> mv $i $k
> done
> done < swcodes
it runs properly !!!!
If you have any comments, enhancements, please don't hesitate to inform me.
Thank you too much.
Tamer.