Extracting information using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting information using awk
# 1  
Old 04-22-2013
Extracting information using awk

I want to write a script that extracts a value from a line of text. I know it can be done using awk but I've never used awk before so I don't know how to do it. The text is:

Mem: 100M Active, 2150K Cache, 500M Buf, 10G Free

I want to extract the free memory value to use as a variable. In this example what I want is 10G.

My initial guess is that it is something about extracting the 8th field using awk. Does this need to be converted from ASCII to be interpreted as a number (10,000,000,000)?
# 2  
Old 04-22-2013
The value of 'free' is worthless without knowing the value of 'cache' too, since cache counts as free memory. If you've been rebooting every time free goes low, you don't need to.
# 3  
Old 04-22-2013
Code:
$ echo Mem: 100M Active, 2150K Cache, 500M Buf, 10G Free | awk '{print $8}'
10G

$ echo Mem: 100M Active, 2150K Cache, 500M Buf, 10G Free | awk '{print substr($8,length($8),1)}'
G

The first line shows how to extract.
The 2nd line pulls the 'G' out. You will need to analyze for all the possibilities - K, M, G, ??? to convert and create a number.
This User Gave Thanks to joeyg For This Post:
# 4  
Old 04-22-2013
Quote:
Originally Posted by joeyg
Code:
$ echo Mem: 100M Active, 2150K Cache, 500M Buf, 10G Free | awk '{print $8}'
10G
 
$ echo Mem: 100M Active, 2150K Cache, 500M Buf, 10G Free | awk '{print substr($8,length($8),1)}'
G

The first line shows how to extract.
The 2nd line pulls the 'G' out. You will need to analyze for all the possibilities - K, M, G, ??? to convert and create a number.
Thanks. That worked a charm Smilie

How do I remove the G from the 10G i.e. only display the 10?
# 5  
Old 04-22-2013
Code:
$ echo Mem: 100M Active, 2150K Cache, 500M Buf, 10G Free | awk '{print substr($8,1,length($8)-1)}'
10

This User Gave Thanks to joeyg For This Post:
# 6  
Old 04-22-2013
If you want the value converted to a number, you can try something like:
Code:
awk '
BEGIN { factor=1000     # change 1000 to 1024 if using ISO/IEC values
}
function f(v,   ind) {
        ind = index("KMGTPE", substr(v, length(v), 1))
        return(substr(v, 1, length(v) - 1) * (factor ^ ind))
}
{       printf("%s\n", $8 ~ /^[0-9]*[KMGTPE]$/ ? f($8) : $8)
}' input.txt

As always, if you're using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.

I left off the multipliers for zettabyte (Z) and yottabyte (Y) because the floating point types usually used by awk implementations don't have enough precision to compute the correct value in those ranges. If a multiplier given in the input is not known, it is printed unchanged. With the input file:
Code:
Mem: 100M Active, 2150K Cache, 500M Buf, 990 Free
Mem: 100M Active, 2150K Cache, 500M Buf, 991K Free
Mem: 100M Active, 2150K Cache, 500M Buf, 992M Free
Mem: 100M Active, 2150K Cache, 500M Buf, 993G Free
Mem: 100M Active, 2150K Cache, 500M Buf, 994T Free
Mem: 100M Active, 2150K Cache, 500M Buf, 995P Free
Mem: 100M Active, 2150K Cache, 500M Buf, 996E Free
Mem: 100M Active, 2150K Cache, 500M Buf, 997Z Free
Mem: 100M Active, 2150K Cache, 500M Buf, 998Y Free
Mem: 100M Active, 2150K Cache, 500M Buf, 999u Free

the above script produces the following output:
Code:
990
991000
992000000
993000000000
994000000000000
995000000000000000
996000000000000000000
997Z
998Y
999u

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting information from XML to excel

Hi, I am trying to extract information from a XML file and write it to a excel sheet. I am not sure where to start from. Here is the content from my input XML file. <com.cloudbees.hudson.plugins.folder.properties.FolderProxyGroupContainer plugin="nectar-rbac@4.5"> <groups> ... (4 Replies)
Discussion started by: Sajjadmehdi
4 Replies

2. Shell Programming and Scripting

[Solved] Extracting information from DDL's

Dear Experts, I need your help here. I have lot of teradata DDL's as follows, i want to extract field names , field attributes and NOT NULL information from DDL.Could you please help here. Sample DDL: CREATE MULTISET TABLE APS_CALL_IN_PICKUP_CANCELED ,NO FALLBACK , NO BEFORE... (2 Replies)
Discussion started by: srikanth38
2 Replies

3. Shell Programming and Scripting

Problems extracting some information

Hi there! Well, I'm writing a script to obtain certain information about files. Specifically, I want to get the information about those files which last access were in the last 24 hours, so I'm doing something like this: find <directory_name> -atime -1 -printf '%f %a\n' I would also... (4 Replies)
Discussion started by: Skirmish
4 Replies

4. Shell Programming and Scripting

Extracting relevant information from syslogs.

I need to analyse some syslogs and I want to print out all the lines containing SSH connections to the inside interface of the firewall and ignore lines where the originating port is 22. So basically I want to print all matches after "to inside:" that contains /22 and ignore lines where /22 occur... (2 Replies)
Discussion started by: lewk
2 Replies

5. Shell Programming and Scripting

extracting information from multiple files

Hello there, I am trying to extract (string) information ( a list words) from 4 files and then put the results into 1 file. Currently I am doing this using grep -f list.txt file1 . and repeat the process for the other 3 files. The reasons i am doing that (a) I do know how to code (b) each file... (4 Replies)
Discussion started by: houkto
4 Replies

6. Programming

extracting information from lines, put them into arrays

hi I need a little help writing this small perl script. I'm trying to extract the values from each line in a file and find the average for example cat school Highschool 100, 123, 135 Middleschool 41, 67, 54 Elementary 76, 315, 384 ./average.pl highschool: 119.3 middleschool: 54... (2 Replies)
Discussion started by: gengar
2 Replies

7. Shell Programming and Scripting

Extracting information and creating numeric values using awk

I have some variables containing for example m02-npt02-sr40-syn-dv0p01-16x12drw m02-npt02-sr40-syn-dv0p02-16x12drw m02-npt02-sr40-dv0p03-syn-16x12drw I want to extract the dv entry for example dv0p01 dv0p02 dv0p03 Then I want to convert to a numeric, the p specifies the... (5 Replies)
Discussion started by: kristinu
5 Replies

8. Shell Programming and Scripting

Problems with extracting information

Hi all, <select name="comp" id="comp" style="width:130px;"> <?php $sqlcomp = mysql_query("SELECT * FROM comp"); while ($redcomp = mysql_fetch_array($sqlcomp)) { extract($redcomp); echo "<option value=\"$comp_id\">comp_name</option>"; } ?> ... (0 Replies)
Discussion started by: c0mrade
0 Replies

9. UNIX for Dummies Questions & Answers

Extracting information from text fields.

Dear friends, I'm a novice Unix user and I'm trying to learn the ropes. I have a big task I have to accomplish and I'm convinced Unix can get the job done, I just haven't figured out how. I recently posted on the topic of cutting text between unique text patterns and somebody helped me a great... (24 Replies)
Discussion started by: spindoctor
24 Replies

10. Shell Programming and Scripting

Extracting information from a template

I have a template that I usually use to generate stats on an hourly basis for a number of cell sites altogether. I would like to be able to write a script that would go to the template and extract the information for any single site at any time during the day. For example, let's say that my... (4 Replies)
Discussion started by: Ernst
4 Replies
Login or Register to Ask a Question