Operating System - HP-UX
1833788 Members
2686 Online
110063 Solutions
New Discussion

awk - searching/replacing control characters

 
Lemaitre
Occasional Advisor

awk - searching/replacing control characters

People,

I am trying to delete a load of control characters from a file that has acquired a load of new text via conversion problems. I.e. the pipe delimitted file looks like :

aaaa|bbbb|cccc^M
dddd|eeee|ffff^M
gggg|hhhh|iiii^M

what I am trying to do is convert it to

aaaa|bbbb|cccc|eor
dddd|eeee|ffff|eor
gggg|hhhh|iiii|eor

I have tried the following :

cat filename | awk -F"|" '
{printf("%s|%s|%s|eor",$1,$2,$3)}'

but this gives :

aaaa|bbbb|cccc^M|eor
dddd|eeee|ffff^M|eor
gggg|hhhh|iiii^M|eor

With my limited knowledge of awk, could someone suggest a script that would do this, or better still, identify how awk (if it can that is), searches for this type of character (^M indicates a newline I think)

thanks to you all!
John
9 REPLIES 9
RAC_1
Honored Contributor

Re: awk - searching/replacing control characters

do this
cat test | awk -F"|" {printf("%s|%s|eor\n",$1,$2,$3)}'

Hope this helps.
There is no substitute to HARDWORK
Chris Wilshaw
Honored Contributor

Re: awk - searching/replacing control characters

As an alternative, using sed you could try

cat | sed s/.$/\|eor/g

This will replace any character followed by the end of line marker ($) with |eor
Jean-Louis Phelix
Honored Contributor

Re: awk - searching/replacing control characters

hi,

You should perhaps try to delete these ^m before reformatting using either sed ot dos2ux.

dos2ux filename | awk '{printf "%s|eor\", $0)}

or the most simple :

sed 's/^\(.*\)^M/&|eor/' filename

use ^V^M to avoid ^M interpretation.

Regards.
It works for me (© Bill McNAMARA ...)
u856100
Frequent Advisor

Re: awk - searching/replacing control characters

Hi,

I missed the \n in my previous response!

I have been using :

cat filename | awk -F"|" '
{printf("%s|%s|%s|eor\n",$1,$2,$3)}'


The awk script you mentioned, I think would miss out a column I need (cccc - eeee and iiii)

thanks
John

P.s. I don't know why, but I have just logged in as myself, and got logged in as some person called Lemaitre!

whats going on?????
chicken or egg first?
Jean-Louis Phelix
Honored Contributor

Re: awk - searching/replacing control characters

Hi Chris,

Clever '.$' :^) , not to deal with ^M interpretation. I like it !

Jean-Louis.
It works for me (© Bill McNAMARA ...)
u856100
Frequent Advisor

Re: awk - searching/replacing control characters

people!

in addition to the fact that I have somehow logged in as another person (le-maitre).... I CANT assign points!!!

NOOOO!
chicken or egg first?
H.Merijn Brand (procura
Honored Contributor

Re: awk - searching/replacing control characters

# perl -pi -e's/[\r\n]+$/|eor\n/' filename
Enjoy, Have FUN! H.Merijn
John Meissner
Esteemed Contributor

Re: awk - searching/replacing control characters

did you ftp this file to your server? if you did ftp it... did you use bin or ascii mode? sometime i noted that using the wrong mode will add those characters to the end of each line. Try ftp'ing the file again and see if you get the same results.

If you didn't ftp the file then just follow the suggestions above.
All paths lead to destiny
V. Nyga
Honored Contributor

Re: awk - searching/replacing control characters

Hi,

only an additional info:
this ^M is the UX-interpretation of a DOS carrige return.
To eliminate this character you only need a dos2ux!
dos2ux file>file2
and the ^M will disappear!

Regards
Volkmar
*** Say 'Thanks' with Kudos ***