The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How can i read a non text file in unix - ELF-64 executable object file - IA64 alexcol UNIX for Advanced & Expert Users 8 11-07-2008 08:56 AM
Post Shell programming: Question about source a file and read data from the file ccwq Shell Programming and Scripting 3 08-04-2007 10:28 PM
Read words from file and create new file using K-shell. bsrajirs Shell Programming and Scripting 4 06-01-2007 12:15 PM
Read xml file ram2s2001 Shell Programming and Scripting 2 12-12-2005 12:20 AM
How to read specific lines in a bulk file using C file Programming rajan_ka1 High Level Programming 10 11-10-2005 03:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-25-2008
giorgos193 giorgos193 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 17
Help with awk - read from file

Hi,

I've got a file like the following:

Code:
Starting to process segment 0 (and symmetry related segments)
	Number of (cancelled) singularities: 0
	Number of (cancelled) negative numerators: 0
	Segment 0: 5.49secs
Starting to process segment 1 (and symmetry related segments)
	Number of (cancelled) singularities: 0
	Number of (cancelled) negative numerators: 0
	Segment 1: 10.01secs
Starting to process segment 2 (and symmetry related segments)
	Number of (cancelled) singularities: 0
	Number of (cancelled) negative numerators: 0
	Segment 2: 7.82secs
Starting to process segment 3 (and symmetry related segments)
	Number of (cancelled) singularities: 0
	Number of (cancelled) negative numerators: 0
	Segment 3: 5.31secs
... and I would like to extract the information about the time. I would like to read the file, save the time (of each line) to a variable, remove the suffix secs and then add it to the previous value. The result will be the total time....

I tried many things but none of them works... In case it helps, the last one I tried was

Code:
 
TIME="`awk '/Segment/ {print $3%secs + $TIME} ' log.txt`"

echo $TIME
It's a silly solution, but I think it may convey what I want to do....
I suspect that it has something to do with reading each line separetely, but I can't figure it out...

Thanks a lot in advance...
  #2 (permalink)  
Old 04-25-2008
grumpf grumpf is offline
Registered User
  
 

Join Date: May 2006
Posts: 101
this works for my:

awk -F":" '/Segment/ { print $2}' file.txt
5.49secs
10.01secs
7.82secs
5.31secs

pure shell:

grep Segment file.txt | cut -d: -f2
  #3 (permalink)  
Old 04-25-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
Code:
awk -F:  '{ if(index($0,"Segment")>0){
            tot+=$2
            }
         }
         END {print tot }' file
  #4 (permalink)  
Old 04-25-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Try this:
Code:
awk '
/Segment/{match($3,".");t+=substr($3,RSTART)}
END{print "Totaltime :"t}
' file
Regards
  #5 (permalink)  
Old 04-25-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Even:

Code:
awk '/Segment/{t+=$3}END{print "Totaltime :"t}' file
Regards
  #6 (permalink)  
Old 04-25-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
For adding the values, is it enough if you get the sum at the end? Then awk is all you need:

Code:
awk '/Segment/ { gsub("secs",""); time += $3 } END { print time }' file.txt
If you really genuinely want to loop over the cumulative times and do something which each in the shell, that would be something like

Code:
awk '/Segment/ { gsub ("secs",""); time += $3; print $3, time } file.txt |
while read THISTIME SUMTIME; do
  echo This time, we got $THISTIME
  echo Sum so far is $SUMTIME
done
The shell's arithmetic facilities are integer only, so you can't sum numbers with fractional parts in the shell itself. Getting awk to print both the current and the cumulative value for each line is a nice workaround for that (and a good opportunity to demonstrate how to read multiple values from the same line in the shell).
  #7 (permalink)  
Old 04-25-2008
giorgos193 giorgos193 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 17
I want to thank you all for you rapid response! All solutions are great! I really appreciate your effort!

@ era: I'm really impressed by your "sophisticated" solution!

Thanks again!!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 03:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0