1834395 Members
10430 Online
110066 Solutions
New Discussion

sed or awk challenge

 
Rene Krewinkel
Occasional Contributor

sed or awk challenge

Hi,

I have the following 'challenge':
- a 3rd party program reads the data in an ascii file until it finds a trailer record with the last word being "EOF".
- the program doesnot accept any characters or lines after this "EOF" marker. So the string EOF has to be followed by the actual EOF-code.
- The data for this file is spooled by the oracle utl_file package, so every written line in a text file is terminated with a line-feed/cariage return character.
- My question: does any one have a clue on how to replace the "EOF" string with the "EOF"string combined with the EOF character??
thanx in advance...


7 REPLIES 7
Robin Wakefield
Honored Contributor

Re: sed or awk challenge

Hi Rene,

So if your file, /tmp/abc, contains:

this is a line
so is this
and this
this is the end EOF
should not get here
or here

is this what you require?:

awk '/EOF/{print;exit}{print}' /tmp/abc
this is a line
so is this
and this
this is the end EOF

Rgds, Robin.
Bill McNAMARA_1
Honored Contributor

Re: sed or awk challenge

There could only be one person to do that!

you da man Robin!!

Bill
It works for me (tm)
Rene Krewinkel
Occasional Contributor

Re: sed or awk challenge

Sorry Robin, thanx for the reponse tough,
but if I transfer the file (binary or ascii) doesn't matter to a NT environment, there is still a new-line char after the EOF statement...
Robin Wakefield
Honored Contributor

Re: sed or awk challenge

Hi Rene,

sorry, try this:

awk '/EOF/{printf"%s",$0;exit}{print}' /tmp/abc

Rgds, Robin.
Marco Paganini
Respected Contributor

Re: sed or awk challenge

Hello,

I'd suggest just a small change on Robin's solution.

If you feed something like:

this is a line
so is this
and this
this is a false EOF folks...
this is the end EOF
should not get here

to the previous script, it will fail (EOF is there, but it is not the last word). A solution would be:

awk '/EOF[ \t]*$/{printf"%s",$0;exit}{print}'

Hope it helps,
Paga
Keeping alive, until I die.
Krishna Prasad
Trusted Contributor

Re: sed or awk challenge

If you are moving the file from Unix to NT and then accessing it in NT I believe you will always have a new line character becuase of the way each operating system saves files.

If you use vi to look at the file you see a ^M at the end of everyline. You can strip the control M on the Unix box if it came from NT.
However, I think you will have to have some kind of utility on the NT box that will give you an Unix shell to stripe the control M.

on the Unix side you can do something like

cat filename | sed 's/^M///g' > newfilename

the ^ is actually the control character not just a shift 6 you need to hit the control key.

Let me know if this is what you are asking?
Positive Results requires Positive Thinking
Rene Krewinkel
Occasional Contributor

Re: sed or awk challenge

Thanx guy's,

Marco's solution seems to do the trick after all. I MUST transfer the file binary though!

regards,

Rene