Operating System - HP-UX
1827875 Members
1399 Online
109969 Solutions
New Discussion

script substitute with regular expression

 
SOLVED
Go to solution
Franky Leeuwerck_1
Regular Advisor

script substitute with regular expression

Hi,

I need some help on the following script problem.

How can I change a fraction at the beginning of a line in this way :

'Dec 3 09' should be changed into 'Dec 3 09' .
'Dec 12 09' should stay that way.

The first 3 characters ( month) can be different, also the last two digits (day of the month).

Thanks in advance.
Franky
8 REPLIES 8
Franky Leeuwerck_1
Regular Advisor

Re: script substitute with regular expression

It is not clearly visible in the web page, but the string to be replaced contains two spaces between the month and the day of the of the month number.

Franky
Mark Grant
Honored Contributor

Re: script substitute with regular expression

if I understand you correctly, you want to remove an extra space which is between one of those three "words"

If so, then this will return the fields without the extra space

echo "Dec 02 09"| awk '{printf "%s %s %s", $1,$2,$3}'

Please clarify if you need something else.
Never preceed any demonstration with anything more predictive than "watch this"
Elmar P. Kolkman
Honored Contributor

Re: script substitute with regular expression

In that case I think you need the following:
sed 's| *| |g'

This will replace all multiple spaces to a single space.

Or, if the month is at the start of the line, it can be done in a better way:
sed 's|^\([A-Za-z]*\) |\1 |g'
(Mind the double space after the closing bracket!)
Every problem has at least one solution. Only some solutions are harder to find.
Franky Leeuwerck_1
Regular Advisor

Re: script substitute with regular expression

Hello Mark,

Thanks for your quick reply.

No, that's not want I need. Sorry, for the unclear explanation.

This is what I need. I will mark the space with 'SPACE' to make the example clear.

'DecSPACE3SPACE9' should be replaced with
'DecSPACESPACE3SPACE9'

'DecSPACE10SPACE9' should stay that way.

Franky

Franky Leeuwerck_1
Regular Advisor

Re: script substitute with regular expression

Hi Elmar,

See my reply to Mark.
Mark Grant
Honored Contributor
Solution

Re: script substitute with regular expression

Ahh, got you :)

In that case

echo "Dec 2 09" | awk '{printf "%s %2s %s", $1,$2,$3}'

Regards
Never preceed any demonstration with anything more predictive than "watch this"
Franky Leeuwerck_1
Regular Advisor

Re: script substitute with regular expression

Thanks Mark !
That's what I needed.

Regards,
Franky
john korterman
Honored Contributor

Re: script substitute with regular expression

Hi,
try this, using your infile as $1:


#!/usr/bin/sh
typeset -R2 day
while read month day number
do
echo "$month" "$day" "$number"
done <$1

regards,
John K.

it would be nice if you always got a second chance