LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2012, 10:00 AM   #1
[RMC]Pip
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Rep: Reputation: Disabled
awk add value to column


Hi,

I want to add a value to a specific column of a file if that line starts with a pattern.

Samle file:
Quote:
ExecutionTime = 28.26 s ClockTime = 31 s
blabla
ExecutionTime = 53.7 s ClockTime = 59 s
bla
ExecutionTime = 77.28 s ClockTime = 86 s
yeah
ExecutionTime = 101.14 s ClockTime = 114 s
anyway
ExecutionTime = 123.02 s ClockTime = 140 s
I want to modify the two numerical values.

My utility function for this task:
Code:
timeCorrectLog()
{
	local execTime=$2
	local clockTime=$3

	awk -F '[ \t"#]*' '/^ExecutionTime/{sub($3,$3+'$execTime')}1' $1 > tmpAWK
	awk -F '[ \t"#]*' '/^ExecutionTime/{sub($7,$7+'$clockTime')}1' tmpAWK > timeCorrect.$1
	rm -f tmpAWK
}
The command:
Code:
timeCorrectLog file 86728.8 95351
The output:
Quote:
ExecutionTime = 86757.1 s ClockTime = 95382 s
blabla
ExecutionTime = 86782.5 s ClockTime = 95410 s
bla
ExecutionTime = 95437806.1 s ClockTime = 86 s
yeah
ExecutionTime = 86829.9 s ClockTime = 95465 s
anyway
ExecutionTime = 86851.8 s ClockTime = 95491 s
Clearly, something went very wrong in line 5. I used the script for a huge log file, and it messed up only that line, have no clue what is happening!

In short, this is the command that causes the trouble:
Code:
echo "ExecutionTime = 86806.1 s  ClockTime = 86 s" | awk -F '[ \t"#]*' '/^ExecutionTime/{sub($7,$7+95351)}1'
Thank you!
 
Old 04-23-2012, 10:23 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Using the sub function it substitutes the first occurrence of $7 (that is the string "86") and not $7 itself. In that line there is a "86" at the beginning of the third field:
Code:
ExecutionTime = 86806.1 s  ClockTime = 86 s
You should specify the substitution has to be made in $7:
Code:
sub($7,$7+95351,$7)
but the correct way is simply:
Code:
$7 += 95351
that is the same as
Code:
$7 = $7 + 95351
 
1 members found this post helpful.
Old 04-23-2012, 10:30 AM   #3
[RMC]Pip
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks a lot colucix!
 
Old 04-23-2012, 10:40 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're welcome! An aside note: you can do it using a single awk call:
Code:
#!/bin/bash
#
timeCorrectLog()
{
	local execTime=$2
	local clockTime=$3

	awk -F '[ \t"#]*' '/^ExecutionTime/{$3 += '$execTime'; $7 += '$clockTime'}1' $1 > timeCorrect.$1
}
This should speed-up the process, avoiding the need for the temporary file.
 
Old 04-23-2012, 12:28 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
You may even wish to look at the -v option for awk to set variables to be used within the command.
 
Old 04-24-2012, 02:28 AM   #6
[RMC]Pip
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks, really great help!
 
Old 04-24-2012, 03:42 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Please mark as SOLVED once you have a solution
 
Old 04-24-2012, 05:27 AM   #8
[RMC]Pip
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Cool

Yepp, just waited a bit in case of someone has some more interesting stuff!

Wait is over, thank you all!
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Adding a column with awk Tauro Linux - Newbie 2 07-27-2011 07:41 AM
[SOLVED] Not able to extract last column from awk vinaytp Linux - Newbie 4 05-20-2011 04:27 AM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM
Column statistic by awk ? cs24 Programming 7 01-15-2010 05:41 AM
awk column printing schneidz Programming 7 09-29-2005 06:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration