Sponsored Content
Top Forums Shell Programming and Scripting Search two patterns using awk to print the variable sum Post 302778607 by Ramesh M on Monday 11th of March 2013 10:08:36 AM
Old 03-11-2013
Search two patterns using awk to print the variable sum

Coins.txt:

gold 1 1986 USA American Eagle
gold 1 1908 Austria-Hungary Franz Josef 100 Korona
silver 10 1981 USA ingot
gold 1 1984 Switzerland ingot
gold 1 1979 RSA Krugerrand
gold 0.5 1981 RSA Krugerrand
gold 0.1 1986 PRC Panda
silver 1 1986 USA Liberty dollar
gold 0.25 1986 USA Liberty 5-dollar piece
silver 0.5 1986 USA Liberty 50-cent piece
silver 1 1987 USA Constitution dollar
gold 0.25 1987 USA Constitution 5-dollar piece
gold 1 1988 Canada Maple Leaf


awk '/gold/ {ounces += $2} END {print "value = $" 425*ounces}' coins.txt

Output: value = $2592.5

When i tried modifying one of the above programs to calculate and display the total amount (in ounces) of gold and silver, separately but with one program. By using two pairs of pattern/action but it won’t works.

Help me on the above obligation

Last edited by Ramesh M; 03-11-2013 at 11:15 AM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

connecting search patterns in awk with AND

Hello friends, I couldnt connect two search patterns in awk, what i want is to search for two words in a log file; "+MB)" and "Done" i use this code /usr/xpg4/bin/awk '/+MB)/ {gsub("\(","",$5);print int($5)}' mylog.txt and i get integer part of (123,45MB) in a log file "mylog" with ... (1 Reply)
Discussion started by: EAGL€
1 Replies

2. Shell Programming and Scripting

Using a script variable in awk search patterns

Hi all, In a script like : job_date=.... ls -l 2>/dev/null | awk -v var =$job_date ' /Name\.Version\.+\.xml$/ { How can i include a script variable job_date store in "var" in the pattern "/Name\.Version\.+\.xml$/" Thanks in advance (12 Replies)
Discussion started by: abhinav192
12 Replies

3. Shell Programming and Scripting

loop + sum + print using awk

Hi, I am unable sum of each column in the loop usng awk command. Awk is not allowing the parameters in the command. i am facing the below error. awk: 0602-562 Field $() is not correct. Source file abc.txt 100,200,300,400,500,600,700,800,900 101,201,301,401,501,601,701,801,901 ... (1 Reply)
Discussion started by: number10
1 Replies

4. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

5. Shell Programming and Scripting

Search for the two patterns and print everything in between

Hi all, I have a file having data: @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTAATA NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTA... NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#.....CT NTTGGGTTTTCT I want to print everything starting from # till line ends. can you please help me how... (5 Replies)
Discussion started by: pirates.genome
5 Replies

6. Shell Programming and Scripting

sum from ls -al |awk '{print $5}'

// AIX 5.3 & AIX 6.1 ls -al |awk '{print $5}' This gives each file's size in byte. I need to get: - the sum of all files in Giga bytes with loop. - excluding the size of directories (ls -al returns the size of directories). There are hundreds and thousands of files, so summing up... (8 Replies)
Discussion started by: Daniel Gate
8 Replies

7. Shell Programming and Scripting

awk search patterns from file in another

I would like to grep for aaa and bbb and ccc from one line in file1.txt in any order on a line on file2.txt file1.txt aaa bbb ccc ddd fff ggg hhh ddd jjj jjj cccfile2.txt aaa bbb ccc ddd fff ggg --> output whole line since it matches with aaa bbb ccc of file1.txt aaa ddd jjj hhh --> no... (1 Reply)
Discussion started by: sdf
1 Replies

8. Shell Programming and Scripting

awk - Print Sum

Hi, I have an awk command that I am using, and part of it sums COL_9 however when I read the output it is not including decimal places; awk ' BEGIN{FS=OFS=","} NR==1{print;next} {a+=$9 c = $12 d = $18 } END{for(i in a) {split(i,b,";"); print $1, $2, $3, b, $5, $6, b, b, a, $10, $11,... (1 Reply)
Discussion started by: Ads89
1 Replies

9. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies
scanmem(1)						      General Commands Manual							scanmem(1)

NAME
scanmem - locate and modify a variable in an executing process. SYNOPSIS
scanmem [--version] [--help] [--debug] [--backend] target-program-pid DESCRIPTION
scanmem is an interactive debugging utility that can be used to isolate the address of a variable in an executing process by successively scanning the process' address space looking for matching values. By informing scanmem how the value of the variable changes over time, it can determine the actual location (or locations) of the variable by successively eliminating non-matches. scanmem determines where to look by searching for mappings with read / write permission, these are referred to as regions. Users can eliminate regions they believe are likely unrelated to the target variable (for example, located in a shared library unrelated to the variable in question), this will improve the speed of the scan, which can initially be quite slow in large programs. Once a variable has been found, scanmem can monitor the variable, or change it to a user specified value, either once, or continually over a period of time. scanmem works similarly to the " pokefinders " once commonly used to cheat at video games, this function is a good demonstration of how to use scanmem , and is used in the documentation. USAGE
scanmem should be invoked with the process id of the program you wish to debug as an argument. Once started, scanmem accepts interactive commands. These are described below, however entering help at the > prompt will allow you to access scanmem's online documentation. The target-program-pid can be specified in decimal, hexadecimal, or octal using the standard C language notation (leading 0x for hexadeci- mal, leading 0 for octal, anything else is assumed to be decimal). --version Print version and exit. --help Print a short description of command line options then exit. --debug Run in debug mode, more information will be outputted. --backend Work as backend, normal users should not use this paratmeter. COMMANDS
While in interactive mode, scanmem prints a decimal number followed by > , the number is the current number of possible candidates for the target variable that are known. 0 indicates that no possible variables have been eliminated yet. n Where n represents any number in decimal, octal or hexadecimal, this command tells scanmem that the current value of the target variable is exactly n. scanmem will begin a search of the entire address space, or the existsing known matches (if any), eliminating any variable that does not have this value. set [match-id][,match-id,...]=]value[/delay] [...] Set the value value into the match numbers match-id , or if just value is specified, all known matches. value can be specified in standard C language notation. All known matches, along with their match-id's can be displayed using the list command. Multiple match-id's can be specified, separated with commas and terminated with an = sign. To set a value continually, suffix the command with / followed by the num- ber of seconds to wait between sets. You can interrupt the set command with ^C to return to the scanmem prompt. This can be used to sustain the value of a variable which decreases overtime, for example a timer that is decremented every second can be set to 100 every 10 seconds to prevent some property from ever changing. This command is used to change the value of the variable(s) once found by elimination. Please note, some applications will store values in multiple locations. dump <address> <length> [<filename>] Dump the memory region starting from <address> of length <length> into a human-readable format. If <filename> is given, data will be saved into the file, otherwise data will be displayed into stdout in a human readable format write <value_type> <address> <value> Manually set the value of the variable at the speicified address. Names of value_type are subject to change in different versions of scanmem, see more info using the `help write' command. > Tells scanmem that it should eliminate all matched variables that have not increased since the last search. For example, if the value of a variable is known to be zero when a program is executed, but increases over time, this command can be used several times to eliminate vari- ables that have decreased or not changed. < As for > but indicates that the target variable has decreased since the last scan. = As for > but indicates that the target variable has not changed since last scan. snapshot Save a snapshot of existing program state, for use with > , < , and = , although other commands can still be used. list List all the possible candidates currently known, including their address, last known value and possible types. The value in the first col- umn is the match id, and can be used in conjunction with the delete command to eliminate matches. delete [match-id] Delete match match-id , which can be found from the output of the list command. To delete all matches, see the reset command, or to delete all matches associated with a particular library, see the dregion command, which also removes any associated matches. Pleae note that match-ids may be recalculated after matches are removed or added. watch [match-id] Monitor the value of match-id , and print its value as it changes. Every change is printed along with a timestamp, you can interrupt this command with ^C to stop monitoring. pid [new-pid] Print out the process id of the current target program, or change the target to new-pid , which will reset existing regions and matches. lregions List all the known regions, this can be used in combination with the dregion command to eliminate regions that the user believes are not related to the variable in question, thus reducing the address space required to search. The value in the first column is the region-id which must be passed to the dregion command. The size and path (if applicable) is also printed. This can be used to eliminate regions located in shared libraries that are unlikely to be relevant to the variable required. dregion [!][region-id][,region-id][,...] Delete the region region-id , along with any matches from the match list. The region-id can be found in the output of the lregions command. A leading ! indicates the list should be inverted. reset Forget all known matches and start again. shell [shell-command] Execute shell-command using /bin/sh, then return. option <name> <value> Change options in runtime. See `help option` for all possible names/values. version Print the version of scanmem in use. help Print a short summary of available commands. exit Detach from the target program and exit immediately. EXAMPLES
Cheat at nethack, on systems where nethack is not installed sgid. $ scanmem `pidof nethack` info: attaching to pid 13070. info: maps file located at /proc/13070/maps opened. info: 17 suitable regions found. Please enter current value, or "help" for other commands. 0> The 0 in the scanmem prompt indicates we currently have no candidates, so I enter how much gold I currently have (91 pieces) and let scan- mem find the potential candidates. 0> 91 info: searching 0xbfffa000 - 0xc0000000...........ok info: searching 0x401c2000 - 0x401e3000...........ok info: searching 0x401c1000 - 0x401c2000...........ok info: searching 0x401b6000 - 0x401b8000...........ok info: searching 0x401b5000 - 0x401b6000...........ok info: searching 0x40189000 - 0x4018a000...........ok info: searching 0x40188000 - 0x40189000...........ok info: searching 0x40181000 - 0x40183000...........ok info: searching 0x4017f000 - 0x40181000...........ok info: searching 0x40070000 - 0x40071000...........ok info: searching 0x40068000 - 0x40070000...........ok info: searching 0x40030000 - 0x40031000...........ok info: searching 0x40029000 - 0x4002a000...........ok info: searching 0x4001f000 - 0x40020000...........ok info: searching 0x40016000 - 0x40017000...........ok info: searching 0x081d4000 - 0x0820a000...........ok info: searching 0x081b7000 - 0x081d4000...........ok info: we currently have 16 matches. 16> list [ 0] 0x081c1f34 { 91} (/usr/share/games/nethack/nethack) [ 1] 0x081c1780 { 91} (/usr/share/games/nethack/nethack) [ 2] 0x081be436 { 91} (/usr/share/games/nethack/nethack) [ 3] 0x081eeffc { 91} (unassociated, typically .bss) [ 4] 0x081ee0c0 { 91} (unassociated, typically .bss) [ 5] 0x081eddb8 { 91} (unassociated, typically .bss) [ 6] 0x081d6d88 { 91} (unassociated, typically .bss) [ 7] 0x4001fcd3 { 91} (/lib/libnss_compat-2.3.5.so) [ 8] 0x40029fe3 { 91} (/lib/libnss_nis-2.3.5.so) [ 9] 0x40029f8b { 91} (/lib/libnss_nis-2.3.5.so) [10] 0x40029efb { 91} (/lib/libnss_nis-2.3.5.so) [11] 0x40029bff { 91} (/lib/libnss_nis-2.3.5.so) [12] 0x401d18d3 { 91} (unassociated, typically .bss) [13] 0x401d156f { 91} (unassociated, typically .bss) [14] 0x401d120b { 91} (unassociated, typically .bss) [15] 0xbfffd76c { 91} (unassociated, typically .bss) 16> 16 potential matches were found, many of them are clearly unrelated, as they're part of unrelated libraries (libnss_nis.so). We could make scanmem eliminate these manually using the delete command, however just waiting until the amount of gold changes and telling scanmem the new value should be enough. I find some more gold, and tell scanmem the new value, 112. 16> 112 info: we currently have 1 matches. info: match identified, use "set" to modify value. info: enter "help" for other commands. 1> list [ 0] 0x081d6d88 { 112} (unassociated, typically .bss) Only one of the 16 original candidates now have the value 112, so this must be where the amount of gold is stored. I'll try setting it to 10,000 pieces. 1> set 10000 info: setting *0x081d6d88 to 10000... 1> The resulting nethack status: Dlvl:1 $:10000 HP:15(15) Pw:2(2) AC:7 Exp:1 NOTES
scanmem has been tested on multiple large programs, including the 3d shoot-em-up quake3 linux. Obviously, scanmem can crash your program if used incorrectly. Some programs store values in multiple locations, this is why set will change all known matches. BUGS
The first scan can be very slow on large programs, this is not a problem for subsequent scans as huge portions of the address space are usually eliminated. This could be improved in future, perhaps by assuming all integers are aligned by default. Suggestions welcome. The snapshot command uses memory inefficiently, and should probably not be used on large programs. In future this will use a more intelli- gent format. HOMEPAGE
http://code.google.com/p/scanmem/ AUTHORS
Tavis Ormandy <taviso(a)sdf.lonestar.org> http://taviso.decsystem.org/ Eli Dupree <elidupree(a)charter.net> WANG Lu <coolwanglu(a)gmail.com> All bug reports, suggestions or feedback welcome. SEE ALSO
gdb(1) ptrace(2) nethack(6) pidof(8) version 0.08 January 01, 2010 scanmem(1)
All times are GMT -4. The time now is 02:47 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy