Visit Our UNIX and Linux User Community


AWK and sub/gsub: updating a date/time block


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK and sub/gsub: updating a date/time block
# 1  
Old 10-23-2009
AWK and sub/gsub: updating a date/time block

I have a file ("modtest") in which I want to update the last date/time block in the lines beginning with a period. Here is a sample:

Code:
.ROMULT 10150908EDT 10270908EDT 10010908EDT   RANGE
RAWV2    1.00
.ROMULT 10150908EDT 10270908EDT 10010908EDT   FGROUP
CHOWANRV 1.00
.RRIMULT 10150908EDT 10270908EDT 10010908EDT
RAWV2    1.13
.RRIMULT 10150908EDT 10270908EDT 10010908EDT   FGROUP
CHOWANRV 1.13
.TSCHNG 10190914EDT  10010908EDT
RAWV2    RAWV2    QINE 6 49.72 48.42 47.11 45.81 44.51 43.20 42.43 &
 41.65 40.88 40.10 39.32 38.55 38.31 38.08 37.85 37.62 37.38 37.15 &
 36.92 36.69 36.55 36.41 36.27 36.13 35.99 35.85 35.71 35.57 &
 PLOT-TUL

These lines have anywhere from 2 to 5 fields. The date/time block I want to update will either be the last or next-to-last field in the line.

Code:
awk ' ( NF == 5 ) && ( /^\./ ) {  gsub(/[0-1][0-9][0-3][0-9][0-9][0-9][0-2][0-9][E][DS][T]/, dateblock, $4) ; print } ' modtest

The dateblock variable is mmddyy, two digits for hour, and EST or EDT.

When I run this command, the lines that have 5 fields and begin with a period are changed as follows, but the 4th field that I meant to update does not appear at all:

Code:
.ROMULT 10150908EDT 10270908EDT   RANGE
.ROMULT 10150908EDT 10270908EDT   FGROUP
.RRIMULT 10150908EDT 10270908EDT   FGROUP
.SWITCHTS 10150908EDT 10270908EDT   FGROUP

I have tried various things with sub and gsub, including a less-specific regular expression, and that didn't work either. I'm very new to awk, so trying to figure this out has been a good learning experience, but I'm hoping to "cut to the chase" now... Smilie Any help appreciated. Thanks!

Last edited by zaxxon; 10-23-2009 at 10:44 AM.. Reason: code tags also for data and logs please, ty.
# 2  
Old 10-23-2009
how do you pass the 'dateblock' variable into awk?
For example
Code:
awk ' ( NF == 5 ) && ( /^\./ ) {  gsub(/[0-1][0-9][0-3][0-9][0-9][0-9][0-2][0-9][E][DS][T]/, dateblock, $4) ; print } ' dateblock=$(date +%Y%m%d) modtest

# 3  
Old 10-23-2009
I defined dateblock earlier in the program, before the awk statement. Unfortunately, the desired hour number is not just system time, but one of (02, 08, 14, 20) under EDT and (01, 07, 13, 19) under EST. I have code earlier in the program that figures out which one is appropriate based on the current system time.
# 4  
Old 10-23-2009
Quote:
Originally Posted by chrismcg24
I defined dateblock earlier in the program, before the awk statement. Unfortunately, the desired hour number is not just system time, but one of (02, 08, 14, 20) under EDT and (01, 07, 13, 19) under EST. I have code earlier in the program that figures out which one is appropriate based on the current system time.
Then just pass it into awk as noted above.
# 5  
Old 10-23-2009
OK, that helped -- thanks!

I was under the impression that gsub would make the substitutions in the matching lines in the input file itself, but that the output would contain both the changed and unchanged lines. All I've been able to make it do so far is output the lines it changes. By chance are there any switches in gsub that might make it do what I want, or do I need to figure out some other way to make it pick up the additional unchanged lines in the proper order?
# 6  
Old 10-23-2009
Code:
awk ' ( NF == 5 ) && ( /^\./ ) {  gsub(/[0-1][0-9][0-3][0-9][0-9][0-9][0-2][0-9][E][DS][T]/, dateblock, $4) } 1' dateblock=$(date +%Y%m%d) modtest

# 7  
Old 10-29-2009
I have been unable to find documentation to tell me how that works exactly -- it seems to print the entire file as well as the changed lines. I can get rid of the duplicates with uniq, but since I need to run a similar command on the same file 4 times, is there a way to have it only print the entire file once?

Previous Thread | Next Thread
Test Your Knowledge in Computers #645
Difficulty: Easy
The first web browser, WorldWideWeb, was invented on the NeXTSTEP platform.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Validate date and time in filename by awk

hi i want to validate the date and time in filename filename : mohan.moh.ccyymmdd.ccyymmdd.hhmmss.txt mohan_moh.20151222.20151222.122442.txt i want code that check that date given in filename 20151222 in this format ccyymmdd else it mark file is not valid used in my OS detail is AIX 6... (12 Replies)
Discussion started by: MOHANP12
12 Replies

2. Shell Programming and Scripting

awk gsub

Hi, I want to print the first column with original value and without any double quotes The output should look like <original column>|<column without quotes> $ cat a.txt "20121023","19301229712","100397" "20121023","19361629712","100778" "20121030A","19361630412","100838"... (3 Replies)
Discussion started by: ysrini
3 Replies

3. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk. I tried This is how each line of the file looks like, different date and time in this format Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th ... (2 Replies)
Discussion started by: bkkid
2 Replies

4. Shell Programming and Scripting

Access log field - using awk to pull date/time

hey guys. the following line is a line taken from apache's access_log 10.10.10.10 - jdoe "GET /images/down.gif HTTP/1.1" 304 I'm concerned about the field that has the date and time in it. if assuming the delimiter in the file is a space, then the fourth field will always have the date... (5 Replies)
Discussion started by: SkySmart
5 Replies

5. Shell Programming and Scripting

Using awk or nawk to convert epoch time to date format

Looking for some help and usually when I do a search this site comes up. Hopefully someone can give me a little direction as to how to use one of these two commands to achieve what I'm trying to do. What am I trying to do? I need to take the time value in epoch format returned from the... (5 Replies)
Discussion started by: minigts
5 Replies

6. Shell Programming and Scripting

Date and time range extraction via Awk or analysis script?

Hello does anyone know of an awk that will extract log file entries between a specific date and time range, eg: awk '/15\/Dec\/2010:16:10:00/, /15\/Dec\/2010:16:15:00/' access_log but one that works? Or a free command line log file analysis tool/script? I'd like to be able to view... (2 Replies)
Discussion started by: competitions
2 Replies

7. Shell Programming and Scripting

awk & date time format

This gives: grep "qprt -o -dp -N 1 -P" printfile.log.1216 |awk '{print $7, $9}' |grep lpc1z The output like below: lpc1z /cerner/d_prod/print/ccltemp4235396d030a.dat.tmp.1216075330] lpc1z /cerner/d_prod/print/ccltemp4235396d036a.dat.tmp.1216075634] lpc1z... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

8. Shell Programming and Scripting

awk date and time validation

How can i validate if user inserted date and optionly a time is vaild but in awk scripting? (18 Replies)
Discussion started by: tal
18 Replies

9. Shell Programming and Scripting

awk updating one file with another, comparing, updating

Hello, I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea... (11 Replies)
Discussion started by: mecano
11 Replies

10. Shell Programming and Scripting

awk gsub

Hi all I want to do a simple substitution in awk but I am getting unexpected output. My function accepts a time and then prints out a validation message if the time is valid. However some times may include a : and i want to strip this out if it exists before i get to the validation. I have shown... (4 Replies)
Discussion started by: pxy2d1
4 Replies

Featured Tech Videos