Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix script using sed or maybe awk? 1

Status
Not open for further replies.
Oct 6, 2000
13
0
0
US
I have following data in a file. I want to insert the date before each line after it. So that it look like:
Dec 11 00:00:00^0^0^208037^670^0^0^0^8^22^0^188^623^573^13^9^76^2
How can I do this? I was trying to use sed but haven't found a way that works yet. Please help.

Dec 11 00:00:00
^0^0^208037^670^0^0^0^8^22^0^188^623^573^13^9^76^2
^0^1^207940^234^0^0^0^172^846^0^431^7681^465^11^8^79^2
^0^1^207940^217^0^0^0^0^0^0^352^808^153^1^2^78^19
^0^1^207940^700^0^0^0^96^298^0^329^9157^314^8^6^86^1
^0^1^207940^700^0^0^0^0^0^0^273^1346^91^1^1^98^0
^0^1^207940^698^0^0^0^0^0^0^362^4667^370^7^2^91^0
^0^1^207940^698^0^0^0^0^0^0^274^935^107^1^1^99^0
^0^1^207940^698^0^0^0^0^0^0^330^3822^284^6^1^93^0
^0^1^207940^696^0^0^0^0^0^0^275^2277^106^2^1^96^0
^0^1^207940^696^0^0^0^0^0^0^323^3442^241^5^1^94^0
^0^1^207940^694^0^0^0^0^0^0^280^1254^168^3^1^97^0
^0^1^207945^689^0^0^0^0^0^0^292^1259^145^1^1^98^0
^0^1^207940^694^0^0^0^0^0^0^314^3998^281^6^2^92^0
Dec 11 00:30:00
^0^0^208281^336^0^0^0^8^22^0^188^624^573^13^9^76^2
^0^1^208646^144^0^0^0^310^1206^0^331^5888^356^8^6^82^3
^0^1^208646^144^0^0^0^0^0^0^302^1474^165^2^1^98^0
^0^1^208676^125^0^0^0^1475^5001^0^718^4666^946^9^12^53^26
^0^2^208676^123^0^0^0^3213^10401^0^993^1280^1100^3^19^39^39
 
I'm sure there are better ways to do this, but this works given the data you've posted:

awk 'BEGIN { tm=""; } {
if (NF==3) {
printf("%s %s\n",$1,$2);
tm=$NF;
} else {
if (tm!="") {
printf("%s%s\n",tm,$0);
tm="";
}
}
}' name_of_data_file

Russ
bobbitts@hotmail.com
 
That code almost works. But it has two problems. 1. The date portion is on a differnt line. 2. It only does inserts for one line. I need to insert into every line below until it hits another line with a date on it. Unfortunatley I don't understand awk very well and don't understand your code.

Thanks

Miguel
 
Ok, I misunderstood your problem. I /think/ I understand what you want now (although I don't understand what you mean by "the date portion is on a different line" as being a problem, as the script accounts for that). Try this modified code. Unfortunately, I can't test it right now, but hopefully this will do the trick:

awk '{
if (NF==3) {
# This is a line with the date, only print the
# month and the month day.
printf("%s %s\n",$1,$2);
# Save the time to insert on the lines below
tm=$NF;
} else {
# This is an ordinary line, prepend the time
printf("%s%s\n",tm,$0);
}
}' name_of_data_file

Russ
bobbitts@hotmail.com
 
Thank you very much. I had to modify slightly but it worked great!

Here is what I did.
awk '{

if (NF==3) {

# This is the line with the date, sends it to /dev/null


printf("%s %s ", $1, $2) >/dev/null;

# Save the date and time to insert on the lines below

tm=$0;

} else {

# This is an ordinary line, prepend the time

printf("%s%s\n",tm,$0);

}

}' frank.txt >www

Once again, thank you very much.

Miguel
 
AH...now, I understand...you don't want to have the date line retained at all, so instead of your /dev/null thing:

awk '{
if (NF==3) {
# Save the time to insert on the lines below
tm=$NF;
} else {
# This is an ordinary line, prepend the time
printf("%s%s\n",tm,$0);
}
}' name_of_data_file



Russ
bobbitts@hotmail.com
 
hi
I have a big problem,maybe it's really easy for you .I have a data like :

37
94
176
209
262
320
357
375
421

there is two blanks before each line .I want to put
kill -9 before each line but i don't know how to do?
Could you please help me?

Regards
 
Try a sed command like "sed -e 's/^\ */kill\ -9\ /g' ". This should take any line begining with 1 or more spaces and replace it with "kill -9 ".
 
It works well thank you very much.
 
Why you want to put [tt]kill -9[/tt] before aech line? Why don't you simply exec
[tt]
kill -9 `cat /file/with/pids` and such?
 
I have one file having two columns with entries as different dates. Now I want to take system date and match this in the first column, and then take the corresponding entry in the second coulmn.Now, I want to append this string (which I got from the second colum) in different file , whereever it finds any date entries.

Thank you in advance,
 
ok....

could you post a few lines from your input file and then a few lines from what you want in your output file? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top