Visit Our UNIX and Linux User Community


script command to replace character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script command to replace character
# 1  
Old 07-16-2009
Question script command to replace character

Hi,

i have log like this :
Quote:
00:00 ,30011 ,376 ,375
00:00 ,30012 ,474 ,473
01:00 ,30011 ,263 ,262
01:00 ,30012 ,360 ,358
02:00 ,30011 ,416 ,414
02:00 ,30012 ,510 ,508
03:00 ,30011 ,418 ,418
03:00 ,30012 ,459 ,460
04:00 ,30011 ,399 ,397
04:00 ,30012 ,397 ,397
05:00 ,30011 ,498 ,498
05:00 ,30012 ,462 ,461
06:00 ,30011 ,482 ,480
06:00 ,30012 ,403 ,403
07:00 ,30011 ,520 ,515
07:00 ,30012 ,402 ,402
08:00 ,30011 ,539 ,538
08:00 ,30012 ,523 ,523
09:00 ,30011 ,543 ,544
09:00 ,30012 ,557 ,556
10:00 ,30011 ,578 ,576
10:00 ,30012 ,596 ,596
11:00 ,30011 ,651 ,648
11:00 ,30012 ,655 ,651
12:00 ,30011 ,598 ,594
12:00 ,30012 ,535 ,536
13:00 ,30011 ,472 ,472
13:00 ,30012 ,661 ,661
14:00 ,30011 ,457 ,450
14:00 ,30012 ,746 ,729
15:00 ,30011 ,220 ,220
15:00 ,30012 ,672 ,666
16:00 ,30011 ,181 ,181
16:00 ,30012 ,523 ,523
17:00 ,30011 ,174 ,174
17:00 ,30012 ,449 ,449
18:00 ,30011 ,159 ,159
18:00 ,30012 ,355 ,355
19:00 ,30011 ,155 ,154
19:00 ,30012 ,311 ,311
20:00 ,30011 ,150 ,150
20:00 ,30012 ,316 ,316
21:00 ,30011 ,183 ,182
21:00 ,30012 ,316 ,316
22:00 ,30011 ,192 ,193
22:00 ,30012 ,326 ,327
23:00 ,30011 ,190 ,190
23:00 ,30012 ,443 ,443
Actually i want to change the time stamp, and the rule is like this :

Quote:
00:00 --> 08:00
01:00 --> 09:00
02:00 --> 10:00
03:00 --> 11:00
04:00 --> 12:00
05:00 --> 13:00
06:00 --> 14:00
07:00 --> 15:00
08:00 --> 16:00
09:00 --> 17:00
10:00 --> 18:00
11:00 --> 19:00
12:00 --> 20:00
13:00 --> 21:00
14:00 --> 22:00
15:00 --> 23:00
16:00 --> 00:00
17:00 --> 01:00
18:00 --> 02:00
19:00 --> 03:00
20:00 --> 04:00
21:00 --> 05:00
22:00 --> 06:00
23:00 --> 07:00
and My script is like this :

Quote:
cat $@ | sed '
s/00:00/08:00/g;
s/01:00/09:00/g;
s/02:00/10:00/g;
s/03:00/11:00/g;
s/04:00/12:00/g;
s/05:00/13:00/g;
s/06:00/14:00/g;
s/07:00/15:00/g;
s/08:00/16:00/g;
s/09:00/17:00/g;
s/10:00/18:00/g;
s/11:00/19:00/g;
s/12:00/20:00/g;
s/13:00/21:00/g;
s/14:00/22:00/g;
s/15:00/23:00/g;
s/16:00/00:00/g;
s/17:00/01:00/g;
s/18:00/02:00/g;
s/19:00/03:00/g;
s/20:00/04:00/g;
s/21:00/05:00/g;
s/22:00/06:00/g;
s/23:00/07:00/g'
I know the script will loop and loop again after 07:00
like this:
Quote:
00:00 ,30011 ,376 ,375
00:00 ,30012 ,474 ,473
01:00 ,30011 ,263 ,262
01:00 ,30012 ,360 ,358
02:00 ,30011 ,416 ,414
02:00 ,30012 ,510 ,508
03:00 ,30011 ,418 ,418
03:00 ,30012 ,459 ,460
04:00 ,30011 ,399 ,397
04:00 ,30012 ,397 ,397
05:00 ,30011 ,498 ,498
05:00 ,30012 ,462 ,461
06:00 ,30011 ,482 ,480
06:00 ,30012 ,403 ,403
07:00 ,30011 ,520 ,515
07:00 ,30012 ,402 ,402
00:00 ,30011 ,539 ,538
00:00 ,30012 ,523 ,523
01:00 ,30011 ,543 ,544
01:00 ,30012 ,557 ,556
02:00 ,30011 ,578 ,576
02:00 ,30012 ,596 ,596
03:00 ,30011 ,651 ,648
03:00 ,30012 ,655 ,651
04:00 ,30011 ,598 ,594
04:00 ,30012 ,535 ,536
05:00 ,30011 ,472 ,472
05:00 ,30012 ,661 ,661
06:00 ,30011 ,457 ,450
06:00 ,30012 ,746 ,729
07:00 ,30011 ,220 ,220
07:00 ,30012 ,672 ,666
00:00 ,30011 ,181 ,181
00:00 ,30012 ,523 ,523
01:00 ,30011 ,174 ,174
01:00 ,30012 ,449 ,449
02:00 ,30011 ,159 ,159
02:00 ,30012 ,355 ,355
03:00 ,30011 ,155 ,154
03:00 ,30012 ,311 ,311
04:00 ,30011 ,150 ,150
04:00 ,30012 ,316 ,316
05:00 ,30011 ,183 ,182
05:00 ,30012 ,316 ,316
06:00 ,30011 ,192 ,193
06:00 ,30012 ,326 ,327
07:00 ,30011 ,190 ,190
07:00 ,30012 ,443 ,443
Can somebody help me ??

