Operating System - HP-UX
1846986 Members
3676 Online
110257 Solutions
New Discussion

Pattern Finding & replacing tool

 
SOLVED
Go to solution
Tamer Shaalan
Regular Advisor

Pattern Finding & replacing tool

Hi all of you,

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)
Success is a journey, not a destination
9 REPLIES 9
curt larson_1
Honored Contributor

Re: Pattern Finding & replacing tool

do you have an example of what your trying to do.

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
Tamer Shaalan
Regular Advisor

Re: Pattern Finding & replacing tool

OK Curt,

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.
Success is a journey, not a destination
curt larson_1
Honored Contributor
Solution

Re: Pattern Finding & replacing tool

lots of ways to do that

olddir=$(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;"
curt larson_1
Honored Contributor

Re: Pattern Finding & replacing tool

looks like a couple of characters didn't get there

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
Tamer Shaalan
Regular Advisor

Re: Pattern Finding & replacing tool

Hi Curt,

many Thanks for you for helping me !!!!!

what about this :

# ls giza* > list
# for i in $(> do
> 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.
Success is a journey, not a destination
curt larson_1
Honored Contributor

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.
curt larson_1
Honored Contributor

Re: Pattern Finding & replacing tool

oops forgot to increment my loop counter

done
x=$(( $x + 1 ))
done
cd $olddir
curt larson_1
Honored Contributor

Re: Pattern Finding & replacing tool

what about this :

# ls giza* > list
# for i in $(> do
> what about this :

# ls giza* > list
# for i in $(> do
> 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/')
Tamer Shaalan
Regular Advisor

Re: Pattern Finding & replacing tool

Hi Curt,

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 $(> do
> 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.
Success is a journey, not a destination