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
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Homework & Coursework Questions 1 10-08-2009 02:27 PM
compare file modification date/time to current date/time Bill Ma Shell Programming and Scripting 5 08-27-2009 01:02 PM
how to convert date time to epoch time in solaris anshuman0507 Shell Programming and Scripting 6 08-22-2009 11:28 PM
Convert Epoch Time to Standard Date and Time & Vice Versa DrivesMeCrazy Shell Programming and Scripting 5 02-07-2009 01:40 AM
Processing a log file based on date/time input and the date/time on the log file primp Shell Programming and Scripting 4 03-16-2008 11:23 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-23-2009
chrismcg24 chrismcg24 is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
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... 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 (permalink)  
Old 10-23-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
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 (permalink)  
Old 10-23-2009
chrismcg24 chrismcg24 is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
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 (permalink)  
Old 10-23-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Quote:
Originally Posted by chrismcg24 View Post
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 (permalink)  
Old 10-23-2009
chrismcg24 chrismcg24 is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
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 (permalink)  
Old 10-23-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
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 (permalink)  
Old 4 Weeks Ago
chrismcg24 chrismcg24 is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
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?
Reply

Bookmarks

Tags
awk, date/time, gsub

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 06:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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