Replacing junk characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing junk characters
# 1  
Old 07-12-2010
Replacing junk characters

Hi,

I have a file with data as given below
Code:
$cat file1
123|abc|345
345|def|567

The first record is good record. The second record has an invisible junk character like \032.

I was replace all the occurences of that invisible character with #.
I want to do this for a set of invisible characters in a particular field of a file.
How can i do this.
# 2  
Old 07-12-2010
First confirm the character's octal code with this "sed" enquiry.

Code:
sed -n l file1


If the character turned out to be carriage-return (octal 015).

Code:
ROGUE=`echo "\0015"`
ukh44004 # sed -e "s/${ROGUE}/#/g" file1 > file2

# 3  
Old 07-12-2010
This translates all control characters except '\n' to #.
Code:
cat file1 | tr '[\000-\011\013-\037]' '#'

This deletes them.
Code:
cat file1 | tr -d '[\000-\011\013-\037]'

# 4  
Old 07-12-2010
@ Jackson

Thanks for that!!

Is it possible for me to extend it to do it for a pipe delimited file.
For Ex: the file is as given below
Code:
$cat file 1
123|USA|abc|454
567|Canada|def|456

In field 2 if there is either '\032 or '\015' then i have to delete them.
In field 3 if there is either '\032 or '\015' then i have to replace with #.

There is a file with 100 columns where some columns need to check for specific characters and they have to be either deleted or removed.Can this be done using awk if i know the fields.

---------- Post updated at 06:27 AM ---------- Previous update was at 06:15 AM ----------

Jackson,
There is some problem with that command. I tested on this data.
Code:
$ cat -v -e -t junk1.txt
123|john|ziet^Z|234$
345|goodyahoo.com|boy|765$
345|abcgood@yahoo.com|765$
345|#$%abc@yahoo.com|987$
dskajfk^Zjdfaks$
jhdfasjfh$

Executed the command
Code:
 cat junk1.txt | tr '[\000-\011\013-\037]' '#' > test1

The output in test1 is
Code:
$ cat -v -e -t test1
123|john|ziet^Z|234$
345|goodyahoo.com|boy|765$
345|abcgood@yahoo.com|765$
345|#$%abc@yahoo.com|987$
dskajfk^Zjdfaks$
jhdfasjfh$

The ^Z characters are still present

The octal output is given below
Code:
$ od -c junk1.txt
0000000   1   2   3   |   j   o   h   n   |   z   i   e   t 032   |   2
0000020   3   4  \n   3   4   5   |   g   o   o   d   y   a   h   o   o
0000040   .   c   o   m   |   b   o   y   |   7   6   5  \n   3   4   5
0000060   |   a   b   c   g   o   o   d   @   y   a   h   o   o   .   c
0000100   o   m   |   7   6   5  \n   3   4   5   |   #   $   %   a   b
0000120   c   @   y   a   h   o   o   .   c   o   m   |   9   8   7  \n
0000140   d   s   k   a   j   f   k 032   j   d   f   a   k   s  \n   j
0000160   h   d   f   a   s   j   f   h  \n
0000171

# 5  
Old 07-12-2010
This is most curious.

I reproduced your junk1.txt file so that it produces the same output with od. But tr works for me as expected.

It's very easy to get files swapped in little tests like this. Are you sure you are redirecting and dumping the correct files?
# 6  
Old 07-12-2010
The problem with the "tr" approach is that there must be a matching entry on both sides of the translate.

This should work for the two characters concerned.

Code:
cat junk1.txt | tr '\032\015' '##' > test1


With my unix "tr", it would I think need 31 '#" characters to match the ranges in KenJackson's post (untested). Hope this helps.
# 7  
Old 07-12-2010
Quote:
Originally Posted by ashwin3086
Is it possible for me to extend it to do it for a pipe delimited file.
For Ex: the file is as given below
Code:
$cat file 1
123|USA|abc|454
567|Canada|def|456

In field 2 if there is either '\032 or '\015' then i have to delete them.
In field 3 if there is either '\032 or '\015' then i have to replace with #.

