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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk/grep copy and paste and insert in between lines.
# 1  
Old 07-17-2009
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:

Code:
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:

Code:
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.

Last edited by vgersh99; 07-17-2009 at 09:01 PM.. Reason: code tags, PLEASE!
# 2  
Old 07-17-2009
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 [code] and [/code] 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
# 3  
Old 07-18-2009
use something like this
Code:
awk -F":" 'NR==1{var=$0}/^[0-9][0-9]*/{A[$1]=A[$1]"\n"$0}END{for ( i in A){print A[i]}}' filename

# 4  
Old 07-20-2009
please try below perl script
Code:
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

# 5  
Old 07-20-2009
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:
Code:
0 0 * * * /some/script >> /var/log/somelog 2>/dev/null

and script is:
Code:
#!/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)
# 6  
Old 07-20-2009
Hi,

Thanks for all the replies!

@vidyadhar85
I used your command and it produced this output:
Code:
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:

Code:
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!
# 7  
Old 07-21-2009
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 ?
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use sed/awk to do like copy and paste

I have rrd file which is have the gaps and I want to fill it out with some value , I've got 10 NaN record and I try to populate data from 10 records be for NaN to change instead of NaN :( <!-- 2016-05-19 14:10:00 CST / 1463638200 -->... (11 Replies)
Discussion started by: boobytrap
11 Replies

2. UNIX for Dummies Questions & Answers

Copy Lines between Keywords & paste them to another file

hi, I have Multiple files with the following data : File1 100414 DR1 END XXXXX Test1 Test2 Test3 Test4 Test5 Test6 END 100514 DR2 END XXXXX Test7 Test8 Test9 Test10 Test11 Test12 END 100614 DR3 (5 Replies)
Discussion started by: newageBATMAN
5 Replies

3. Shell Programming and Scripting

awk to insert duplicated lines

Dear All, Suppose I have a file: 1 1 1 1 2 2 2 2 3 3 3 3I want to insert new line under each old line so that the file would become: 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3How can this be accomplished using awk (or sed)? (5 Replies)
Discussion started by: littlewenwen
5 Replies

4. Shell Programming and Scripting

AWK/GREP: grep only lines starting with integer

I have an input file 12.4 1.72849432773174e+01 -7.74784188610632e+01 12.5 9.59432114416327e-01 -7.87018212757537e+01 15.6 5.20139995965960e-01 -5.61612429666624e+01 29.3 3.76696387248366e+00 -7.42896194101892e+01 32.1 1.86899877018077e+01 -7.56508762501408e+01 35 6.98857157014640e+00... (2 Replies)
Discussion started by: chrisjorg
2 Replies

5. 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

6. Shell Programming and Scripting

sed/awk to insert multiple lines before pattern

I'm attempting to insert multiple lines before a line matching a given search pattern. These lines are generated in a separate function and can either be piped in as stdout or read from a temporary file. I've been able to insert the lines from a file after the pattern using: sed -i '/pattern/... (2 Replies)
Discussion started by: zksailor534
2 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. UNIX for Dummies Questions & Answers

copy and paste certain many lines of huge file in linux

Dear All, I am working with windoes OS but remote a linux machine. I wonder the way to copy an paste some part of a huge file in linux machine. the contain of file like as follow: ... dump annealling all custom 10 anneal_*.dat id type x y z q timestep 0.02 run 200000 Memory... (2 Replies)
Discussion started by: ariesto
2 Replies

9. Shell Programming and Scripting

sed/awk script selective insert between lines

Hi I have a file in the foll. format *RECORD* *FIELD NO* ....... ....... *FIELD TX* Data *FIELD AV* Data *FIELD RF* *RECORD* *FIELD NO* ....... ....... *FIELD TX* Data *FIELD RF* (4 Replies)
Discussion started by: dunstonrocks
4 Replies

10. Shell Programming and Scripting

Cut Paste and Insert Help

Hello I have a very large file where say each line is made up of 80 characters. I want to cut the characters from 20-30 and 50-60 from each line and then insert a delimiter between them (# or | etc). eg input file 000000000131.12.20990000590425246363375670011200140406... (5 Replies)
Discussion started by: PradeepRed
5 Replies
Login or Register to Ask a Question