Sponsored Content
Top Forums Shell Programming and Scripting Check Character matching from pos 7-15 to pos 211-219 if True then replace 211-219 with spaces Post 302709451 by Don Cragun on Wednesday 3rd of October 2012 09:00:09 AM
Old 10-03-2012
Quote:
Originally Posted by lancesunny
Script for if characters from positions 7-15 are matching with characters from position 211-219 then replace all char from 211-219 with 9 space.
Total length of record is 413.

Before:
RR 201 123456789 00599 0001230111 +00000000123 XXX 100111......123456789 ..............................................................................1
SS 201 00456 00599 0001230111 +00000000123 XXX 100222......12345..............................................................................2
TT 201 234567890 00599 0001230111 +00000000123 ABC 100333......234567890 ..............................................................................3

Hoping for After:
RR 201 123456789 00599 0001230111 +00000000123 XXX 100111...... ..............................................................................1
SS 201 00456 00599 0001230111 +00000000123 XXX 100222......12345..............................................................................2
TT 201 234567890 00599 0001230111 +00000000123 ABC 100333...... ..............................................................................3

where
1st Record position 7-15 & 211-219 Value=123456789 are matching and therefore we need to replace position 211-219 with 9 space characters
1st Record position 7-15 & 211-219 Value=234567890 are matching and therefore we need to replace position 211-219 with 9 space characters.

Any help on this will be appreciated.
Thanks in advance.
Please use code tags when showing input and output strings. You say that the total record length of each of your input and output records is 413, but the three input records you have given us contain 152, 143, and 152 bytes, respectively (not counting the terminating <newline> character) and each of your three output records contain 143 bytes.

You also give two conflicting descriptions of what is in the 1st record.

Making a guess at what your input really looks like, the following may work:
Code:
awk '{if(length() > 220 && substr($0,7,9) == substr($0,211,9))
                printf("%s         %s\n", substr($0,1,210), substr($0,220))
        else    print}' input

 

3 More Discussions You Might Find Interesting

1. What is on Your Mind?

This Weeks Lottery - Jackpot Now 219,500 Bits

If you want to win some Bits, the jackpot for tomorrow's drawing is up to 219,500 Bits Lottery tickets are only 100 Bits :D (0 Replies)
Discussion started by: Neo
0 Replies

2. Homework & Coursework Questions

POS 420 Week 4 IA

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: <LI style="MARGIN: 0in 0in 0pt; mso-list: l0 level1 lfo1; tab-stops: list .5in" class=MsoNormal>In your week4... (0 Replies)
Discussion started by: smiley76112
0 Replies

3. Shell Programming and Scripting

if characters from positions 7-15 are matching 219 then replace al

Script for if characters from positions 7-15 are matching with characters from position 211-219 then replace all char from 211-219 with 9 space. Total length of record is 420. Here is the specification of the data in file. Position Field Data Type... (2 Replies)
Discussion started by: lancesunny
2 Replies
SUBSTR_REPLACE(3)							 1							 SUBSTR_REPLACE(3)

substr_replace - Replace text within a portion of a string

SYNOPSIS
mixed substr_replace (mixed $string, mixed $replacement, mixed $start, [mixed $length]) DESCRIPTION
substr_replace(3) replaces a copy of $string delimited by the $start and (optionally) $length parameters with the string given in $replace- ment. PARAMETERS
o $string - The input string. An array of strings can be provided, in which case the replacements will occur on each string in turn. In this case, the $replacement, $start and $length parameters may be provided either as scalar values to be applied to each input string in turn, or as arrays, in which case the corresponding array element will be used for each input string. o $replacement - The replacement string. o $start - If $start is positive, the replacing will begin at the $start'th offset into $string. If $start is negative, the replacing will begin at the $start'th character from the end of $string. o $length - If given and is positive, it represents the length of the portion of $string which is to be replaced. If it is negative, it rep- resents the number of characters from the end of $string at which to stop replacing. If it is not given, then it will default to strlen( $string ); i.e. end the replacing at the end of $string. Of course, if $length is zero then this function will have the effect of inserting $replacement into $string at the given $start offset. RETURN VALUES
The result string is returned. If $string is an array then array is returned. EXAMPLES
Example #1 Simple substr_replace(3) examples <?php $var = 'ABCDEFGH:/MNRPQR/'; echo "Original: $var<hr /> "; /* These two examples replace all of $var with 'bob'. */ echo substr_replace($var, 'bob', 0) . "<br /> "; echo substr_replace($var, 'bob', 0, strlen($var)) . "<br /> "; /* Insert 'bob' right at the beginning of $var. */ echo substr_replace($var, 'bob', 0, 0) . "<br /> "; /* These next two replace 'MNRPQR' in $var with 'bob'. */ echo substr_replace($var, 'bob', 10, -1) . "<br /> "; echo substr_replace($var, 'bob', -7, -1) . "<br /> "; /* Delete 'MNRPQR' from $var. */ echo substr_replace($var, '', 10, -1) . "<br /> "; ?> Example #2 Using substr_replace(3) to replace multiple strings at once <?php $input = array('A: XXX', 'B: XXX', 'C: XXX'); // A simple case: replace XXX in each string with YYY. echo implode('; ', substr_replace($input, 'YYY', 3, 3))." "; // A more complicated case where each replacement is different. $replace = array('AAA', 'BBB', 'CCC'); echo implode('; ', substr_replace($input, $replace, 3, 3))." "; // Replace a different number of characters each time. $length = array(1, 2, 3); echo implode('; ', substr_replace($input, $replace, 3, $length))." "; ?> The above example will output: A: YYY; B: YYY; C: YYY A: AAA; B: BBB; C: CCC A: AAAXX; B: BBBX; C: CCC NOTES
Note This function is binary-safe. SEE ALSO
str_replace(3), substr(3), String access and modification by character. PHP Documentation Group SUBSTR_REPLACE(3)
All times are GMT -4. The time now is 09:36 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy