Replace a string within a file.. with help of positions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a string within a file.. with help of positions
# 1  
Old 03-02-2011
Replace a string within a file.. with help of positions

I have a huge file with lot of rows... with each row around 400 characters.. with spaces as well..

(e.g)
Line1:
"AC254600606 USDMI000001Anom01130073981 0000000000000.002005040720991231 999 NNNNdir01130073981 dir0113007 MEDE DEF CO Y MR. NN "

I wanted to replace the characters from '12 - 22' in each line..
(i.e) old value " USD"
new value " 1100USD"

Can anyone please help me with this.

Thanks in advance.
Gopi
# 2  
Old 03-02-2011
Try:
Code:
perl -pe 's/(?<=.{11}).{11}/    1100USD/' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 03-02-2011
Replace a string within a file.. with help of positions

Hi,

Thanks for the reply.. The code worked.. but i have a lil diffrent reqt...

Actually.. The lines will contain different values...

Characters in between 12 -22 in each line... i m giving an example below...
OLD Values:
Code:
"        USD"
"        EUR"
"        JPY"
"     USDUSD"
"     INRINR"
"         UN"

New Values:
Code:
"    1100USD"
"    1100EUR"
"    1100JPY"
" 1100USDUSD"
" 1100INRINR"
"     1100UN"

I need to add "1100" just before each of these words within "11 - 22" positions, in each line.... There will be 4 spaces for sure.... but the point is there can be more spaces also.. but i should add "1100" just before the existing word.. and keep the remaining as spaces..
Please help.

Thanks.
Gopi

Last edited by Franklin52; 03-02-2011 at 09:57 AM.. Reason: Please use code tags
# 4  
Old 03-02-2011
See if this works for you:
Code:
sed 's/    \([^ ]\)/1100\1/' inp_file

This User Gave Thanks to Shell_Life For This Post:
# 5  
Old 03-02-2011
Replace a string within a file.. with help of positions

Thanks a ton for this...
It worked...
Code:
sed 's/    \([^ ]\)/1100\1/' File

But i don't understand how it changed exactly in the same position and not in other positions..
Also i have a requirement... where in i have to do such changes in other positions as well...
So please let me know the meaning behind this... as i wanted to replace certain things.. near 150,225 th position's in each line.. in the file..

Thanks in advance..
Gopi

Last edited by radoulov; 03-02-2011 at 06:52 PM.. Reason: Code tags!
# 6  
Old 03-02-2011
Quote:
Originally Posted by gopeezere
Also i have a requirement...
Hi, Gopi

In the future, please take the time to detail the problem in its entirety to the best of your ability in the first post. Doing so will help those that are trying to help you not waste time on suboptimal approaches.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 03-02-2011
As an example to replace four spaces proceeding the next word from column 150 with a value of "8251", you would do this (Note requires sed that supports the -E option):

Code:
sed -E 's/^(.{150}.*)    ([^ ])/\18251\2/' infile

Input:
Code:
      1         1         1
      4         5         6
...789012345678901234567890123456
---------------------------------
...567484   498.23      EF  funny
...  8484    32.58     FAU  funky
... 14841  1234.60       R  happy

Output:
Code:
      1         1         1
      4         5         6
...789012345678901234567890123456
---------------------------------
...567484   498.23  8251EF  funny
...  8484    32.58 8251FAU  funky
... 14841  1234.60   8251R  happy


Last edited by Chubler_XL; 03-03-2011 at 06:58 PM..
This User Gave Thanks to Chubler_XL For This Post:
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

Replace multiple positions in records which match crireria

I have a test file a.txt 001 123 456 789 002 This is just a 001 test data 003 file. I want to clear columns 5 and 6 if the first 3 characters are 001 using awk. I tried following but does not work. Any suggestions? awk 'BEGIN{OFS=FS=""} {if (substr($0,1,3)=="123") $5=" "; $6="... (20 Replies)
Discussion started by: Soham
20 Replies

3. Shell Programming and Scripting

Replace characters at fixed positions

My objective is to replace the 8th, 9th, 10th characters by 1 space per character (total 3 spaces) in a file. I achieved this using following command: sed 's/\(.\)/\1@/7;s/@\(...\)/ /' FileData.txt > FileData_UPDATED.txt Another situation comes when I need to done same but excluding 1st... (5 Replies)
Discussion started by: manishdivs
5 Replies

4. UNIX for Dummies Questions & Answers

Replace alphabets from certain positions

Hi all, I have column 2 full of values like HIVE4A-56 and HIVE4-56. I want to convert all values like HIVE4A-56 to HIVE4-56. So basically I want to delete all single alphabets before the '-' which is always preceded by a number. Values already in the desired format should remain unchanged... (4 Replies)
Discussion started by: ames1983
4 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Replace dashes positions 351-357 & 024-043 with 0 & replace " " if exis with 04 at position 381-382

I need to replace dashes (i.e. -) if present from positions 351-357 with zero (i.e. 0), I also need to replace dash (i.e “-“) if present between position 024-043 with zero (i.e. 0) & I replace " " (i.e. 2 space characters) if present at position 381-382 with "04". Total length of record is 413.... (11 Replies)
Discussion started by: lancesunny
11 Replies

8. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

9. Shell Programming and Scripting

awk script replace positions if certain positions equal prescribed value

I am attempting to replace positions 44-46 with YYY if positions 48-50 = XXX. awk -F "" '{if (substr($0,48,3)=="XXX") $44="YYY"}1' OFS="" $filename > $tempfile But this is not working, 44-46 is still spaces in my tempfile instead of YYY. Any suggestions would be greatly appreciated. (9 Replies)
Discussion started by: halplessProblem
9 Replies

10. Shell Programming and Scripting

Replace 9-16 positions of a text file.

Hi i am having text file like this 40000201040005200213072009000000700000050744820906904421 40069300240005200713072009000000067400098543630000920442 i want to replace 9-16 positions of my txt file...by 1234567...in a single line command i.e 0400052....should be replaced by... (2 Replies)
Discussion started by: suryanarayana
2 Replies
Login or Register to Ask a Question