Control character in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Control character in a file
# 1  
Old 04-25-2007
Control character in a file

Hi All,

I am looking for a solution to capture any ASCII control character in a file
( where the ASCII control character is in decimal value from 0 to 31 and 127
( Hex value from 00 to 1F and 7F ) ) by returning any affected lines.

The intended good file should contain "ASCII printable character" where ASCII
printable character is in decimal value from 32 to 126 ( Hex value from 20 to
7E ).

Thanks in advance.

Best regards,
cursive
# 2  
Old 04-25-2007
Removes ASCII control characters from 0 to 31 and 127
Code:
tr -d "\000-\037\177" < file

Code:
while read line 
do
       [[ -n "$( echo $line | tr -d "\000-\037\177" )" ]] && echo "$line"
done < file


Last edited by anbu23; 04-25-2007 at 06:33 AM.. Reason: Not read requirement properly
# 3  
Old 04-25-2007
you may also want to try awk's POSIX character classes like [:print:],[:graph:] etc...
# 4  
Old 04-25-2007
Hi Anbu23,

I am not sure if I missing something here by using the given solution :
tr -d "\000-\037\177" < file because the output is still contained the control
character.

Provided a sample of the input file :
-------------------------------------------------
Monday Tuesday Wednesday
Thursday Friday
Saturday 
Sunday
Number = 0.36")· )

Apple orange
Pinapple


Output file ( display lines with control character ):
---------------
>Monday Tuesday Wednesday
>Saturday 
>Number = 0.36")· )
>Apple orange
>


Thanks in advance,
cursive
# 5  
Old 04-25-2007
Code:
/db2home/training$ cat -e f
Monday Tuesday Wednesday$
Thursday Friday$
Saturday ^F$
Sunday$
Number = 0.36")· )$
$
Apple orange $
Pinapple$
^G$
/db2home/training$ tr -d "\000-\011\013-\037\177-\377" < f | cat -e
Monday Tuesday Wednesday$
Thursday Friday$
Saturday $
Sunday$
Number = 0.36") )$
$
Apple orange $
Pinapple$
$

# 6  
Old 04-26-2007
Hi anbu23,

Thanks for the solution and it work perfectly.

Best regards,
cursive
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Removing the ^M control character

I've got a file where each line is separated by ^M characters. I want to be able to cat the file without those lines. When I cat the file now what I see are blank lines. However, the blank lines are actually ^M characters; when I open the file with vi they show up. X38888 No No No... (7 Replies)
Discussion started by: newbie2010
7 Replies

2. Shell Programming and Scripting

Replace Control M (^M) character with new line

Hi All, We are getting an external file in abc.csv format. When opened in excel spread sheet, it is opening alright. But when opened in notepad, I see the contents in a single line. Ftp'd the file in binary mode to AIX UNIX host. When opened, I see Control M (^M) characters in place of New Line... (16 Replies)
Discussion started by: njny
16 Replies

3. Shell Programming and Scripting

Formatting file data to another file (control character related)

I have to write a program to read data from files and then format into another file. However, I face a strange problem related to control character that I can't understand and solve. The source file is compose of many lines with such format: T_NAME|P_NAME|P_CODE|DOCUMENT_PATH|REG_DATE ... (3 Replies)
Discussion started by: hk6279
3 Replies

4. Shell Programming and Scripting

control M character in unix file

in a file we are getting control character in a file , is there any way that they can be removed once we have the file for eg. BEGIN-PROCEDURE INITIALIZE ^M LET #row_count = 0^M ^M ^M (2 Replies)
Discussion started by: lalitpct
2 Replies

5. Shell Programming and Scripting

perl cmd to remove the control-Z character at end of 10GB file

In a 10-50GB file , at end of file there is Control-z character tried the below options, 1. perl -p -i -e 's/^Z//g' new.txt 2. perl -0777lwi -032e0 new.txt and Sed command, dos2unix etc it takes more time to remove the control-z. need a command or perl program to GO TO LAST LINE OF FILE ... (7 Replies)
Discussion started by: prsam
7 Replies

6. Programming

Problem with control file and special character

I am getting error when loading data file using ctl file. I get this error only when there is special character. Below is some data. DataFile=> company_id|ciu_id|english_name|iso_country_code|active|partner_name 1-2JT-122||Expert Järvenpää|FI|A|Expert Järvenpää Control File=> LOAD DATA... (1 Reply)
Discussion started by: rshivarkar
1 Replies

7. UNIX for Dummies Questions & Answers

Odd Control Character issue ^A

Sorry to bug you, but my sed is failing me, I have a file auto generated from abinitio, it has a string of chars ending with a line break, and then it has added a ^A character, I can remove this is vi by using the following %s/^A//g (where ^A is ctrl v and control A), however when I try to sed... (1 Reply)
Discussion started by: badg3r
1 Replies

8. Shell Programming and Scripting

Replace any control character in the string

Need to replace any control character in the string in perl ---------- Post updated at 04:22 PM ---------- Previous update was at 03:50 PM ---------- Any help !!! Thanks in advance (2 Replies)
Discussion started by: hansini
2 Replies

9. UNIX for Dummies Questions & Answers

How to find the ^M(control M) character in unix file?

can any one say about command to find "^M" (Control M)characters in a unix text file. ^M comes when a file ftped from windows to unix without using bin mode. I need the command to find lik this, ex.txt: ------------------------------ ...,name,time^M go^M ...file,end^M... (5 Replies)
Discussion started by: prsam
5 Replies

10. Shell Programming and Scripting

Removing Control M character

Hi, I'm using MKS tool kit to execute KSH on windows and samba to move files to unix from windows. My script is appending header record for the data file. I'm using echo "$header" > $SambaFilename cat $windowsfile >> $SambaFilename But after execution of script ,The file in Unix... (2 Replies)
Discussion started by: ammu
2 Replies
Login or Register to Ask a Question