awk string-function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk string-function
# 8  
Old 07-05-2014
solved

Bloke, at least one step ahead with the statement, no matter whether or not the format looks floating point, hexadecimal, it works fine this part of the if-else-if-ladder. (sorry for the term bloke :-) The solution for me: regex.

Code:
awk '$4 ~ /^Juni$/  {if ($2>max) max=$2; ++n; s+=$2}  END { print "Maximum\t" (max), "Average\t" (s/n)};' datafile.txt

# 9  
Old 07-05-2014
I am very glad to hear that you have solved your problem. (Even though I don't see anything here that is at all related to trying to convert abbreviated month names to unabbreviated month names that seemed to be the focus of the first post in this thread.)

The script you have shown us as your solution, will produce absolutely no output with the sample input you provided (since Juni did not appear anywhere in your sample input).

And, the maximum and average your script is printing is not the maximum and average of the values in field 2 of the matched lines! For example, with your code:
Code:
awk '$4 ~ /^Juni$/  {if ($2>max) max=$2; ++n; s+=$2}  END { print "Maximum\t" (max), "Average\t" (s/n)};' datafile.txt

and datafile.txt containing:
Code:
webster    2:48, 29.06.2014 Juni
webster    3:50, 29.06.2014 Juni
webster   10:59, 29.06.2014 Juni

you get the output:
Code:
Maximum	3:50, Average	5

even though the maximum is 10:59 instead of 3:50, and the average of 2 hours 48 minutes, 3 hours 50 minutes, and 10 hours 59 minutes is 5.8721666666 hours (5 hours, 52 minutes, and just under 20 seconds) instead of exactly 5 hours.

Of course, I already provided you with a function to convert strings of the form hh:mm, to an integral number of minutes (see awk last n lines of file) which you can add together and divide to compute averages and I provided examples to print those averages as hours, minutes, and seconds or as a floating point number of minutes. If you wanted hours, you could easily divide the minutes by 60.

I hope this helps you get what you want. Since you have refused to ever show us the output you wanted to produce in either of these threads, and since you don't seem to find my comments helpful, I won't be replying to any more of your posts.
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

String Function in UNIX

Hi - Have file1 which has the below date 08/31/2018 And also have file2 which has the below texts ASOF:<CMODate> FUND I need to read the second file if it has colon (:) then move the date from first file to second file like this ASOF:08/31/2018 have used cut -d":" -f1 and moved the... (2 Replies)
Discussion started by: Mohan0509
2 Replies

2. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

How do I get the first string value from function?

Hello All, I am trying to get the value "node01_mymachine" and disregard the rest of the returned string (command ran*) from myscript.sh $ myscript.sh GetNodeName node01_mymachine Command ran successfully. If I called from another script like this: anyprocess=`myscript.sh... (2 Replies)
Discussion started by: msetjadi
2 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

6. Programming

Is this string splitting function OK?

Hello, I am recently working on an application that sends large strings accross a network very often. These then need to be broken up first with '!' and then with ','. My current function (below) works fine for this when not too much data is being sent across the network but segfaults when a... (4 Replies)
Discussion started by: kpedersen
4 Replies

7. Shell Programming and Scripting

function returns string

Can I create a function to return non-interger value in shell script? for example, function getcommand () { echo "read command" read command echo $command } command=$(getcommand) I tried to do something as above. The statement echo "read command" does not show up. ... (5 Replies)
Discussion started by: lalelle
5 Replies

8. Shell Programming and Scripting

Passing string from function with '*'

Hi I have a shell function which returns string(ksh). The string is an sql statement. This statement can have '*' in its content (i.e. select 100 / 2 *100 from dual). When this happens ret_str will have contents of current directry I run the script from build in sql. Is there any way to fix it... (2 Replies)
Discussion started by: zam
2 Replies

9. Programming

string returning function

I have two string returning function in ESQL/C char *segment_name(lbuffer) char *lbuffer; {..... and char *get_bpdvalue(f_name) char *f_name; {...... both declared above main() char *get_bpdvalue(); char *segment_name(); my problem is segment_name works on sprintf and strcpy... (5 Replies)
Discussion started by: jisc
5 Replies

10. Programming

string function

I have a question concerning string functions. I have not been able to locate a function that does what I want, so I fugured I'd ask before I wrote on myself. Is there a function to which I can pass 2 strings (character string a and character string b) and have it tell me if string b appears... (7 Replies)
Discussion started by: jalburger
7 Replies
Login or Register to Ask a Question