Print byte position of extended ascii character

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Print byte position of extended ascii character
# 29  
Old 07-20-2018
It's a copy past error , I have the ! sign on the script.

I am still receiving the same error

Code:
#!/usr/bin/bash
while read T
  do    ((CNT++))
        for ((i=0; i<${#T}; i++))
          do    LC_ALL=C TMP=$(printf "%d\n" "'"${T:i:1})
                [ $TMP -gt 127 ] && printf "%d %c %d\n" $i ${T:i:1} $CNT
          done
  done <Test.TXT

# 30  
Old 07-20-2018
Well it looks as though %d is sign extended so try this:
Code:
#!/bin/bash
while read T
  do    ((CNT++))
        for ((i=0; i<${#T}; i++))
          do    LC_ALL=C TMP=$(printf "%d\n" "'"${T:i:1})
                [ $TMP -lt 0 ] && printf "%d %c %d\n" $i ${T:i:1} $CNT
          done
  done </tmp/Test.TXT

OSX 10.13.5, default bash terminal:
Code:
Last login: Fri Jul 20 19:42:36 on ttys000
AMIGA:amiga~> ./bin_char.sh
5 ? 1
6 ? 1
6 ? 2
7 ? 2
6 ? 3
7 ? 3
6 ? 4
7 ? 4
5 ? 5
6 ? 5
7 ? 5
8 ? 5
4 ? 6
5 ? 6
7 ? 6
8 ? 6
4 ? 7
5 ? 7
AMIGA:amiga~> _

EDIT, 21-07-2018, 6:34am, UK time.
Don't forget to change your first line to read #!/usr/bin/bash ...

Last edited by wisecracker; 07-21-2018 at 01:34 AM.. Reason: See above.
This User Gave Thanks to wisecracker For This Post:
# 31  
Old 07-21-2018
The script is running but where is it writing the output to ?

Test.TXT is my input file , can I re-direct the output to a file rather than printing it on the screen ?
# 32  
Old 07-21-2018
Hi rosebud123...

This is a working model.
I have used the '/tmp' directory for ease of testing.
READ THE NOTES INSIDE THE CODE!
Code:
#!/bin/bash
while read T
  do    ((CNT++))
        for ((i=0; i<${#T}; i++))
          do    LC_ALL=C TMP=$(printf "%d\n" "'"${T:i:1})
                [ $TMP -lt 0 ] && printf "%d\t%c\t%d\n" $i ${T:i:1} $CNT
          done
  done < /tmp/Test.TXT > /tmp/done.TXT
cat /tmp/done.TXT
# exit 0

# NOTES:
# 1) This script will ONLY remove _binary_ characters, decimal 128 - 255.
# 2) NULL character, decimal 0, WILL BREAK this script!!! If one is encountered a different approach is required.
# 3) The DELETE/BACKSPACE character, decimal 127, and Control characters can be catered for if need be.
# 4) The CODE PAGE 437 '?' characters in the centre column cannot be displayed in single byte UTF8.
# 5) Displayed as BYTE_POSITION_RELATIVE_TO_0, UNKNOWN_CHARACTER, LINE_NUMBER and now TAB spaced.
# 6) YOU will have to change the absolute PATH and filename(s) to suit yourself.
# 7) Have fun!

OSX 10.13.5, default bash terminal.
Code:
Last login: Sat Jul 21 20:11:00 on ttys000
AMIGA:amiga~> ./bin_char.sh
5	?	1
6	?	1
6	?	2
7	?	2
6	?	3
7	?	3
6	?	4
7	?	4
5	?	5
6	?	5
7	?	5
8	?	5
4	?	6
5	?	6
7	?	6
8	?	6
4	?	7
5	?	7

This User Gave Thanks to wisecracker For This Post:
# 33  
Old 07-21-2018
The script sits there and never completes , is the script expecting proper record terminators on the file ?

Code:
#!/usr/bin/bash
while read T
  do    ((CNT++))
        for ((i=0; i<${#T}; i++))
          do    LC_ALL=C TMP=$(printf "%d\n" "'"${T:i:1})
                [ $TMP -lt 0 ] && printf "%d %c %d\n" $i ${T:i:1} $CNT
          done
  done <Test.TXT > output.test127.txt
~

# 34  
Old 07-21-2018
You are running before you can walk!

Where has the Tilde '~' come from?

Why not copy and paste MY version into your editor and save that, make a backup, test and then modify it.

EDIT:
Don't forget to change the FIRST line to #!/usr/bin/bash ...

Last edited by wisecracker; 07-21-2018 at 05:28 PM.. Reason: See above.
# 35  
Old 07-21-2018
It's a copy paste error , I do not have ~ in the script.

I opened the script in vi editor and when I copy pasted it added a ~

Code:
#!/usr/bin/bash
while read T
  do    ((CNT++))
        for ((i=0; i<${#T}; i++))
          do    LC_ALL=C TMP=$(printf "%d\n" "'"${T:i:1})
                [ $TMP -lt 0 ] && printf "%d %c %d\n" $i ${T:i:1} $CNT
          done
  done <Test.TXT > output.test127.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removal Extended ASCII using awk

Hi All, I am trying to remove (SELECTIVE - passed as argument) Extended ASCII using Awk based on adhoc basis. Can you please let me know how to do it. I have to implement this using awk only. Thanks & Regads (14 Replies)
Discussion started by: tostay2003
14 Replies

2. Programming

How to read extended ASCII characters from stdin?

Hi, I want to read extended ASCII characters from keyboard using c language on unix/linux. How to read extended characters from keyboard or by copy-paste in terminal irrespective of locale set in the system. I want to read the input characters from keyboard, store it in an array or some local... (3 Replies)
Discussion started by: sanzee007
3 Replies

3. Shell Programming and Scripting

Search and Replace Extended Ascii Characters

We are getting extended Ascii characters in the input file and my requirement is to search and replace them with a space. I am using the following command LANG=C sed -e 's// /g' It is doing a good job, but in some cases it is replacing the extended characters with two spaces. So my input... (12 Replies)
Discussion started by: ysvsr1
12 Replies

4. Shell Programming and Scripting

Print the next ASCII character

Hi, In my file, for few field I have to print the next ASCII character for every character. In the below file, I have to do for the 2,3 and 5th fields. Input File ======== 1|abc|def|5|ghi 2|jkl|mno|6|pqr Expected Ouput file ======= 1|bcd|efg|5|hij 2|klm|nop|6|qrs (2 Replies)
Discussion started by: machomaddy
2 Replies

5. AIX

Printing extended ASCII

Hi All, I'm trying to send extended ascii characters to my HP2055 as part of PCL printer control codes. What I want to do is select a bar code font, print the bar code and reset the printer to the default font. Selecting the bar code font works good. Printing the bar code goes almost ok too. ... (5 Replies)
Discussion started by: petervg
5 Replies

6. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (1 Reply)
Discussion started by: manaswinig
1 Replies

7. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (2 Replies)
Discussion started by: manaswinig
2 Replies

8. UNIX for Advanced & Expert Users

Processing extended ascii character file names in UNIX (BASH scipts)

Hi, I have a accentuated letter (ö) in a script for an Installer. It's a file name. This is not working and I'm told to try using the octal value for the extended ascii character. Does anyone no how to do this? If I had the word "filförval", can I just put in the value between the letters, like... (9 Replies)
Discussion started by: peli
9 Replies

9. Shell Programming and Scripting

extended ascii problem

hi i would like to check text files if they contain extended ascii characters within or not. i really dont have any idea how to start your kind help would be very much appreciated thanks. (7 Replies)
Discussion started by: smooth
7 Replies

10. Programming

Extended ascii

Hi all, I would like to change the extended ascii code ( 128 - 255). I tried to change LC_ALL and LANG in current session ( values from locale -a) and for no good. Thanks. (0 Replies)
Discussion started by: avis
0 Replies
Login or Register to Ask a Question