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     %user     %nice   %system   %iowait    %steal     %idle
23:35:01        all      1.33      0.00      0.08      0.00      0.00     98.59
23:45:01        all      1.36      0.00      0.08      0.00      0.00     98.56
23:55:01        all      1.33      0.00      0.08      0.00      0.00     98.58
23:59:01        all      1.59      0.00      0.09      0.00      0.00     98.32
00:00:01        all      1.50      0.00      0.12      0.00      0.00     98.32
00:05:01        all      1.49      0.00      0.12      0.14      0.00     98.25
00:15:01        all      1.33      0.00      0.07      0.00      0.00     98.60
00:25:01        all      1.44      0.00      0.07      0.00      0.00     98.49
00:35:01        all      1.26      0.00      0.07      0.00      0.00     98.66
00:45:01        all      1.37      0.00      0.09      0.00      0.00     98.53

Desired output:

Linux 2.6.24-24-generic (abc)       07/15/09
23:25:01        CPU     %user     %nice   %system   %iowait    %steal     %idle
23:35:01        all      1.33      0.00      0.08      0.00      0.00     98.59
23:45:01        all      1.36      0.00      0.08      0.00      0.00     98.56
23:55:01        all      1.33      0.00      0.08      0.00      0.00     98.58
23:59:01        all      1.59      0.00      0.09      0.00      0.00     98.32
Linux 2.6.24-24-generic (abc)       07/16/09
00:05:01        all      1.49      0.00      0.12      0.14      0.00     98.25
00:15:01        all      1.33      0.00      0.07      0.00      0.00     98.60
00:25:01        all      1.44      0.00      0.07      0.00      0.00     98.49
00:35:01        all      1.26      0.00      0.07      0.00      0.00     98.66
00:45:01        all      1.37      0.00      0.09      0.00      0.00     98.53

As you can see the first line of the txt file was copied and inserted between 23:59:01 and 00:05:01 with the date changed to 07/16/09. I'm thinking something along the lines of adding one using awk could solve this problem? I'm not sure, but basically i can use any utility including but not limited to awk, sed, perl to do this.

Thanks so much unix community,
the1hand3r.

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

use something like this

awk -F":" 'NR==1{var=$0}/^[0-9][0-9]*/{A[$1]=A[$1]"\n"$0}END{for ( i in A){print A}}' filename

please try below perl script

my (@tmp,$head,$key);
while(<DATA>){
	if(! /^[0-9]/){
		$head=$_;
		print;
		next;
	}
	else{
		@tmp=split(" ",$_,2);
		$tmp[0]=~s/://g;
	}
	if($tmp[0] < $key){
		$head=~s:([0-9]{2})/([0-9]{2})/([0-9]{2}):$1."/".($2+1)."/".$3:e;
		print $head;
	}
	$key=$tmp[0];
	print;
}

__DATA__
Linux 2.6.24-24-generic (abc)       07/15/09
23:25:01        CPU     %user     %nice   %system   %iowait    %steal     %idle
23:35:01        all      1.33      0.00      0.08      0.00      0.00     98.59
23:45:01        all      1.36      0.00      0.08      0.00      0.00     98.56
23:55:01        all      1.33      0.00      0.08      0.00      0.00     98.58
23:59:01        all      1.59      0.00      0.09      0.00      0.00     98.32
00:00:01        all      1.50      0.00      0.12      0.00      0.00     98.32
00:05:01        all      1.49      0.00      0.12      0.14      0.00     98.25
00:15:01        all      1.33      0.00      0.07      0.00      0.00     98.60
00:25:01        all      1.44      0.00      0.07      0.00      0.00     98.49
00:35:01        all      1.26      0.00      0.07      0.00      0.00     98.66
00:45:01        all      1.37      0.00      0.09      0.00      0.00     98.53
00:15:01        all      1.37      0.00      0.09      0.00      0.00     98.53

Or if you are more interesting how it works in future, why not make cron rule to add line to the logfile about 00:00:00

using:
crontab -e
you can edit your crontab file:

0 0 * * * /some/script >> /var/log/somelog 2>/dev/null

and script is:

#!/bin/bash   
#or #!/bin/ksh
# this is little quick&dirty but give example howto ...
time=$( date '+%m/%d/%Y' )
echo "Linux 2.6.24-24-generic (abc)  $time"

or use ex. datecalc to calculate next day value. (use forum search to find datecalc)

Hi,

Thanks for all the replies!

@vidyadhar85
I used your command and it produced this output:

00:00:01        all      1.50      0.00      0.12      0.00      0.00     98.32
00:05:01        all      1.49      0.00      0.12      0.14      0.00     98.25
00:15:01        all      1.33      0.00      0.07      0.00      0.00     98.60
00:25:01        all      1.44      0.00      0.07      0.00      0.00     98.49
00:35:01        all      1.26      0.00      0.07      0.00      0.00     98.66
00:45:01        all      1.37      0.00      0.09      0.00      0.00     98.53
00:15:01        all      1.37      0.00      0.09      0.00      0.00     98.53

23:25:01        CPU     %user     %nice   %system   %iowait    %steal     %idle
23:35:01        all      1.33      0.00      0.08      0.00      0.00     98.59
23:45:01        all      1.36      0.00      0.08      0.00      0.00     98.56
23:55:01        all      1.33      0.00      0.08      0.00      0.00     98.58
23:59:01        all      1.59      0.00      0.09      0.00      0.00     98.32

It doesn't show the date =(

@summer cherry
I was not able to get the script to run in my terminal. I think I might be doing something wrong, b/c when I copy and pasted the code into the terminal, it gave me a message saying:

bash: split1: line 3: syntax error near unexpected token `@tmp,$head,$key'
bash: split1: line 3: `my (@tmp,$head,$key);'

@kshji
I'm sorry, but I'm like I said I'm a newb at linux, but your idea seems very plausible. Can you elaborate on how I would go about changing the crontab file more in depth, and I'm not sure what I should type into /some/script. the file name is called tester and it is located in my home directory. Thanks.

Thanks everyone, but I'm still unable to complete this task. Any sugesstions/help would be appreciated!

echo $HOME
take that string and add your script name. That is full path for your script.
ex. /u2/home/userxxx/myscript.sh
Did you try to edit crontab file using command ?

crontab -e

0 0 * * *
=00:00 everyday do something

If you have not priledge to edit logfile, then give this crontabline for user who has.