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 03-17-2012, 04:23 PM   #1
carlr
LQ Newbie
 
Registered: Mar 2012
Posts: 11

Rep: Reputation: Disabled
Awk variables


Hi,

I am learning Awk and unix programming. I have a file of data, the first column in time. I have parsed the original files into multiple files each containing 1 cycle. I want to normalise the time in each file so that each file starts from time 0 s. Obviously the first cycle starts from 0 s any way.

I have many cycle files to do so i don't want to do this manualy. Eventually this will all be put in a loop so that i can do all the files with the name cycle_data_* in a given folder.

Using Awk how do I set "t_norm" to be the value of $1 of line 1 when it is processing all lines in the file in the below script?

awk -F, "{print ($1-(t_norm)) "," $2 "," $3 "," $4} Cycle_file.txt > Cycle_file_T_normalised.txt


I have also tried doing this another way, whereby I firstly create a file (file_init_time.txt) with the value which needs to be subtracted from the value $1 of each line in the file. However, i am struggling passing values between the awk commands. I have tried different ways but can't get it to work either.

awk -F, -v VAR1='${$1}' file_init_time.txt |& awk -F, '{print ($1-$VAR1) "," $2 "," $3 "," $4}' Cycle_Data_11_T.txt > Cycle_Data_T_Norm.txt


Any help would be much appreciated
I'm using Cygwin.

Carl
 
Old 03-17-2012, 05:04 PM   #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
Hi Carl and welcome to LinuxQuestions!
Quote:
Originally Posted by carlr View Post
Using Awk how do I set "t_norm" to be the value of $1 of line 1 when it is processing all lines in the file in the below script?
Just add a rule to be executed only for the first record, e.g.
Code:
awk -F, 'NR==1{t_norm=$1}{print ($1-(t_norm)) "," $2 "," $3 "," $4}' Cycle_file.txt
where NR is the current record number (a record in awk is a single line of input). Hope this helps.

Last edited by colucix; 03-17-2012 at 06:06 PM.
 
Old 03-17-2012, 05:35 PM   #3
carlr
LQ Newbie
 
Registered: Mar 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
I can't get this to work. There is something wrong with the syntax which i can't seem to solve.

awk -F, '{ {NR==1 t_norm=$1} {print ($1-t_norm) $2 "," $3 "," $4} }' Cycle_Data_11_T.txt > Cycle_Data_11_T_help.txt
 
Old 03-17-2012, 06:05 PM   #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
Quote:
Originally Posted by carlr View Post
I can't get this to work. There is something wrong with the syntax which i can't seem to solve.

awk -F, '{ {NR==1 t_norm=$1} {print ($1-t_norm) $2 "," $3 "," $4} }' Cycle_Data_11_T.txt > Cycle_Data_11_T_help.txt
This is not what I suggested. The expression NR == 1 should be outside the braces.

Edit: sorry, I've corrected misspelled quotes in my previous post. Please, try it now.

Last edited by colucix; 03-17-2012 at 06:08 PM.
 
  


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
Awk variables danicobos Linux - General 7 10-27-2011 09:43 AM
LXer: Using Variables With awk LXer Syndicated Linux News 0 11-17-2010 01:30 AM
[SOLVED] Not able to get external variables in awk vinaytp Linux - Newbie 4 05-25-2010 01:41 AM
Using variables with awk dots Linux - Newbie 4 07-07-2009 03:00 PM
using variables in awk vgr12386 Programming 14 06-24-2009 04:19 AM

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

All times are GMT -5. The time now is 07:38 PM.

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