LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-25-2017, 03:47 PM   #1
xisan
LQ Newbie
 
Registered: Apr 2017
Posts: 2

Rep: Reputation: Disabled
Smile Using awk to do calculation inside a text file


Hi,

I'm very new in Linux operating system. I've started looking at different types scripting and trying to manipulate one of the large text files that I'm using to run a simulation of coastal flooding. Following is the text file:

Code:
FileVersion      = 1.03
Filetype         = meteo_on_curvilinear_grid
 TIME = 0 hours since 1900-01-01 12:00:00 +00:00
 -6.855  -6.828  -6.801  -6.774  -6.747  -6.719  -6.691  -6.663  -6.634  -6.606  -6.577  -6.548  -6.519  -6.489  
 TIME = 0 hours since 1900-01-01 12:00:00 +00:00
 -6.855  -6.828  -6.801  -6.774  -6.747  -6.719  -6.691  -6.663  -6.634  -6.606  -6.577  -6.548  -6.519  -6.489  
 TIME = 0 hours since 1900-01-01 12:00:00 +00:00
 -6.855  -6.828  -6.801  -6.774  -6.747  -6.719  -6.691  -6.663  -6.634  -6.606  -6.577  -6.548  -6.519  -6.489
TIME = 0 hours since 1900-01-01 12:00:00 +00:00
 -6.855  -6.828  -6.801  -6.774  -6.747  -6.719  -6.691  -6.663  -6.634  -6.606  -6.577  -6.548  -6.519  -6.489
The file is very large; around 78 MB. I just shortened it to the first few lines. If you notice carefully, all the times are showing 0 hours and for 1900-01-01 12:00:00 +00:00 time periods. I wanted all the 1900-01-01 12:00:00 +00:00 to change in to 2016-10-03 12:00:00 +00:00 . And I did this by using
Code:
sed -i 's/1900-01-01/2016-10-03/g' file.txt
but I also want to change all the time which are showing 0 hours
Code:
TIME = 0 hours since
to change 6 hourly intervals so that it becomes like this:
Code:
TIME = 0 hours since 
TIME = 6 hours since
TIME = 12 hours since
TIME = 18 hours since
The output that I'm trying to get finally is something like this:

Code:
FileVersion      = 1.03
Filetype         = meteo_on_curvilinear_grid
 TIME = 0 hours since 2016-10-03 12:00:00 +00:00
 -6.855  -6.828  -6.801  -6.774  -6.747  -6.719  -6.691  -6.663  -6.634  -6.606  -6.577  -6.548  -6.519  -6.489  
 TIME = 6 hours since 2016-10-03 12:00:00 +00:00
 -6.855  -6.828  -6.801  -6.774  -6.747  -6.719  -6.691  -6.663  -6.634  -6.606  -6.577  -6.548  -6.519  -6.489  
 TIME = 12 hours since 2016-10-03 12:00:00 +00:00
 -6.855  -6.828  -6.801  -6.774  -6.747  -6.719  -6.691  -6.663  -6.634  -6.606  -6.577  -6.548  -6.519  -6.489
TIME = 18 hours since 2016-10-03 12:00:00 +00:00
 -6.855  -6.828  -6.801  -6.774  -6.747  -6.719  -6.691  -6.663  -6.634  -6.606  -6.577  -6.548  -6.519  -6.489
Is it possible to conduct both the operations by using a single loop combining sed and awk operations. Can anyone suggest me which options would be best here? That'd be very helpful for my work.

Thanks in advance.
 
Old 04-25-2017, 04:35 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,179

Rep: Reputation: 533Reputation: 533Reputation: 533Reputation: 533Reputation: 533Reputation: 533
You need a single loop.
sed does not handle variables, so this one is better for awk:
Code:
awk 'BEGIN {t=0} ($1=="TIME" && $2=="=" && $3=="0" && $6=="1900-01-01") {$3=t; t+=6; $6="2016-10-03"} {print}' file.txt
If this looks good, redirect its output to a new(!) file.
As a last step you can copy or move the new file back to the original file.
 
1 members found this post helpful.
Old 04-25-2017, 06:46 PM   #3
xisan
LQ Newbie
 
Registered: Apr 2017
Posts: 2

Original Poster
Rep: Reputation: Disabled
That worked perfectly, thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] What does !# <text> mean inside a file? Gremlin022 Linux - Newbie 4 07-09-2014 12:01 PM
[SOLVED] end to end delay calculation in satellite links awk file problem-ns2.35 rianariana Linux - Software 3 11-23-2013 09:46 AM
[SOLVED] How to replace text using sed or awk in a file without changing the commented text coolpraz Programming 4 01-03-2013 06:19 AM
parsing a text file - to awk or not to awk ? rollyah Programming 9 08-18-2011 02:20 PM
[SOLVED] How to read a file inside AWK? NabiVakili Programming 7 04-17-2011 03:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:56 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration