Sponsored Content
Top Forums Shell Programming and Scripting Replace Special Character With Next Present Byte Post 302913975 by Don Cragun on Thursday 21st of August 2014 06:21:52 AM
Old 08-21-2014
This does what you requested:
Code:
#!/bin/bash
# Code supplied by Donald W. Cragun in response to post on www.unix.com:
# https://www.unix.com/shell-programming-and-scripting/250310-replace-special-character-next-present-byte-4.html
/usr/xpg4/bin/awk -F 'ú' '
#{	printf(" Input:%s\nOutput:", $0) }
{	printf("%s", $1)
	for(i = 2; i <= NF; i++) {
		# Print 3 spaces.
		printf("   ")

		# Get repetition count.  (This seems likely to be error prone,
		# Dnd therefore dangerous, but this is what was specified by
		# Dineshkumar Nakka.
		if((cnt = substr($i, 1, 2)) ~ "[0-9][0-9]") {
			# Use decimal if both characters are decimal digits.
			base = cnt
			cnt = cnt + 0
		} else {# Otherwise, use hexadecimal.
			base = "0x" cnt
			cnt = sprintf("%d", "0x" cnt) + 0
		}

		# Print the character to be repeated cnt times.
		chr = substr($i, 3, 1)
#		printf("|cnt=%d(from %s),chr=%s|", cnt, base, 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 ""
}' sample.dat > output.dat

If sample.dat contains:
Code:
302619ú1A? 
ABCD172 2 B10001ú0E Fú08 DineshGú14 KumarNakkaú0E ú3C?INú14?EFGH340
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

it will produce the output:
Code:
302619   ?????????????????????????? 
ABCD172 2 B10001                 F           DineshG                 KumarNakka                    ????????????????????????????????????????????????????????????IN   ??????????????EFGH340
ABC196 7 MIB513AMMOD          MIByx66mcp00         302619                             00005014072605331600             980                                   201407260533160                 2TRG212 7 98            3P PAUTO FMGBN  MIB513AMMOAMERICAN MODER
N INSURANCE302619                                           97871407259787    140724 NBS                      A2014072520140724

in the file output.dat.
 

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
basename(1)							   User Commands						       basename(1)

NAME
basename, dirname - deliver portions of path names SYNOPSIS
/usr/bin/basename string [suffix] /usr/xpg4/bin/basename string [suffix] dirname string DESCRIPTION
The basename utility deletes any prefix ending in / and the suffix (if present in string) from string, and prints the result on the stan- dard output. It is normally used inside substitution marks (``) within shell procedures. /usr/bin The suffix is a pattern defined on the expr(1) manual page. /usr/xpg4/bin The suffix is a string with no special significance attached to any of the characters it contains. The dirname utility delivers all but the last level of the path name in string. EXAMPLES
Example 1 Setting environment variables The following example, invoked with the argument /home/sms/personal/mail sets the environment variable NAME to the file named mail and the environment variable MYMAILPATH to the string /home/sms/personal: example% NAME=`basename $HOME/personal/mail` example% MYMAILPATH=`dirname $HOME/personal/mail` Example 2 Compiling a file and moving the output This shell procedure, invoked with the argument /usr/src/bin/cat.c, compiles the named file and moves the output to cat in the current directory: example% cc $1 example% mv a.out `basename $1 .c` ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of basename and dirname: LANG, LC_ALL, LC_CTYPE, LC_MESSAGES, and NLSPATH. EXIT STATUS
The following exit values are returned: 0 Successful completion. >0 An error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: /usr/bin +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ /usr/xpg4/bin +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWxcu4 | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
expr(1), basename(3C), attributes(5), environ(5), standards(5) SunOS 5.11 18 Mar 1997 basename(1)
All times are GMT -4. The time now is 10:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy