Search comppare replace (substr/lppad)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search comppare replace (substr/lppad)
# 8  
Old 05-15-2016
Is it possible you searched for 8 chars to be replaced by a 4 char zero padded string? Try
Code:
awk ' 
        {split (POS, P, "-")
         P[2]++
         LEN = P[2] - P[1]
         printf "%s%0*d%s\n", substr($0, 1, P[1]-1), LEN, substr($0, P[1], LEN), substr ($0, P[2])
        }
' POS="45-55" file
XXXXXXX XXXXX                 MM11111    ZM    0         000000000000000000000OREGON XXXXXXX HXN  001111  01010  -->Original
XXXXXXX XXXXX                 MM11111    ZM 00000000000  000000000000000000000OREGON XXXXXXX HXN  001111  01010  --> After

This User Gave Thanks to RudiC For This Post:
# 9  
Old 05-16-2016
Thanks Rudic. Your code is working fine. I did some mistake in the positional notation.

Thank You Don for jumping in and assisting.

Aia - I will definitely try all the codes you have mentioned.

Many thanks to everyone for your kind help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to comppare two files using rwo fields

I use the two awk scripts for comparing file1 and file2. First awk compare $3 column: awk -v OFS="\t" 'NR==FNR{a=$4;next}{$2=$2 "\t"(a?a:"-")}1' file1 file2Second awk compare $2 column: awk -v OFS="\t" 'NR==FNR{a=$4;next}{$2=$2 "\t"(a?a:"-")}1' file1 file2 The only difference ... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

3. Shell Programming and Scripting

Search for a substr with nawk

Hi, I have files, with fixed length fields/let's say every field 5 positions/, like this: xxxx 140 xxxxx xxxx 140 xxxxx xxxx 1400 xxxxx xxxx 150 xxxxx I need to get only the records, which have 140 in the second column. I use that command: nawk '{if (substr($0,6,3)=="140") print $0}'... (3 Replies)
Discussion started by: apenkov
3 Replies

4. Shell Programming and Scripting

search and replace.

Hi, I have a file which contains data in this form. /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */;... (2 Replies)
Discussion started by: arijitsaha
2 Replies

5. UNIX for Dummies Questions & Answers

Help with search and replace or search only of / in vi

Hi all, I am editing a config file in vi that has a / on it. At the moment, search and replace looks alright as am able to use a # as a temporary separator, i.e. :,$s#/u01/app#/u02/app#g For doing a search, I have to escape the / do. So if I want to search for /u01/app, I am having to do... (2 Replies)
Discussion started by: newbie_01
2 Replies

6. Shell Programming and Scripting

AWK Substr - find and replace question...

Hello Experts, I have a input file that I need to replace a value only if the file contains the number 6 in column 1. I would like to use AWK in a shell script (ksh on a AIX platform). I need all rows written out, but only change 2 fields when the first column contains a numer 6. Input... (2 Replies)
Discussion started by: scottb
2 Replies

7. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

8. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

9. Shell Programming and Scripting

Search and replace

Hi All, Suppose I have a file "a.doc" It's contents are : mos44.0 ) ..... ...... ..... export TKTS_RELEASE=tkts44.0b7 ...... ....... Now , I need to first search for the pattern "mos44.0 )" and then TKTS_RELEASE will... (8 Replies)
Discussion started by: sanyerra
8 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question