Re: Scripting question (390 Views)
Reply
Advisor
Dadski
Posts: 17
Registered: ‎01-08-2009
Message 1 of 19 (390 Views)
Accepted Solution

Scripting question

[ Edited ]

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

Please use plain text.
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 2 of 19 (390 Views)

Re: Scripting question

Hi

grep -v "PP\=" file > non_PP_lines_file
grep "PP\=" file > PP_lines_file
Support Fatherhood - Stop Family Law
Please use plain text.
Honored Contributor
Tim Nelson
Posts: 3,727
Registered: ‎06-01-2000
Message 3 of 19 (390 Views)

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
Please use plain text.
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 4 of 19 (390 Views)

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.

Please use plain text.
Advisor
Dadski
Posts: 17
Registered: ‎01-08-2009
Message 5 of 19 (390 Views)

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.
Please use plain text.
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 6 of 19 (390 Views)

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
Please use plain text.
Advisor
Dadski
Posts: 17
Registered: ‎01-08-2009
Message 7 of 19 (390 Views)

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?
Please use plain text.
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 8 of 19 (390 Views)

Re: Scripting question

Throw some bacon treats and paste in what you got so fare
Support Fatherhood - Stop Family Law
Please use plain text.
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 9 of 19 (390 Views)

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
}
~
Please use plain text.
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 10 of 19 (390 Views)

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
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,972
Registered: ‎03-06-2006
Message 11 of 19 (390 Views)

Re: Scripting question

>but I need the whole contents of the file and not just the lines containing PP=.

Then just print them:
awk '
/PP=/{print $1, $2 *2; next}
{ print $0 }' filename > file1
Please use plain text.
Honored Contributor
Raj D.
Posts: 2,624
Registered: ‎06-12-2004
Message 12 of 19 (390 Views)

Re: Scripting question

mlewi000,

Check this out:


Code:
--------------------------------------------
# awk '{$2=$2*2} /PP=/ {print $0}' pp_file
--------------------------------------------

# cat pp_file
PP= 2 As a quick 0909 A00990
PP= 3 As a quick A09093 2342
PP= 5 sas a quick 09000! A00
PP= 9 sa a aqij 0900 A009
PP= 6 as oks fA09000 0090 00
SP= 7 sk sa aik a0000 aa
PP= 8.1 Asa sfllks 9990
NP= 3 aksfs A0009 aa0009
#
# awk '{$2=$2*2} /PP=/ {print $0}' pp_file
PP= 4 As a quick 0909 A00990
PP= 6 As a quick A09093 2342
PP= 10 sas a quick 09000! A00
PP= 18 sa a aqij 0900 A009
PP= 12 as oks fA09000 0090 00
PP= 16.2 Asa sfllks 9990
#



Cheers,
Raj.




*Remember to assign points to the responses all who tried o help you.
" If u think u can , If u think u cannot , - You are always Right . "
Please use plain text.
Honored Contributor
Suraj K Sankari
Posts: 1,659
Registered: ‎09-09-2008
Message 13 of 19 (390 Views)

Re: Scripting question

Hi,
If you satisfied with the answer and your problem got solved then please make a habit to assign points,
Those people who give there valuable time for your problem they should expect some apparition from you in terms of points.

To know how to assign points please go through the below link

http://forums13.itrc.hp.com/service/forums/helptips.do?admit=109447627+1256027208667+28353475#33

Thanks
Suraj
Please use plain text.
Advisor
Dadski
Posts: 17
Registered: ‎01-08-2009
Message 14 of 19 (390 Views)

Re: Scripting question

Old School and Raj D, apologies for not getting back, but the weekend took hold. I did'nt really get what I was looking for but you 2 were close. Thanks for everyones time and effort. I'm going to look into an awk script combined with sed to achieve it. All points assigned
Please use plain text.
Advisor
Dadski
Posts: 17
Registered: ‎01-08-2009
Message 15 of 19 (390 Views)

Re: Scripting question

told a complete lie, Dennis your response was perfect, solved it a treat. Thanks for your time and I have assigned your points.
Please use plain text.
Advisor
Dadski
Posts: 17
Registered: ‎01-08-2009
Message 16 of 19 (390 Views)

Re: Scripting question

Dennis thanks, thats perfect.
Please use plain text.
Advisor
Dadski
Posts: 17
Registered: ‎01-08-2009
Message 17 of 19 (390 Views)

Re: Scripting question

Michael, just read your comments, i was'nt aware firstly, a while back when I first joined about the points, nor did I realise poeple should become really touchy on the system, or that when the weekend arrives you should be waiting at the computer and neglecting time with the family and friends, just to appease some guy, who has no life and personality. Next time I post I will ensure I neglect my parental responsibilities to appease some aneraked plebs appetite for some points. Which actually you did'nt post any correct answers. Denis, thanks for the correct answer, I gues it was just that awk option I was looking for and not aware of, please have all 10 points and old schoool and Raj for your time and being close.
Please use plain text.
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 18 of 19 (390 Views)

Re: Scripting question

Gee Martin, wouldn't blow your bubble if it became a paid for service instead of the accumulatin of 100's of years of experience all for free?
Support Fatherhood - Stop Family Law
Please use plain text.
Honored Contributor
Michael Steele_2
Posts: 4,934
Registered: ‎08-14-2002
Message 19 of 19 (390 Views)

Re: Scripting question

Hi

Let me take a different approach since its easy to miss some of these links in your profile. Here is your profile

http://forums11.itrc.hp.com/service/forums/publicProfile.do?forumId=1&userId=CA828944

At the very bottom under the last section titled "My Questions" is a hard to miss link call "unassigned points", every thread where you haven't provided 0 to 10 points still gets flagged. And the only way you can remove these flags is by assigning points.
Support Fatherhood - Stop Family Law
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation