LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 06-22-2009, 08:06 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Rep: Reputation: 43
regex needed (?)


I have a text file /export/home/tester/file
Code:
Sbalbalbav
erypkerioghjy
tyilerjgyioerjgy
erterilyer
r;yke5ophy
rghryrxxxxxx 
WHERE
UINETDATA_DAILY.begT >= 1245103200000 AND UINETDATA_DAILY.begT < 1245189600000 AND
ufhwerukgher
rklgherio;
What I need to do here is to change/replace values at UINETDATA_DAILY.begT >=
and UINETDATA_DAILY.begT <

1st value should be replace with 1245189600000 (1245103200000+86400000)
2nd value should be replace with 1245276000000 (1245189600000+86400000)

Can anybody help me with that please ?
 
Old 06-22-2009, 08:39 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
Code:
awk '/^UINETDATA/{
  # Read the value of nit
  for (i=1; i<=NF; i++){
  if ($i == ">=")
     gsub($(i+1),$(i+1)+86400000,$(i+1))
  if ($i == "<")
     gsub($(i+1),$(i+1)+86400000,$(i+1))
  }
}1' /export/home/tester/file
Use nawk if running on Solaris.

Last edited by colucix; 06-22-2009 at 08:41 AM.
 
Old 06-25-2009, 03:06 AM   #3
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
MAGIC!

Can you help me to better understand code ?
My comments/questions followed by ##

Code:
## looking for line with string UINETDATA in /export/home/tester/file
nawk '/^UINETDATA/{

  # Read the value of nit

## some loop (?)
  for (i=1; i<=NF; i++){

## if the sign next to UINETDATA is ">=" then to $i... which is the value after ">=" sign ?
  if ($i == ">=")
     gsub($(i+1),$(i+1)+86400000,$(i+1))

  if ($i == "<")
     gsub($(i+1),$(i+1)+86400000,$(i+1))
  }
}1' /export/home/tester/file

Last edited by czezz; 06-25-2009 at 03:07 AM.
 
Old 06-25-2009, 04:11 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
The loop parses field after field
Code:
for (i=1; i<=NF; i++)
it loops from the first field to the last (e.g. the NF-th field). The line containing UINETDATA in your example has 8 fields. Then it checks if the i-th field is ">=" and substitute the next field (which is the value you want) with itself plus 86400000. The same when it encounters the "<" sign. Take in mind that the gsub function actually change the value of the field, so that when the line is printed out the new value is shown.

The print out of every line is performed by the 1 at the end of the awk script. It is simply an expression always true. In that case you don't need to use the print statement, since awk performs a print as "default action". In other words, doing
Code:
1
or
Code:
{print}
is the same.
 
  


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
Help with regex tbeehler Linux - Software 4 07-11-2008 10:05 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
regex help siyisoy Programming 4 04-07-2006 05:32 AM
Regex Help cmfarley19 Programming 5 03-31-2005 10:13 PM
Regex help needed in perl Dr Twox Programming 2 08-07-2004 05:58 AM

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

All times are GMT -5. The time now is 09:23 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