LinuxQuestions.org
Help answer threads with 0 replies.
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-19-2018, 04:27 PM   #1
rshepard
Member
 
Registered: Oct 2007
Location: Troutdale, Oregon
Distribution: Slackware
Posts: 435

Rep: Reputation: 28
gawk: replace second instance of a value


I have many files of hourly values over the entire year. The awk script I used to add the hour to the date and data values had an error: I entered 16:00 (4pm) twice and now I need to change the second instance to 17:00. I have no prior experience with awk flags so this is the latest iteration of my script:

#!/usr/bin/gawk
# This script replaces the second instance of 16:00 with 17:00.

BEGIN { FS=","; OFS="," }
$2!="16.00" { print }
$2=="16:00" { print ; flag=1 }
$2=="16:00" { $2=="17:00"; flag=0; print }

It prints the input file without change.

This is a test data set, test.dat:

2012-10-01,14:00,90.7999
2012-10-01,15:00,90.8121
2012-10-01,16:00,90.8121
2012-10-01,16:00,90.8121
2012-10-01,18:00,90.8091
2012-10-01,19:00,90.8030

Each file is 8,761 lines long (a header line is on the top), and I have a dozen or so years that need correcting. I want to learn how to do this correction with awk.
 
Old 06-19-2018, 05:34 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,150

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
I guess I would do it like this:
Code:
#!/usr/bin/gawk
# This script replaces the second instance of 16:00 with 17:00.

BEGIN { FS=","; OFS="," }
      { if ($2 == "16:00") {if (flag == 1) {$2 = "17:00"};
                            flag = 1} 
        else {flag=0}
        print}
 
1 members found this post helpful.
Old 06-20-2018, 08:57 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Please remember to use [code][/code] tags around your code to preserve formatting and readability.

You were already fairly close, you just needed to use your flag variable to test when to make the change to $2
Code:
#!/usr/bin/gawk -f

BEGIN { FS=","; OFS="," }

$2 == "16:00"{
  if(++f%2 == 0)
    $2 = "17:00"
}

1

Last edited by grail; 06-20-2018 at 08:59 AM.
 
1 members found this post helpful.
Old 06-20-2018, 10:31 AM   #4
rshepard
Member
 
Registered: Oct 2007
Location: Troutdale, Oregon
Distribution: Slackware
Posts: 435

Original Poster
Rep: Reputation: 28
Smallpond/Grail:

Thank you both for solutions. I've not before used a flag in awk and you both helped me understand how to use them.

Regards,

Rich
 
Old 06-21-2018, 06:28 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Based on your original code
Code:
BEGIN { FS=","; OFS="," }
$2=="16:00" && flag==1 { $2=="17:00"; flag=0 }
$2=="16:00" { flag=1 }
{ print }
Note the order: 1. test the flag 2. set the flag
So 1. can only be true in a following line.

Last edited by MadeInGermany; 06-21-2018 at 06:30 PM.
 
  


Reply

Tags
awk, flags, gawk



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
Can't ping or ssh to the cirros instance of the openstack instance visuharsha Linux - Networking 0 12-21-2017 01:44 PM
Postfix multiple instance only default instance work Dante K Linux - Server 0 09-05-2017 09:45 AM
[SOLVED] sed: how to replace only the first instance of a line RandomTroll Linux - Newbie 6 11-16-2015 04:07 AM
[SOLVED] sed, grep,gawk :: Finding First Instance Of String Before A Given Line Number cin_ Programming 13 05-28-2014 07:04 AM
[SOLVED] sed to replace first and second instance mhouston100 Linux - Newbie 6 07-18-2011 02:00 AM

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

All times are GMT -5. The time now is 12:07 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
Open Source Consulting | Domain Registration