Use sed/awk to do like copy and paste


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use sed/awk to do like copy and paste
# 8  
Old 05-19-2016
Luckily your file uses YYYY-MM-SS HH:MM:SS timestamps, which sort alphabetically. Those can be compared with < > <= etc without worrying about any date math. MIN="..." and MAX="..." are where those values are input into awk.

Code:
$ awk -f xml.awk -e 'BEGIN { P[0]=0; P[1]=0; ORS="" }
                # Extract YYYY-MM-DD HH:MM:SS time from special tag
                SPEC && match($0, /[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9:]*/) {
                        TIME=rall($0);
                }
                /^row>/ { D=0 }
                /^v>/ {
                        if($2 != "NaN") { P[D]=$2; }
                        else if((TIME >= MIN) && (TIME <= MAX) && P[D])
                        {       $2=P[D] }
                        D++;
                }

                { print "<" $0; }' MIN="2016-05-19 15:15:00" MAX="2016-05-19 15:55:00" datafile

<!-- 2016-05-19 14:10:00 CST / 1463638200 --> <row><v>8.2147341249e+06</v><v>1.1121553155e+07</v></row>
                        <!-- 2016-05-19 14:15:00 CST / 1463638500 --> <row><v>8.2746054049e+06</v><v>1.1065925833e+07</v></row>
                        <!-- 2016-05-19 14:20:00 CST / 1463638800 --> <row><v>8.5873130347e+06</v><v>1.1330819417e+07</v></row>
                        <!-- 2016-05-19 14:25:00 CST / 1463639100 --> <row><v>8.7400510244e+06</v><v>1.1466312671e+07</v></row>
                        <!-- 2016-05-19 14:30:00 CST / 1463639400 --> <row><v>8.8934198478e+06</v><v>1.1612750075e+07</v></row>
                        <!-- 2016-05-19 14:35:00 CST / 1463639700 --> <row><v>NaN</v><v>1.1451382750e+07</v></row>
                        <!-- 2016-05-19 14:40:00 CST / 1463640000 --> <row><v>8.6519721561e+06</v><v>1.1490998652e+07</v></row>
                        <!-- 2016-05-19 14:45:00 CST / 1463640300 --> <row><v>8.7016410558e+06</v><v>1.1758066110e+07</v></row>
                        <!-- 2016-05-19 14:50:00 CST / 1463640600 --> <row><v>8.7026601272e+06</v><v>1.1808612970e+07</v></row>
                        <!-- 2016-05-19 14:55:00 CST / 1463640900 --> <row><v>8.8925026101e+06</v><v>1.2043713450e+07</v></row>
                        <!-- 2016-05-19 15:00:00 CST / 1463641200 --> <row><v>8.7455789370e+06</v><v>1.2420369632e+07</v></row>
                        <!-- 2016-05-19 15:05:00 CST / 1463641500 --> <row><v>8.6264080619e+06</v><v>1.2602318595e+07</v></row>
                        <!-- 2016-05-19 15:10:00 CST / 1463641800 --> <row><v>8.8378430034e+06</v><v>1.3041342755e+07</v></row>
                        <!-- 2016-05-19 15:15:00 CST / 1463642100 --> <row><v>8.8378430034e+06</v><v>1.3041342755e+07</v></row>
                        <!-- 2016-05-19 15:20:00 CST / 1463642400 --> <row><v>8.8378430034e+06</v><v>1.3041342755e+07</v></row>
                        <!-- 2016-05-19 15:25:00 CST / 1463642700 --> <row><v>8.8378430034e+06</v><v>1.3041342755e+07</v></row>
                        <!-- 2016-05-19 15:30:00 CST / 1463643000 --> <row><v>8.8378430034e+06</v><v>1.3041342755e+07</v></row>
                        <!-- 2016-05-19 15:35:00 CST / 1463643300 --> <row><v>8.8378430034e+06</v><v>1.3041342755e+07</v></row>
                        <!-- 2016-05-19 15:40:00 CST / 1463643600 --> <row><v>8.8378430034e+06</v><v>1.3041342755e+07</v></row>
                        <!-- 2016-05-19 15:45:00 CST / 1463643900 --> <row><v>8.8378430034e+06</v><v>1.3041342755e+07</v></row>
                        <!-- 2016-05-19 15:50:00 CST / 1463644200 --> <row><v>8.8378430034e+06</v><v>1.3041342755e+07</v></row>
                        <!-- 2016-05-19 15:55:00 CST / 1463644500 --> <row><v>8.8378430034e+06</v><v>1.3041342755e+07</v></row>
                        <!-- 2016-05-19 16:00:00 CST / 1463644800 --> <row><v>NaN</v><v>NaN</v></row>
                        <!-- 2016-05-19 16:05:00 CST / 1463645100 --> <row><v>9.4998580882e+06</v><v>1.3563528239e+07</v></row>
                        <!-- 2016-05-19 16:10:00 CST / 1463645400 --> <row><v>9.3834388158e+06</v><v>1.3154323934e+07</v></row>

$

I added an NaN to an earlier line to test if it would be fixed or not.

