Replace second match+awk/sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace second match+awk/sed
# 8  
Old 11-22-2013
Wowza! It works! Thanks so much, I've been scratching my head on this problem for a couple days. I think it might help if you wouldn't mind to explain the formatting line by line and what it is doing. I think the logic in my head from my first attempt at the awk statement got scrambled. I think it would help to hear exactly what it was doing.

Also, if I may add a twist...

How would we add in a condition that if it was the last entry for the station, that the close statement date and time would be 12/31/2500 23:59:59? Is there something easy to add in that would do that? Or should I add in some code after this iteration to change the date and time.

For example, for the text sample I gave then it would look like this:
Code:
----------------------------------------- 
sta WP00
 time 10/23/2013 20:10:17 
sensor trillium_240_2 0 583 
add 
close sensor trillium_240_2 10/28/2013 20:20:28  

sensor trillium_120 0 279 
add 
close sensor trillium_120 10/28/2013 20:20:28 
--------------------------------------------- 
sta WP00
 time 10/28/2013 20:20:28
 sensor trillium_240_2 0 583 
add 
close sensor trillium_240_2 12/31/2500 23:59:59  

sensor trillium_120 0 268 
add 
close sensor trillium_120 12/31/2500 23:59:59 
----------------------------------------------

So in effect, it would do what we have done by swapping the times on any of the previous ones (which your code works great for), but then if it was the last station entry (for each station) it would swap it's time for 12/31/2500 23:59:59...make sense?

Last edited by Scrutinizer; 11-23-2013 at 03:41 AM.. Reason: formatting is incorrect on text file data; added code tags (mod)
# 9  
Old 11-22-2013
Try
Code:
tac file | awk '/time/ {X=$NF}  /close/ {if (X) $NF=X; else {$(NF-1)="12/31/2500";$NF="23:59:59"}}  1 '| tac

# 10  
Old 11-23-2013
RudiC,

Your tac code changes the time on the entry before but it doesn't also change the date.

So for the text it reads:
Code:
sta WP00 34.07335 -106.91932 
time 10/23/2013 20:10:17
sensor trillium_240_2 0 583
add
close sensor trillium_240_2 10/23/2013 20:20:28

sensor trillium_120 0 279
add
close sensor trillium_120 10/23/2013 20:20:28

Instead of having 10/28/2013 20:20:28. It's definitely a faster approach but it's missing the date too. I'm not familiar with tac, how would you add in the date element to it? Also the change to close statement to be 12/31/2500 only changes the very last entry, but I would like it to change the very last entry for each station that it runs through. In the example text there is only one station but in the full text file there are many more. Ideas?

And thanks for the help!
Moderator's Comments:
Mod Comment Please use CODE tags and stop depending on site administrator's to fix your posts for you.

Last edited by Don Cragun; 11-23-2013 at 04:26 PM.. Reason: Add CODE tags.
# 11  
Old 11-24-2013
For date and time, try (untested)
Code:
tac file | awk '/time/ {X=$(NF-1);Y=$NF}  /close/ {if (X) {$(NF-1)=X; $NF=Y}; else {$(NF-1)="12/31/2500";$NF="23:59:59"}}  1 '| tac

For the other one, the tac approach might be the wrong one.
# 12  
Old 11-25-2013
Yeah that one now does the dates, but it also swaps the dates from the first and the last one. I just want to swap the date from the one before it. I appreciate the help though, and thanks for introducing me to tac Smilie I will go with the awk solution I think.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern match and replace indirect directory reference using sed

Hi, I need a ksh script to replace indirect directory references in an .ini file with a env variable using sed or awk. The .ini file is for example as such: A=.. B=../ C=../.. D=../../ E=../bin F=../../bin G=../../bin/xml H=../../bin/xml/ Need to replace an instance of .. or... (2 Replies)
Discussion started by: andyatit
2 Replies

2. Shell Programming and Scripting

sed : replace Nth match in a file

I have a situation where a file "config.txt" looks like this Servername: OS: Serername: OS: Servername: OS: .... .... ... Servername: OS: looking for the sed syntax to replace the "Nth" occurrence of Servername (i would apply the same logic to OS as well), want to replace the Nth... (4 Replies)
Discussion started by: alldbest
4 Replies

3. Shell Programming and Scripting

Need help with sed to match and replace a string

friends I am struck in a situation where I need to comment a line start with space as below in a file root@LOCALHOST * rw LOCALHOST* r I should comment second line only Any help please (16 Replies)
Discussion started by: mallak
16 Replies

4. Shell Programming and Scripting

awk Match First Field and Replace Second Column

Hi Friends, I have looked around the forums and over online but couldn't figure out how to deal with this problem input.txt gene1,axis1/0/1,axis2/0/1 gene1,axis1/1/2,axis2/1/2 gene1,axis1/2/3,axis2/2/3 gene2,axis1/3/4,axis2/3/4 Match on first column and if first column is... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

5. Shell Programming and Scripting

sed Character match and replace

Hello All I am struck in the issue which I want to share with all of you. What I am trying to do is For every line in a file I have to replace a particular character from the given character in a file For Example Suppose the data is 1111x2222 1111x2222 2222y3333 1111x2222 I... (4 Replies)
Discussion started by: adisky123
4 Replies

6. UNIX for Dummies Questions & Answers

awk/sed match and extraction

Hi, I have a file like this- aa 12 23 34 aa 21 34 56 aa 78 45 56 I want to print out only the lines after the last aa. How do I do this? I tried using grep -A and sed -n, but both didnt work as I wanted to. Could someone help me out please.. (3 Replies)
Discussion started by: jamie_123
3 Replies

7. Shell Programming and Scripting

Match a Pattern & Replace The value Using AWK

I have a csv file in which i have to search a particular string and replace the data in any column with something else. How do i do it using awk. file ------ 2001,John,USA,MN,20101001,29091.50,M,Active,Y 2002,Mike,USA,NY,20090130,342.00,M,Pending,N... (3 Replies)
Discussion started by: Sheel
3 Replies

8. Shell Programming and Scripting

Match and replace value in 2 different places using awk

Hi, I need help on replacing values in certain field in my file1.txt based on matched patterns in file2.txt using awk. The blue color need to match with one of the data in field $2 in file2.txt. If match, BEGIN and FINISHED value in red will have a new value from field $3 and $4 accordingly.... (3 Replies)
Discussion started by: redse171
3 Replies

9. Shell Programming and Scripting

SED to replace exact match, not first occurrence.

Lets say I have file.txt: (Product:Price:QuantityAvailable) (: as delimiter) Chocolate:5:5 Banana:33:3 I am doing a edit/update function. I want to change the Quantity Available, so I tried using the SED command to replace 5, but my Price which is also 5 is changed instead. (for the Banana... (13 Replies)
Discussion started by: andylbh
13 Replies

10. Shell Programming and Scripting

Sed scripting, match text within line and replace

New to sed... Have a file foo.txt (below). Need to replace text on 2 lines, but can only feed sed the first few characters of each line (all lines are unique). So, in my example, I have put '$' in place of what I need to figure out how to feed the whole line. What I have thus far: sed -e... (6 Replies)
Discussion started by: boolean2222
6 Replies
Login or Register to Ask a Question