awk - change variable on fly


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk - change variable on fly
# 29  
Old 04-21-2012
my script:
# grep -v ^# letsgo.sh
DateStart="2012/01/01"
DateEnd="2012/04/15"
start=`date +%s -d "$DateStart"`
end=`date +%s -d "$DateEnd"`
gawk -F '^"|","|"$' -v start=$start '{cmd="date +%s -d \"" $10 "\""; cmd | getline current; close(cmd)} current > start ' file.csv > temp.file
# grep -v ^# letsgo.sh

my basic file contains very similiar records like below:

Code:
# cat lol.tmp
"aaaaa","bbbbbb","ccccc","ddddd,eeeee","ffffff","ggggg","3","2012/02/22 12:25:21","2012/02/22 00:00:00","8","hhhh","iiii"
#

I need to count records which have second date (in above example it is "2012/02/22 00:00:00") between given dates (variables $end and $start) and redirect result to temporary file for future modifications.

I saw ready scripts for compare dates (loop in loop), but I thought it will be faster to change dates into unix timestamp and compare. I guess awk is a best tool to such operation, but now I have doubts ;/

If you have better idea - I am willing to listen Smilie

Thanks in advance for any tips.

Kind regards
# 30  
Old 04-21-2012
Thanks for your reply but I'm afraid you failed to answer to the questions I asked.
Here they are again:
  • Can you provide a sample input file with more than one line ?
  • Are the records ordered ?
# 31  
Old 04-21-2012
1. this might be sample input file:
Code:
"aaaaa","bbbbbb","ccccc","ddddd,eeeee","ffffff","ggggg","3","2012/02/22 12:25:21","2012/02/22 00:00:00","8","hhhh","iiii"
"aaaaa","bbbbbb","ccccc","ddddd,eeeee","ffffff","ggggg","2","2012/02/22 12:25:21","2011/02/22 00:00:00","8","hhhh","iiii"
"aaaaa","bbbbbb","ccccc","ddddd,eeeee","ffffff","ggggg","2","","2010/02/22 00:00:00","8","hhhh","iiii"
"aaaaa","bbbbbb","ccccc","ddddd,eeeee","ffffff","ggggg","1","2012/02/22 12:25:21","2009/02/22 00:00:00","8","hhhh","iiii"

2. no order. only a lots of records (at least 40 MB) Smilie
# 32  
Old 04-21-2012
Is there always a colon inside field 4 ?
Is the HH:MM:SS always 00:00:00 in field 9 ?
# 33  
Old 04-21-2012
1. yes, colon might appear in field 4 ( generally this field contain input from user, so here can be everything.... thats why I have weird separator in awk)
2. field 9 always contains "00:00:00". however if it is possible - please do not skip this part. In my next script I have to work with fields where this part is different then "00:00:00"
# 34  
Old 04-21-2012
Here is something probably much faster than your previous attempts:
Code:
startDate="2012/01/01 00:00:00"
endDate="2012/04/15 00:00:00"
gawk -F'","' \
    -v start=${startDate//[\/ :]/} \
    -v end=${endDate//[\/ :]/} '
{
    a=gensub("[/ :]","","g",$9);
    if((a>=start)&&(a<=end))
        print
}' file.csv > extract.csv

This User Gave Thanks to jlliagre For This Post:
# 35  
Old 04-21-2012
LOL ...its working....it is ultra-fast ! Smilie

Many thanks for help jlliagre ! Smilie ( now I have to slowly read and understand your code ;p )

Best regards,
Paweł

Last edited by haczyk; 04-21-2012 at 10:55 AM.. Reason: typo
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Make change to variable value inside of awk script

Hello, I have text data that looks like this, Mrv16a3102061815532D 6 6 0 0 0 0 999 V2000 -0.4018 1.9634 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -1.1163 1.5509 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -1.1163 0.7259 ... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

2. UNIX for Dummies Questions & Answers

Change variable value

I have big XML which i want to change all VERSIONNUMBER equal to 1,in existing file values of VERSIONNUMBER will be different as below now i want to change all VERSIONNUMBER values qual to 1.Please help me which will convert versionnumber values. <SHORTCUT OBJECTSUBTYPE ="" OBJECTTYPE ... (5 Replies)
Discussion started by: katakamvivek
5 Replies

3. Shell Programming and Scripting

I can't change the value of my variable!

I made this HEADMAKER variable to pull the header from the first file in the loop, but then to stop so it doesn't override the file with later loops. However, I CANNOT get it to reassign the value of my variable away from "FIRST". I have also tried it with 1 and 0, and with and without quotes and... (3 Replies)
Discussion started by: crankymonkey
3 Replies

4. Shell Programming and Scripting

Change Variable Value from Multiple Scripts and Use these Variable

Hi to All, Please find below details. file_config.config export file1_status="SUCCESS" export file2_status="SUCCESS" file_one.sh I am calling another two shell script from these script. I need to pass individual two script status (If it's "FAILED") to file_main.sh. file_main.sh I... (2 Replies)
Discussion started by: div_Neev
2 Replies

5. Shell Programming and Scripting

Change the filename variable value

Hi guys, I have a variable where i am storing the filename (with full path). I just need the value before ".txt". But instead of getting the filename i am getting the contents of the filename. FileName=/appl/data/Input/US/Test.txt a=`awk -F"." '{print $1}' ${FileName}` echo $a... (3 Replies)
Discussion started by: mac4rfree
3 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. Shell Programming and Scripting

Store Host lookup in variable ("on the fly")

Hi, I'm new here. I was wondering why I can't store a host lookup in a variable. for line in $(< blacklist) do STOREIP=host $line; if ]; then $line >> blacklist2; else $line >> blacklist3; fi done Result: "ip" command not found .. so how would I store the host lookup in the... (2 Replies)
Discussion started by: sOliver
2 Replies

8. Shell Programming and Scripting

awk: change string variable

Hi. How to change string variable in awk? for example, I parse with awk script text file named some_name_with_extension.txt I want to print only some_name in my script .... varCompName = FILENAME print varCompName How to put not all symbols from FILENAME to variable? thank you This... (4 Replies)
Discussion started by: cintlt
4 Replies

9. Shell Programming and Scripting

Change only the name of a variable

if I have a variable named z inside a java file, is there any way to globally change the name of the variable and all its occurences as a variable? (but not any other z that is not part of that variable name) (3 Replies)
Discussion started by: lydiaflamp
3 Replies

10. Shell Programming and Scripting

How to change awk and shell variable value?

In awk script, #!/bin/sh awk 'BEGIN{i=0;}{i=i+5;}END{print i}' in.txt vr=0; vr=$i; echo "$vr" How can i assign that value of i in $vr(variable) of shell script? (7 Replies)
Discussion started by: cola
7 Replies
Login or Register to Ask a Question