Note the last line of NaN's not corrected from specifying 15:55 instead of 16:00
# 9  
Old 05-19-2016
Code:
awk '
NR==FNR {
   if ($7 !~ /NaN/) {f7[NR]=$7; for (i=NR-gc7; i<NR; i++) f7[i]=f7[i-gc7] ? f7[i-gc7] : l7 ; l7=$7; gc7=0} else {gc7++}
   if ($11 !~ /NaN/) {f11[NR]=$11; for (i=NR-gc11; i<NR; i++) f11[i]=f11[i-gc11] ? f11[i-gc11] : l11 ; l11=$11; gc11=0} else {gc11++}
   next
}
{
   if (($7+0) != $7) {sub("NaN", f7[FNR])}
   if (($11+0) != $11) {sub("NaN", f11[FNR])}
}
1
' FS="[<>]" infile infile

This User Gave Thanks to rdrtx1 For This Post:
# 10  
Old 05-20-2016
@rdrtx1 can fill the gapbut when try to restore xml into rrd it's not able to plot the graph.

@Corona688 I'm thinking to duplicate data from yesterday with same time frame to fill the Nan value. so the graph will become to strait line , actually try do some tricky way


Code:
 $2=P[D] * rand()

but value is too different than SD ,will try to work around on your code. xml.awk let me iterate through xml a lot easier
# 11  
Old 05-20-2016
Could you completely explain what you want? Show input and output. It will be much easier to make what you actually want than modify something you didn't want into something you do, one tiny but drastic rewrite at a time.
# 12  
Old 05-26-2016
sorry for mess the requested just my random outburst idea btw the script in #8 #9 are really fixing the gaps in rrd xml dump file and definitely solved my problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using SED to copy/paste with slashes and tabs.

I have: 2012/01_January/Kite/foldername/otherfoldername/placeholderBlue I want to end up with: /foldername/otherfoldername/2012/01_January/Kite/Blue Basically take everything before the first tab and put it in the place of the consistently named word placeholder and add a slash in place... (2 Replies)
Discussion started by: crowman
2 Replies

2. Shell Programming and Scripting

sed help - search/copy from one file and search/paste to another

I am a newbie and would like some help with the following - Trying to search fileA for a string similar to - AS11000022010 30.4 31.7 43.7 53.8 60.5 71.1 75.2 74.7 66.9 56.6 42.7 32.5 53.3 I then want to replace that string with a string from fileB - ... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

3. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

4. Shell Programming and Scripting

Copy and Paste to a new document

Hello, I am quite new to shell scripting so don't know all the tools available. What I'm trying to do is open a file optimal.txt search for objectiveValue and copy the number in quotes next to it. e.g. ... solutionName="incumbent" solutionIndex="-1" objectiveValue="13246" ... (6 Replies)
Discussion started by: StephanR
6 Replies

5. UNIX for Dummies Questions & Answers

vi copy/paste problem

I'm having a problem copy/pasting from a txt file in windows to vi. What happens is I copy a chunk of text, go to the putty terminal, go into insert mode, and right click, and it will stop pasting at a random point and freeze up. Nothing I do gets out of it. This only happens on my account... (1 Reply)
Discussion started by: solidarity
1 Replies

6. Shell Programming and Scripting

sed copy paste

Hello, I have this path and file: /dir/dir/dir/dir/dir/dir/dir/dir/dir/THIS_SPOT/fle.txt I want to end up with: /dir/dir/dir/dir/dir/dir/dir/dir/dir/THIS_SPOT/fle.txtTHIS_SPOT Take the dir after the 10th slash, add a tab at the end and paste the dir it copied. Thanks (4 Replies)
Discussion started by: crowman
4 Replies

7. Shell Programming and Scripting

copy/paste with awk

Hi everybody, I have two XML files. I am working on a script that could copy and paste the contents of the first xml file to the desired location in the second xml file. Here is my first XML file. This is the second XML file. Finaly, I wnat to obtain something like that : ... (2 Replies)
Discussion started by: lsaas
2 Replies

8. Shell Programming and Scripting

awk/grep copy and paste and insert in between lines.

Hi all, I'm a unix newb andI'm trying to write a script that can copy some text paste it in a certian place and then add a number. It's not really clear but I'll show an example. what the file looks like right now: Linux 2.6.24-24-generic (abc) 07/15/09 23:25:01 CPU ... (6 Replies)
Discussion started by: the1hand3r
6 Replies

9. Shell Programming and Scripting

how to replace paste utility with sed/awk

hi I have two file that I would like to paste line by line. I use to use paste for this. But my current platform will not have paste utility. Could anyone please suggest how I it can be done using sed,awk or even simple bourne shell scripts? Thanks Sabina (1 Reply)
Discussion started by: ssayeed
1 Replies

10. UNIX for Dummies Questions & Answers

awk or sed or paste

i have a file and i need the text to line up currently the file looks like so job scheduled complete 12 12:00 wendsday 13 1:00 wednsday its a text file but not sure how to manipulate the file for it to line up (3 Replies)
Discussion started by: leprichaun
3 Replies
Login or Register to Ask a Question