Sponsored Content
Top Forums Shell Programming and Scripting Replace Special Character With Next Present Byte Post 302913951 by dineshnak on Thursday 21st of August 2014 06:13:27 AM
Old 08-21-2014
Hi,

Thanks for your input. While running the script we are not getting the output and also I need to write the output in a file. Could you please help out on this.

Script
Code:
#!/bin/bash
Input=`cat sample.dat`
echo "$Input"
#while read Input
#do
/usr/xpg4/bin/awk -F 'ú' '
#{      printf(" Input:%s\nOutput:", $0) }
{       # Print the 1st field unchanged.
        printf("%s", $1)
        # Loop through remaining fields.
        for(i = 2; i <= NF; i++) {
                # Print 3 spaces.
                printf("   ")
                # Get repetition count
                cnt = sprintf("%d", "0x" substr($i, 1, 2)) + 0
                # Print the character to be repeated cnt times.
                chr = substr($i, 3, 1)
#               printf("|cnt=%d,chr=%s|", cnt, chr)
                for(j = 1; j <= cnt; j++)
                        printf("%s", chr)
                # Print the remainder of the field.
                printf("%s", substr($i, 4))
        }
        # Add trailing <newline>.
        print ""
}' Input
#done <output.dat

Sample.dat
Code:
ABC196 7 MIB513AMMODú07 MIByx66mcp00ú06 302619ú1A 00005014072605331600ú0A 980ú32 201407260533160ú14 2TRG212 7 98ú09 3P PAUTO FMGBN  MIB513AMMOAMERICAN MODER
N INSURANCE302619ú40 97871407259787    140724 NBSú19 A2014072520140724

Thanks,
Dines

---------- Post updated at 01:38 AM ---------- Previous update was at 12:38 AM ----------

One more point if it is decimal use the same else convert it to decimal from hexadecimal.

Thanks,
Dines

---------- Post updated at 05:13 AM ---------- Previous update was at 01:38 AM ----------

Hi Don Cragun,

Ran the script but the ouput is not as expected. Please find the code & output below. Facing problem while converting to hexadecimal
Code:
cnt = sprintf("%d", 0x substr($i, 1, 2)) + 0

We changed the input file name the code it's not working
Script
Code:
#!/bin/bash
#Input=`cat /home/dgovk/sample.dat`
#echo "$Input"
#while read Input
#do
/usr/xpg4/bin/awk -F 'ú' '
#{      printf(" Input:%s\nOutput:", $0) }
{       # Print the 1st field unchanged.
        printf("%s", $1)
        # Loop through remaining fields.
        for(i = 2; i <= NF; i++) {
                # Print 3 spaces.
                printf("   ")
               printf(substr($i, 1, 2))
#               printf("%d", substr($i, 1, 2))
                # Get repetition count
                cnt = sprintf("%d", 0x substr($i, 1, 2)) + 0
                # Print the character to be repeated cnt times.
                chr = substr($i, 3, 1)
#               printf("|cnt=%d,chr=%s|", cnt, chr)
                for(j = 1; j <= cnt; j++)
                        printf("%s", chr)
                # Print the remainder of the field.
                printf("%s", substr($i, 4))
        }
        # Add trailing <newline>.
        print ""
}' Input

Input:
Code:
302619ú1A?
ABCD172 2 B10001ú0E Fú08 DineshGú14 KumarNakkaú0E ú3C?INú14?EFGH340

Output:
Code:
302619   ?
ABCD172 2 B10001   F           DineshG                 KumarNakka      ???IN   ??????????????EFGH340

Thanks,
Dinesh

Last edited by rbatte1; 08-21-2014 at 06:11 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed special character replace

I need to do the following: text in the format of: ADDRESS=abcd123:1111 - abcd123:1111 is different on every system. replace with: ADDRESS=localhost:2222 sed 's/ADDRESS=<What do I use here?>/ADDRESS=localhost:2222/g' Everything I've tried ends up with: ... (3 Replies)
Discussion started by: toor13
3 Replies

2. Shell Programming and Scripting

how to replace the special character with another using SED

I have the replace the pattern in the file , ); to ); Could someone please help me to get this command. (2 Replies)
Discussion started by: mohan.bit
2 Replies

3. Shell Programming and Scripting

cutting long text by special char around 100 byte and newline

Regard, How can i cut the text by special char(|) around 100 byte and write the other of the text at newline using Perl. ... (3 Replies)
Discussion started by: Shawn, Lee
3 Replies

4. Shell Programming and Scripting

How to replace with a special character in String

Hi, I am beginner to Shell Scripting. I have a String like this "testabcdef", i need the first character as it is and the remaining character should be replaced by the the '*' character. e.g(t***********) PLZ Suggest me. (5 Replies)
Discussion started by: nanthagopal
5 Replies

5. Shell Programming and Scripting

replace /n with special character

I would like to replace /n with ',' and after replace remove last semicolon then put a open brace in starting and closing brace in end of line. See below example: input: 1234 3455 24334 234 output: ('1234,'3455',24334','234') Thanks (3 Replies)
Discussion started by: anupdas
3 Replies

6. Shell Programming and Scripting

How to replace special character using sed?

How can I replace the follong text including to number 7000? cat tmp0.txt Winston (UK) Wong I would the 7000 to replace Winston (UK) Wong. I fail with method below: sed ' s /Winston\(UK\)Wong/7000 tmp0.txt' (1 Reply)
Discussion started by: vivien_chu
1 Replies

7. Shell Programming and Scripting

[Solved] Find and replace till nth occurence of a special character

Hi, I have a requirement to search for a pattern in each line in a file and remove the in between words till the 3rd occurrence of double quote ("). Ex: CREATE TABLE "SCHEMANAME"."AMS_LTV_STATUS" (Note: "SCHEMANAME" may changes for different schemas. Its not a fixed value) I need to... (2 Replies)
Discussion started by: satyaatcgi
2 Replies

8. UNIX for Dummies Questions & Answers

Changing a special line and Byte in a random file

Hello I created 3 files by: dd if=/dev/urandom bs=1024 count=1000000 of=./testfile1 dd if=/dev/urandom bs=1024 count=5000000 of=./testfile2 dd if=/dev/urandom bs=1024 count=10000000 of=./testfile3 Now I want to know how to make a change in a specific byte and/or line of theses files? (2 Replies)
Discussion started by: frhling
2 Replies

9. Shell Programming and Scripting

Replace special characters with backslash and character

Hi, I have a string wherein i need to replace special characters with backslash and that character. Ex: If my string is a=qwerty123@!, then the new string should be a_new=qwerty123\@\!\, Thanks (3 Replies)
Discussion started by: temp_user
3 Replies

10. What is on Your Mind?

Merry Xmas (special present inside)

A Merry Xmas to all of you. And, as a special present to vbe (he knows why) a little exercise: #! /bin/ksh pPrintSnow () { typeset -i iLen=$1 while (( iLen )) ; do if ! (( RANDOM % 31 )) ; then printf "%1s" "." else printf "%1s" " " fi ((... (0 Replies)
Discussion started by: bakunin
0 Replies
All times are GMT -4. The time now is 04:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy