Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Help

Shannon_1
Occasional Advisor

Script Help

Need help with pattern matching reading from a file. Trying to use egrep but not sure that is the best option.

File contents:

Register 6
CASH 100.00
CHECK 50.00
Register 6
.....other stuff
Register 7
CASH 2000.00
CHECK 10.00
Register 7
.....other stuff, etc.

I need my output to be
Register 6
CASH 100.00
CHECK 50.00

Register 7
CASH 2000.00
CHECK 10.00

Any advice on the best utility to use?
6 REPLIES
harry d brown jr
Honored Contributor

Re: Script Help

grep -i -e "^register" -e "^cash" -e "^check" FILENAME

live free or die
harry d brown jr
Live Free or Die
Kent Ostby
Honored Contributor

Re: Script Help

So I'm assuming that you only want to print a register name when you get to a cash and check line.

Create a script in a file called useme.awk :

/^Register/ {dareg=$0}
/CASH/ {dacash=$0}
/CHECK/ {print dareg; print dacash; print $0}

Run the script like this:

awk -f useme.awk < inputfile > outputfile

Best regards,

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Sandman!
Honored Contributor

Re: Script Help

Shannon,

Are you trying to display the sum of the amounts (cash or check) at the different registers? I'm not sure from your posting; could you clarify.

cheers!

Shannon_1
Occasional Advisor

Re: Script Help

Actually I am not trying to display the sum necessarily but just the register number
and the cash and check amounts under the register number. The register numbers are listed twice in the file as well as some other garbage that i don't need. So I want the output to be something like

Register 6
CASH 100.00
CHECK 50.00

Register 7
CASH 2000.00
CHECK 10.00

With the register number and then the corresponding cash and check amounts.

Thanks for any help. I tried the other two suggestions. Couldn't get the grep to work and the awk script did not print register numbers but only amounts.
Stuart Browne
Honored Contributor

Re: Script Help

That's odd, Kent's awk should work just fine. Here it is as a one-liner:

awk '/^Register/{dareg=$0}/CASH/{dacash=$0}/CHECK/{print dareg;print dacash;print $0}' < file

One long-haired git at your service...
Highlighted
Muthukumar_5
Honored Contributor

Re: Script Help

You can get with awk one liner as,

# awk '{ if ( $1 == "Register") { ln1=$0;getline ln2; split(ln2,a," ");if (a[1]=="CASH") { getline ln3; split(ln3,b," ");if (b[1]=="CHECK") { print ln1"\n"ln2"\n"ln3}}} print "\n";}'

hth.
Easy to suggest when don't know about the problem!