There is a file with 100 columns where some columns need to check for specific characters and they have to be either deleted or removed.Can this be done using awk if i know the fields.
Code:
awk 'BEGIN{FS=OFS="|"} {gsub(/[^M^Z]/,"",$2); gsub(/[^M^Z]/,"#",$3); print}' junk > nojunk

Note: In my code, "^M^Z" was entered by typing control-v control-m control-v control-z.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to remove Junk characters

Hi All, I have a issue that we are getting Junk characters from source and i am not able to load that records to Database. Line breakers Junk Characters (Â and different every time) Japanese Characters Every time I am using grep command and awk -F "\007" to find them and delete that... (1 Reply)
Discussion started by: spradeep86
1 Replies

2. Solaris

Junk characters in Solaris 11

Hi, I rebooted a Solaris 11 box and after that date stamp is coming in junk in almost all directories. root@tstilp05 # ls -l total 112 drwxrwxr-x 9 root sys 19 juin 1 03:10 adm drwxr-xr-x 6 root sys 6 sept. 19 2012 ai drwxr-xr-x 3 root bin ... (3 Replies)
Discussion started by: solaris_1977
3 Replies

3. UNIX for Dummies Questions & Answers

How to remove JUNK characters (FROM�)

Hi I have to remove the junk characters from my file. Please help.. File content : CURITY_CODE_GSD) FROM� DL_CB_SOD_EOD_VALUATION WHERE� ASOF (1 Reply)
Discussion started by: arukuku
1 Replies

4. Shell Programming and Scripting

Junk characters in mailx output

I have script which send a mail with top output. The script look like $ cat health.sh #!/bin/sh maillist="email address" rm /home/rtq1/file top -n 1 | head 15 > file cat file | mailx -s "Daily Health Report from `hostname` ..." "${maillist}" But now i am getting some junk characters along... (1 Reply)
Discussion started by: Renjesh
1 Replies

5. Shell Programming and Scripting

Handling Junk Characters

Urgently ur help is needed. Actually my req is i have an input file, that input file may have junk characters (^M, ^Z) etc... eg: cat file name abc^Z addres name2 msdmskd^Z address2 I want to validate the record and display where exactly this junk character resides. I want to... (3 Replies)
Discussion started by: help_scr_seeker
3 Replies

6. UNIX for Dummies Questions & Answers

how to grep junk characters in a file

hi guys, I am generating a file from datastage (an etl tool). Now the file is having some junk characters like ( Á,L´±,ñ and so on).. I want to use the grep function to figure out all the junk characters and their location. Can somebody help me out in finding it out.. if possible i... (1 Reply)
Discussion started by: mac4rfree
1 Replies

7. UNIX for Advanced & Expert Users

junk characters in the begining of every line

Hi Experts, here is a background to my problem : I am exporting data from teradata using fastexport utility, as varchar data. This pads additional two bytes (2 places as seen in notepad) in the resultset. I have found out other means of avoiding it but can't use varchar option in that... (5 Replies)
Discussion started by: sumoka
5 Replies

8. Shell Programming and Scripting

finding junk characters

Hi, Is there anyway to find the junk characters in a file.Consider the file has data as given below: 123|abc^M|Doctor^C #record 1 234|def|Med #record 2 345|dfg^C|Wrong^V #record 3 The junk characters are highlighted and this is a pipe delimited file. Is there anyway to... (20 Replies)
Discussion started by: ashwin3086
20 Replies

9. Solaris

junk characters in ls -l output in solaris10

Dear all, I have installed Solaris10 in a x86 machine.When the ls -l output is taken,at the Month's place some junk characters appear.Rest everything is fine. Cna somebody help..? thanks :b: (7 Replies)
Discussion started by: ragtechy
7 Replies

10. UNIX for Dummies Questions & Answers

Email ends with Junk Characters

I have written the following code ...to include the Subject, Message Body and Attachment with sendmail. When I send mail from my Unix account to diffrent mail servers like Yahoo , Hotmail etc..I recv the Message Body but there is no newline character at the end of each line.... Also I recv the... (5 Replies)
Discussion started by: Amruta Pitkar
5 Replies
Login or Register to Ask a Question