Thanks in advance..
# 2  
Old 07-16-2009
try awk
Code:
awk -F':' '{ z=$1+8; $1=sprintf("%02d", z % 24); print $0}' filename > newfile

# 3  
Old 07-16-2009
thanks jim i modify a bit put ":"

Quote:
awk -F':' '{ z=$1+8; $1=sprintf("%02d:", z % 24); print $1$2}'
# 4  
Old 07-17-2009
help ton convert a file of number.

help ton convert a file of number.

Hello ,
I have this problem. Can somebody can help me ?

my input file is like this:

1225
1254
4567
7895
4565

I want to have in output something like this:

'1225',
'1254',
'4567',
'7895',
'4565'

Thanks for your help.
# 5  
Old 07-17-2009
Code:
sed -e "s/^/\'/" -e  "s/$/\',/"  file

Quote:
Originally Posted by yeclota
help ton convert a file of number.

Hello ,
I have this problem. Can somebody can help me ?

my input file is like this:

1225
1254
4567
7895
4565

I want to have in output something like this:

'1225',
'1254',
'4567',
'7895',
'4565'

Thanks for your help.
# 6  
Old 07-17-2009
You can try:
Code:
sed "s/\(.*\)/\'&\',/" file


Previous Thread | Next Thread
Test Your Knowledge in Computers #350
Difficulty: Medium
AWK was originally written in 1975 and distributed with Version 6 Unix.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to replace nth character with comma

I have a requirement as below. In one of my column, I have data which may or may not be separted with coma always. Now I need to validate the length of these text within the coma (if available) and if the length is more than 30 characters, I need to insert a coma either at 30 characters if its... (3 Replies)
Discussion started by: aramacha
3 Replies

2. Shell Programming and Scripting

Bash script to replace a character with another

Hi. I'm a complete noob when it comes to scripting. I have approximately 2000 files scattered throughout different locations that I need to rename. The current files have a character, "." , that needs to be replaced with an underscore. I have no clue which route to go about correcting this.... (4 Replies)
Discussion started by: Nvizn
4 Replies

3. Shell Programming and Scripting

sed command to replace a character at last

Hi All, I have a file having one line only. It is like trapsess:inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|78|0037| I want to replace the numbers in last two columns by As. It should look like trapsess:inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|AA|AAAA| Please, suggest me any shell... (12 Replies)
Discussion started by: mukeshbaranwal
12 Replies

4. Shell Programming and Scripting

Replace '$' with no character using tr command

Hi All, Need your help urgently. since '$' comes at the end of the line i want to remove '$' using tr command: file: 111111|.11|.00$ 222222|2.22|.00$ 333333|.33|.00$ 444444|.44|.00$ Thanks in Advance (7 Replies)
Discussion started by: HemaV
7 Replies

5. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 Replies

6. Shell Programming and Scripting

Please help replace character command

hi i have log : i want remove some char become like this: anybody can help me ? (7 Replies)
Discussion started by: justbow
7 Replies

7. Shell Programming and Scripting

script to replace a character with '(Apostrophe)

Hi Friends, I need a sed or awk based script to replace a character(alphabet) with ' (Apostrophe). I tried to use the following two commands 1) sed -e 's/a/'/' filename 2) awk '{sub("a","'",$1);print}' filename but both got failed. Any help on this. Thanks in advance.. (3 Replies)
Discussion started by: ks_reddy
3 Replies

8. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

9. UNIX for Dummies Questions & Answers

How to Replace,Sort,and Append Character one script

Hi all i am very new to shell scripting,hope u guys can help i need to replace,sort and append character for the file that look like this: 1007032811010001000100000001X700026930409 1007032811010001000200000002X700026930409 1007032711020001000300000003X700026930409... (2 Replies)
Discussion started by: ashikin_8119
2 Replies

10. UNIX for Dummies Questions & Answers

Command to replace character

I've been googling for the following for the past few weeks several times, but haven't yet come across something that I could easily grasp. Can someone point me in the right direction please? I'm trying to replace a character in file names, i.e. the character is a period, and I want to replace... (5 Replies)
Discussion started by: HLee1981
5 Replies

Featured Tech Videos