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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if characters from positions 7-15 are matching 219 then replace al
# 1  
Old 10-02-2012
if characters from positions 7-15 are matching 211-219 then replace

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.
Code:
Position    Field Data Type
-----------------------------------------------------
1          Capital Letter total length = 1
2-6          Blanks
7-15         Numeric Data total length 9
16-19       Numeric Data total length 4
20-25       Year and Month.
26-31        Blanks
32           Character Data total length 1
33           Character Data total length 1
34-41        Blanks
42-43        Character Data total length 2
44-48        Blanks
49          Capital Letter total length = 1
50-89          Character total 39 length
90-129        Character total 39 length 
130          Capital Letter total length = 1
131-170          Character Data total length 39
171-199         Alphabetic Data total length 29
200-201         Character Data total length 2
202-210         Numeric Data total length 5
211-219         Numeric Data total length 9
220-290         Character Data total length 60
291-330          Character Data total length 39
331-359       Alphabetic total length 29 character 
360-361       Character Data total length 2
362-370       Numeric Data total length 5
371           Capital Letter total length = 1
372-420         Blanks

Any help on this will be appreciated. Thanks in advance.

Last edited by lancesunny; 10-03-2012 at 11:23 AM..
# 2  
Old 10-02-2012
Code:
awk -f a.awk infile > newfile

where a.awk:
Code:
{
  str1=substr($0,1,6);
  str2=substr($0,7,9);
  str3=substr($0,16,211-16);
  str4=substr($0,211,9);
  str5=substr($0,220);
  if (str2 == str4) gsub(".", " ", str4);
  print str1 str2 str3 str4 str5;
}

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 10-03-2012
Thanks rdrtx1.
I will test your solution tomorrow and let you know.
Thanks for your time and help regarding this.

---------- Post updated 10-03-12 at 10:21 AM ---------- Previous update was 10-02-12 at 10:32 PM ----------

Thanks for you help rdrtx1.
I am getting following error when i run it
$ awk -f { str1=substr($0,1,6); str2=substr($0,7,9); str3=substr($0,16,211-16); str4=substr($0,211,9); str5=substr($0,220); if (str2 == str4) gsub(".", " ", str4); print str1 str2 str3 str4 str5; } input.dat > output.dat
-bash: syntax error near unexpected token `('

---------- Post updated at 10:53 AM ---------- Previous update was at 10:21 AM ----------

Thanks you all for you help & time on this one.
Here is the code which worked fine.
Code:
awk 'substr($0,7,9)==substr($0,211,9){
$0=substr($0,1,210) sprintf("%9s",x) substr($0,220)
}1' file

ThanksSmilie
-LanceSunny
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

Check Character matching from pos 7-15 to pos 211-219 if True then replace 211-219 with spaces

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... (5 Replies)
Discussion started by: lancesunny
5 Replies

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

7. Linux

Problem in matching 2 fields with in consisten positions

hii i have a file that contains lines like this 4829:71370 1:N:0:CGATGT + chr6 126912761 GAAGGCATAGCCCGTTGGGCTGTGGTCATCAGCCTC CCCFFFFFHGHHHJHIJJJHIJIGHCGIIJJJJIJI 0 4829:71370 2:N:0:CGATGT + chr7 89349071 AGCCCTGCCCCCACCCCCCATTCTTCTTGACTGTCT C@@FFFFFHHHGHJ JIJIJIIIIJJJJJJJJIIJIJ 0 Now i... (4 Replies)
Discussion started by: anurupa777
4 Replies

8. Shell Programming and Scripting

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 ... (13 Replies)
Discussion started by: gopeezere
13 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