1745788 Members
3327 Online
108722 Solutions
New Discussion юеВ

Scripting question

 
SOLVED
Go to solution
Dadski
Advisor

Scripting question

Hi All, I am attempting to search a file for all occurances of "PP= 2" the number may differ. What I want to do is search for the "PP=" part and any number assigned to PP to be either multiplied or divded. I would then like to save the entire file and not just the lines with PP=, with the new settings.

I could do this using awk as in:
awk '/PP=/{print $1" "$2 *2}' filename > file1
but I need the whole contents of the file and not just the lines containing PP=.

Any Ideas Appreciated.

cheers

Martin

 

 

P.S. this thread has been moved from HP-UX > System Administration to HP-UX > languages - HP Forums Moderator

18 REPLIES 18
Michael Steele_2
Honored Contributor

Re: Scripting question

Hi

grep -v "PP\=" file > non_PP_lines_file
grep "PP\=" file > PP_lines_file
Support Fatherhood - Stop Family Law
Tim Nelson
Honored Contributor

Re: Scripting question

if grep PP= filename > /dev/null 2>&1
then
echo "Yep, it PP= is in this file"
#(substitute above with any action youwish)
else
echo "Nope, PP= is not in this file"
#(ditto)
fi
OldSchool
Honored Contributor

Re: Scripting question

I think he needs something like sed to change

PP=[:digit:]

to

PP=[:digit:] * 2.

while retaining the entirety of the file....I'd guess he needs to keep the order of lines within that file as well, but a better problem description (or sample input with desired output) is needed.

Dadski
Advisor

Re: Scripting question

Old School,

your right I need to keep the integrity of the file, I have used sed to amend all the other entries, the issue I have is I need to either multiply or divide the number assoiciated with PP= while keeping the rest of the file intact. I could use awk but that strips the rest of the file. and I dont believe you can use mathamatical options in sed? saying that I am not a sed expert, but I dont think so. If awk has an option to show the whole file while manipulating the figure then that would be my solution, but again I dont believe it does.?

Any ideas cause I'm stumped.
Michael Steele_2
Honored Contributor

Re: Scripting question

Hi


grep "PP\=" file > PP_lines_file

cat PP_lines_file | sed 's/\=/ /' | awk ${print $1 $2) ) | while read a b
do
TOT=$(($b+$TOT))
done
Support Fatherhood - Stop Family Law
Dadski
Advisor

Re: Scripting question

Michael,

the awk part was'nt right, but did look promising, I have tried to amend but still not working, could it the the ksh I am using?
Michael Steele_2
Honored Contributor

Re: Scripting question

Throw some bacon treats and paste in what you got so fare
Support Fatherhood - Stop Family Law
OldSchool
Honored Contributor

Re: Scripting question

ok, so you're saying you want to find

PP=2 and replace it w/ PP=4?
PP=3 and replace it w/ PP=6?

and not something like:

PP=2*2 or PP=3*2 (which is how I read the original post.

awk will by default read and process every line, in your example you tell it to pattern match, then process

a script like the following might be a start

{
if ( $1 ~ /PP=/ )
{
x=split($1,ppline,"=")
print ppline[1] "=" ppline[2]*2
}
else
print $0
}
~
Michael Steele_2
Honored Contributor

Re: Scripting question

Interesting.

You refuse to award points even to get the final answer of your script problem.

So I went back and check you out, 1 point assigned for 11 questions asked.

Sigh. You know this forum is going Paid in Advanced because of people like you?
Support Fatherhood - Stop Family Law