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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace dashes positions 351-357 & 024-043 with 0 & replace " " if exis with 04 at position 381-382
# 1  
Old 10-01-2012
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.
Here is the example of the one record
B123456Aero 12345678901234-1234 abcd Abcdef 1234 Abc Rd Ste 1 Abc Treak WI12345 0000123 0-1234567890 000000000
B223456Bero 09876543214321-2345 abcd Abcdef 1234 Abc Rd Ste 1 Abc Treak WI12345 0000123 002345678901 02 000000000
B323456Cero 23456789012345-3236 abcd Abcdef 1234 Abc Rd Ste 1 Abc Treak WI12345 0000123 00-456789012 000000000
B423456Dero 32345678901234-4237 abcd Abcdef 1234 Abc Rd Ste 1 Abc Treak WI12345 0000123 004567890123 01 000000000

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

Last edited by lancesunny; 10-01-2012 at 10:51 AM..
# 2  
Old 10-01-2012
How long is the longest record.
What OS and shell are you using? (some tools are limited as to the max size of a record)
# 3  
Old 10-01-2012
The OS used is redhat linux and every record length = is 421. We are using bash shell.
# 4  
Old 10-01-2012
Code:
awk -F "" '{for(i=1;i<=NF;i++){if((i>=351 && i<=357) || (i>=24 && i <=43)){gsub("-","0",$i);} else if(i>=381 && i<=382){gsub("  ","04",$i);}}}1' OFS="" input_file


Last edited by msabhi; 10-01-2012 at 12:09 PM..
# 5  
Old 10-01-2012
Hey msabhi,
awk '{if((NR>=351 && NR<=357) || (NR>=24 && NR <=43)){gsub("-","0");} else if(NR>=381 && NR<=382){gsub(" ","04");}}1' input_file
is not working.
We need to replace character - (i.e. dash) with 0 if exist between position 351 to 357. we also need to replace character - (i.e. dash) with 0 if exist between position 24 to 43. And we also need to replace two blank characters " " with 04 if exist between position 381 to 382 in same record. Each record length is 413.
I have attached input file with this thread. If we open this file with notepad++ or any other editor we can see presence of dashes at 351 to 357 & 24 to 43. for some or all records and presence " " at 81 to 382 for some records.
Thanks for you help in advance.

Last edited by lancesunny; 10-01-2012 at 11:55 AM..
# 6  
Old 10-01-2012
Quote:
Originally Posted by msabhi
Code:
awk '{if((NR>=351 && NR<=357) || (NR>=24 && NR <=43)){gsub("-","0");} else if(NR>=381 && NR<=382){gsub("  ","04");}}1' input_file

Hi msabhi,

I think lancesunny is talking about position of the string..

so NR can be used like this...

i know this is not the efficient way..
try..Smilie
Code:
while read line
do
echo "$line" | sed -e 's/.\{1\}/&\n/g' | awk '{if((NR>=351 && NR<=357) || (NR>=24 && NR <=43)){gsub("-","0");} else if(NR>=381 && NR<=382){gsub("  ","04");}}1' | paste -sd ""
done<file

# 7  
Old 10-01-2012
I got you guys pamu and lancesunny Smilie
my vision Smilie

Lancesunny : I have updated new untested code in my previous post...Can you please check once..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace multiple "&nbsp;" entry with in <td> tag into single entry using sed?

I have the input file like this. Input file: 12.txt 1) There are one or more than one <tr> tags in same line. 2) Some tr tags may have one <td> or more tna one <td> tags within it. 3) Few <td> tags having "<td> &nbsp; </td>". Few having more than one "&nbsp;" entry in it. <tr> some td... (4 Replies)
Discussion started by: thomasraj87
4 Replies

2. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

3. Shell Programming and Scripting

Remove dashes if exist between positions 351-357 and then add - at the 357th position

I need to remove dash (i.e. -) if present from positions 351-357, and then add - (dash) at 357th position. For example in following first and 3rd record we got Before processing 1) 1st Record positions 351-357 = 0-12345 2) 3rd Record positions 351-357 = 00-4567 After processing:- 1) 1st... (7 Replies)
Discussion started by: lancesunny
7 Replies

4. Red Hat

files having Script which works behind "who" & "w" commands

Dear All, plz print the path of files which have the script of "who" & "w" commands. thnx in advance. (6 Replies)
Discussion started by: saqlain.bashir
6 Replies

5. UNIX for Dummies Questions & Answers

replace "&quot;" with "&"

I'm trying to replace the string "&quot;" with "&". I have tried this: echo "&quot;" | sed 's/&quot;/&/g' Is it impossible for sed and awk to output "&"? (1 Reply)
Discussion started by: locoroco
1 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

What does this really mean? "tty -s && stty istrip"

I am having hard time understanding what this really do to the environment? I do understand this part "tty -s && stty " but not "istrip" # stty command is executed only if a tty is attached to the process. # stty istrip : Strip input characters to 7 bits tty -s && stty istrip I am... (3 Replies)
Discussion started by: kchinnam
3 Replies

8. Shell Programming and Scripting

Append && echo "success" to all commands

I am learning to build from SVN and other tools, with a lot of copying and pasting from forums. I like to append && echo "success" to all commands so that I can see at a glance if things went all right. Is there a way that I can have the bash shell append this to all commands? Thanks! (5 Replies)
Discussion started by: dotancohen
5 Replies

9. Shell Programming and Scripting

How to replace "\&" with "&"?

I want to remove the backslash in the front of &, and want to use gsub, how to do that? echo "Free \& Long" | awk '{gsub(/\\&/, "&"); print}' This one is not working, and I think it takes the second "&" in gsub as the substitute of "\\&". Thanks a lot! (2 Replies)
Discussion started by: freelong
2 Replies

10. What is on Your Mind?

[[ $(date +%Y) == 2007 ]] && echo "Happy New Year"

Same as the Title! :) (2 Replies)
Discussion started by: ripat
2 Replies
Login or Register to Ask a Question