Search comppare replace (substr/lppad)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search comppare replace (substr/lppad)
# 1  
Old 05-13-2016
Oracle Search comppare replace (substr/lppad)

Hi Everyone,

I have a data file with data as below which contains millions of data and gets loaded to database using SQL loader using positional notation.
Code:
AT    0001      000000000100000000000
BE    4         000000000000030000000
DE    0055      000004000000000000000
FR    0         000000000000000110000
CZ    003       000330000000050000000

for eg: pos 7 to 10 will yield
Code:
0001
4   
0055
0   
003

My requirement is to lpad to 4 digits if the string 7-10 is having length < 4 chars.

the output should be like as below.
Code:
AT    0001      000000000100000000000
BE    0004      000000000000030000000
DE    0055      000004000000000000000
FR    0000      000000000000000110000
CZ    0003      000330000000050000000

Already burnt a lot of midnight oil for this and what i could able to do is the below.
Code:
awk '{print substr($0,7,4) }' /tmp/BKP_PIX_STG_MAST_200205110.dat

followed by sed which is not giving proper result.

I will really be grateful if anyone can focus some insight or guide me in this regard.

Many Thanks...

Last edited by Showdown; 05-13-2016 at 07:27 PM.. Reason: change in data
# 2  
Old 05-13-2016
Code:
$ cat data
AT    1      000000000100000000000
BE    4      000000000000030000000
DE    55      000004000000000000000
FR    0      000000000000000110000
CZ    3      000330000000050000000

$ awk '{ printf("%-6s%04d      %s\n", $1,$2,$3); }' < data
AT    0001      000000000100000000000
BE    0004      000000000000030000000
DE    0055      000004000000000000000
FR    0000      000000000000000110000
CZ    0003      000330000000050000000

$

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-13-2016
Oracle

Thanks Corona688.

The data file is not a space/ fixed delimited file. I have just provided the example with the format which looks delimited. But actually i need to do lpad for the character position 7-11 or it can be 181-184 which is not fixed

Anyway Thanks for your reply and I will take this idea up to achieve my requirement.
# 4  
Old 05-14-2016
How about
Code:
awk '{split (POS, P, "-"); P[2]++; printf "%s%04d%s\n", substr($0, 1, P[1]-1), substr($0, P[1], P[2]-P[1]), substr ($0, P[2])}' POS="7-10" file

This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-14-2016
Oracle

Hi Rudic,

Much thanks for your help. It pretty much serve my purpose.
To be true, I couldn't understand the syntax which I am trying now to understand.

The only discrepancy here is after formatting using the code mentioned by you the next column is getting shifted by 4 spaces. Please see below.

Code:
XXXXXXX XXXXX                 MM11111    ZM    0         000000000000000000000OREGON XXXXXXX HXN  001111  01010  -->Original
XXXXXXX YYYYY                 MM11111    ZM 0000     000000000000000000000OREGON XXXXXXX HXN  001111  01010      -->After format


If you can help me with the code modification so that the total record length remains the same and next column value doesn't get shifted by 4 spaces it will be really great of you.

Thanks again.
# 6  
Old 05-14-2016
When I run RudiC's code with a file containing:
Code:
XXXXXXX XXXXX                 MM11111    ZM    0         000000000000000000000OREGON XXXXXXX HXN  001111  01010

and the parameters POS="48-51" and the name of the file containing the above text, I get the output:
Code:
XXXXXXX XXXXX                 MM11111    ZM    0000      000000000000000000000OREGON XXXXXXX HXN  001111  01010

which would seem to produce what you want in a manner similar to the example you provided in post #1 in this thread (without shifting any other data around).

Are you saying that you have changed the format of your input data such that the number in the field you are zero-filling is right justified instead of left justified as it was in your original sample input???
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-15-2016
Code:
$ cat sample.txt
AT    0001      000000000100000000000
BE    4         000000000000030000000
DE    0055      000004000000000000000
FR    0         000000000000000110000
CZ    003       000330000000050000000

If the range is 7 to 10, blue 10-7, red 7-1:
Code:
$ perl -pe '$sub = substr $_, 6, 4 and $padd = sprintf "%04d", $sub and s/$sub/$padd/' sample.txt
AT    0001      000000000100000000000
BE    0004      000000000000030000000
DE    0055      000004000000000000000
FR    0000      000000000000000110000
CZ    0003      000330000000050000000

If the range is 48 to 51, blue 51-48, red 48-1:
Code:
$ perl -pe '$sub = substr $_, 47, 3 and $padd = sprintf "%04d", $sub and s/$sub/$padd/' showdown.txt
XXXXXXX XXXXX                 MM11111    ZM    0000       000000000000000000000OREGON XXXXXXX HXN  001111  01010

If the range is 181 to 184, blue 184-181, red 181-1

You can let the calculation be done by the program if the range is supplied in the form of (x,y), for example.

Code:
$ perl -pe 'BEGIN{@p=(48,51); $len=$p[1]-$p[0]; --$p[0]} $sub = substr $_, $p[0], $len and $padd = sprintf "%04d", $sub and s/$sub/$padd/' showdown.txt
XXXXXXX XXXXX                 MM11111    ZM    0000       000000000000000000000OREGON XXXXXXX HXN  001111  01010

This User Gave Thanks to Aia For This Post:
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