Search and replace specific positions of specific lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace specific positions of specific lines
# 1  
Old 03-29-2017
Search and replace specific positions of specific lines

Hi,

I have a file with hundreds of lines. I want to search for particular lines starting with 4000, search and replace the 137-139 position characters; which will be '000', with '036'. Can all of this be done without opening a temp file and then moving that temp file to the original file name.

sample input
Code:
400300000038655300580061482900007000000287546004100457338000010000000850510006001389990000100000008391600060013799700016000000081649009300137011000020000000773100012000
4004001259770000300000006090000170010599300018000000058339010400095541000080000000463550046000759680001000000002942500580005                                            
4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY OF AUSTRALIA       CASTLE HILL  AU 5599   0000000097950007077Y10001 6022100000     0 0
40010000000000 00000000000000000000                                                                                                                                     
4002467076359965203 0042298585118131 2422240200000000N4597104019M462239          7                                                               0000000000000000000    
4000422985400050406669990541353200000007422985707371044941247610075641MC0314ANGLICARE WA             EAST PERTH   AU 5969   0000000002100007073N10001 6051800000     CR0

Code:
$ uname -a
Linux lxserv01 2.6.18-417.el5 #1 SMP Sat Nov 19 14:54:59 EST 2016 x86_64 x86_64 x86_64 GNU/Linux

# 2  
Old 03-29-2017
Hi, try:
Code:
sed '/^4000/s/\(.\{136\}\).../\1036/' file

or
Code:
sed -r '/^4000/s/(.{136}).../\1036/' file

You can use the -i in place option with sed on your system, but it is best to then also specify a backup file with command, for example -ibak , unless you already have a backup...
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-29-2017
Quote:
Originally Posted by Scrutinizer
Hi, try:
Code:
sed '/^4000/s/\(.\{136\}\).../\1036/' file

or
Code:
sed -r '/^4000/s/(.{136}).../\1036/' file

You can use the -i in place option with sed on your system, but it is best to then also specify a backup file with command, for example -ibak , unless you already have a backup...
if i understand correctly, you are searching for lines starting with 4000, go on with any characters till the 136th position; the next 3 dots (...) should be replaced with a 036;\1 ->would be the 1st instance of the search yielding a positive result.

Is the above explanation correct? Also why did you specify a -r (extended regular expressions) flag

And yes, I will be using the -i.bak option
# 4  
Old 03-29-2017
If you compare the two sed scripts, the one with the -r option is way simpler (not that many escapes needed).
The \1 ("back reference") substitutes the 1. paranthesized regex part with itself.
In Scrutinizer's proposal there's no check the three to-be-replaced characters really are "000" as specified in post#1.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-29-2017
Quote:
Originally Posted by dsid
if i understand correctly, you are searching for lines starting with 4000, go on with any characters till the 136th position; the next 3 dots (...) should be replaced with a 036;\1 ->would be the 1st instance of the search yielding a positive result.
[..]
That is correct, a dot in regex means "any character" .



--
Quote:
Originally Posted by RudiC
[..]
In Scrutinizer's proposal there's no check the three to-be-replaced characters really are "000" as specified in post#1.
Hi RudiC, IMO that was not a specification, but rather a remark. It says: which will be '000', therefore I used '...' instead..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-29-2017
Quote:
Originally Posted by Scrutinizer
That is correct, a dot in regex means "any character" .



--

Hi RudiC, IMO that was not a specification, but rather a remark. It says: which will be '000', therefore I used '...' instead..
position 137-139 will definitely be 000 and that needs to be replaced by 036.

thank you both for the explanations

actually I just thought about it RudiC is correct, (...) would mean any characters, can I replace the 3 dots with 000, since I am sure positions 137-139 will be zeros.
# 7  
Old 03-29-2017
You can use 000 instead of ..., which means that it will only be replaced by 036 if positions 137-139 will be zeroes on a line that starts with 4000. Any other value will not get replaced, and be left as is..

If you use ... then any value there will be replaced.

Also, if on a line that starts with 4000 positions 137-139 will always be zeroes (the way I interpreted your specification) , then it does not matter what you use..

--- edit ---
As is apparent in another thread if 000 is used instead of ... then anchoring is needed to the beginning of the line in the form of a caret ( ^ ) otherwise it will try to match 000 with 136 arbitrary characters before it ..

Last edited by Scrutinizer; 03-31-2017 at 02:39 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace specific positions in a file

I have a fixed-length positional file. I am trying to replace content of position 4-13 (length=10) with xxxxxxxxxx. Sample 2 rows in this file: H0187459823 172SMITH, JOE H0112345678 172DOE, JANE In this example 87459823 (from 1st line) and 12345678 (from 2nd line) (both in position... (3 Replies)
Discussion started by: Diver181
3 Replies

2. Shell Programming and Scripting

Filter lines based on values at specific positions

hi. I have a Fixed Length text file as input where the character positions 4-5(two character positions starting from 4th position) indicates the LOB indicator. The file structure is something like below: 10126Apple DrinkOmaha 10231Milkshake New Jersey 103 Billabong Illinois ... (6 Replies)
Discussion started by: kumarjt
6 Replies

3. Shell Programming and Scripting

Search Replace Specific Column using RegEx

Have Pipe Delimited File: > BRYAN BAKER|4/4/2015|518 VIRGINIA AVE|TEST > JOE BAXTER|3/30/2015|2233 MockingBird RD|ROW2On 3rd column where the address is located, I want to add a space after every numeric value - basically doing a "s//&\ / ": > BRYAN BAKER|4/4/2015|5 1 8 VIRGINIA AVE|TEST > JOE... (5 Replies)
Discussion started by: svn
5 Replies

4. UNIX for Dummies Questions & Answers

Search specific string logfile specific date range

Hi, I have logfile like this.. === 2014-02-09 15:46:59,936 INFO RequestContext - URL: '/eyisp/sc/skins/EY/images/pickers/comboBoxPicker_Over.png', User-Agent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko': Unsupported with Accept-Encoding header === 2015-02-09... (8 Replies)
Discussion started by: kishk
8 Replies

5. UNIX for Dummies Questions & Answers

Search for a specific String in a log file for a specific date range

Hi, I have log file which rolls out every second which is as this. HttpGenRequest - -<!--OXi dbPublish--> <created="2014-03-24 23:45:37" lastMsgId="" requestTime="0.0333"> <response request="getOutcomeDetails" code="114" message="Request found no matching data" debug="" provider="undefined"/>... (3 Replies)
Discussion started by: karthikprakash
3 Replies

6. Shell Programming and Scripting

sed to replace specific positions on line with file contents

Hi, I am trying to use an awk command to replace specific character positions on a line beginning with 80 with contents of another file. The line beginning with 80 in file1 is as follows: I want to replace the 000000000178800 (positions 34 - 49) on this file with the contents of... (2 Replies)
Discussion started by: nwalsh88
2 Replies

7. Shell Programming and Scripting

Count specific characters at specific column positions

Hi all, I need help. I have an input text file (input.txt) like this: 21 GTGCAACACCGTCTTGAGAGG 50 21 GACCGAGACAGAATGAAAATC 73 21 CGGGTCTGTAGTAGCAAACGC 108 21 CGAAAAATGAACCCCTTTATC 220 21 CGTGATCCTGTTGAAGGGTCG 259 Now I need to count A/T/G/C numbers at each character location in column... (2 Replies)
Discussion started by: thienxho
2 Replies

8. Shell Programming and Scripting

Urgent request to consider:Search specific name in a file and fetch specific entries

Hi all, I have 2 files, One file contain data like this FHIT CS CHRM1 PDE3A PDE3B HSP90AA1 PTK2 HTR1A ESR1 PARP1 PLA2G1B These names are mentioned in the second file(Please see attached second file) as # Drug_Target_X_Gene_Name:(Where X can be any number (1-1000) (1 Reply)
Discussion started by: manigrover
1 Replies

9. Shell Programming and Scripting

Help search and replace hex values only in specific files

perl -pi -e 's/\x00/\x0d\x0a/g' `grep -l $'GS' filelist` This isn't working :confused:, it's not pulling the files that contain the regex. Please help me rewrite this :wall:. Ideally for this to work on 9K of 20K files in the directory, I've tried this but I don't know enough about awk... (7 Replies)
Discussion started by: verge
7 Replies

10. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies
Login or Register to Ask a Question