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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove dashes if exist between positions 351-357 and then add - at the 357th position
# 1  
Old 10-02-2012
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 Record positions 351-357 = 012345-
2) 3rd Record positions 351-357 = 004567-
Total length of record is 413.

Here is the example of the 4 records:- Before Processing
Code:
B123456Aero 12345678901234-1234 abcd Abcdef 1234 Abc Rd Ste 1 Abc Treak WI12345 0000123 0-12345 000000000
B223456Bero 09876543214321-2345 abcd Abcdef 1234 Abc Rd Ste 1 Abc Treak WI12345 0000123 0023456 000000000
B323456Cero 23456789012345-3236 abcd Abcdef 1234 Abc Rd Ste 1 Abc Treak WI12345 0000123 00-456 000000000
B423456Dero 32345678901234-4237 abcd Abcdef 1234 Abc Rd Ste 1 Abc Treak WI12345 0000123 0045678 000000000

After Processing Expected output:-
Code:
B123456Aero 12345678901234-1234 abcd Abcdef 1234 Abc Rd Ste 1 Abc Treak WI12345 0000123 012345- 000000000
B223456Bero 09876543214321-2345 abcd Abcdef 1234 Abc Rd Ste 1 Abc Treak WI12345 0000123 0023456 000000000
B323456Cero 23456789012345-3236 abcd Abcdef 1234 Abc Rd Ste 1 Abc Treak WI12345 0000123 004567- 000000000
B423456Dero 32345678901234-4237 abcd Abcdef 1234 Abc Rd Ste 1 Abc Treak WI12345 0000123 0045678  000000000

I also attached sample input file containing these records.
any help on this will be appreciated.
Thanks in advance.

Last edited by lancesunny; 10-02-2012 at 05:13 PM.. Reason: code tags
# 2  
Old 10-02-2012
Code:
 
awk '{ if ($14 ~ /-/) {gsub("-","",$14); gsub("$","-",$14) } print }' yourfile

This User Gave Thanks to scottaazz For This Post:
# 3  
Old 10-02-2012
Hi scottaazz,
thanks for you help on this.
But this solution is not working. You solution does replaces record 1 & 3 correctly but changes overall formatting & position of the record no. 1 & 3. i.e 0-1234567890 with 01234567890- and 00-456789012 with 00456789012-.
Please see the attached files source before your command execution and after your command execution.
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
Input processing
1) 1st Record positions 351-357 = 0-12345
2) 3rd Record positions 351-357 = 00-4567
Expected output processing:-
1) 1st Record positions 351-357 = 012345-
2) 3rd Record positions 351-357 = 004567-
Total length of record is 413.

Last edited by lancesunny; 10-02-2012 at 05:01 PM..
# 4  
Old 10-02-2012
Code:
awk 'BEGIN{FS="";OFS=FS;}
{x=0;for(i=351;i<=357;i++){if($i=="-"){$i="";x++;}}
if(x){$357=$357"-"};
}1' input_file

This User Gave Thanks to msabhi For This Post:
# 5  
Old 10-02-2012
Bug

Thanks a lot MSABHI,
Your solution worked fine and fixed the formatting issue.
Again thanks a lot for you help & time on this man.Smilie
-Lancesunny
# 6  
Old 10-02-2012
Quote:
Originally Posted by lancesunny
Hi scottaazz,
thanks for you help on this.
But this solution is not working. You solution does replaces record 1 & 3 correctly but changes overall formatting & position of the record no. 1 & 3. i.e 0-1234567890 with 01234567890- and 00-456789012 with 00456789012-.
Please see the attached files source before your command execution and after your command execution.
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
Input processing
1) 1st Record positions 351-357 = 0-1234567890
2) 3rd Record positions 351-357 = 00-456789012
Expected output processing:-
1) 1st Record positions 351-357 = 01234567890-
2) 3rd Record positions 351-357 = 00456789012-
Total length of record is 413.

Don't understand your output result sample...
you are telling to replace 357th position with "-".
lets consider your sample with their positions in square bracket:
[351]0-1234[357]567890
so output should be : 012345-67890
and not what you have mentioned : 01234567890-......correct me if am wrong...
# 7  
Old 10-02-2012
Thanks MSABHI You provided solution worked fine. UDAMAN.
Sorry for the typo on which i corrected now.
Input processing
1) 1st Record positions 351-357 = 0-12345
2) 3rd Record positions 351-357 = 00-4567
Expected output processing:-
1) 1st Record positions 351-357 = 012345-
2) 3rd Record positions 351-357 = 004567-
Thanks Buddy.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove line break at specific position

Hi, I need to remove line breaks from a file, but only the ones at specific position. Input file: this is ok this line is divided at posit ion 30. The same as this one, also position 30 the rest of lines are ok with different lengths The longest ones are always s plitted at same... (15 Replies)
Discussion started by: qranumo
15 Replies

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

3. Shell Programming and Scripting

Remove words from file2 that don't exist in file1

Hi I have to list of words file1 and file2, I want to compare both lists and remove from file2 all the words that don't exist in file1. How can I do this? Many thanks (4 Replies)
Discussion started by: noliveira
4 Replies

4. Shell Programming and Scripting

Code to remove files when corresponding file doesnt exist isnt working.

I am trying to add some code to the begging of a script so that it will remove all the .transcript files, when their is no coressponding .wav file. But it doesnt work. This is the code I have added: for transcriptfile in `$voicemaildir/*.transcript`; do wavfile=`echo $transcriptfile | cut -d'.'... (2 Replies)
Discussion started by: ghurty
2 Replies

5. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

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

7. Shell Programming and Scripting

Search and remove digits (if exist) from end of the string

Hi Experts, Here is what I am trying to do. 1) say I have a file with below strings database1 database2 database3 data10gdb1 data10gdb2 databasewithoutdigit 2) I want to get the below output. (- if there is any digit at the end of the string, I need to remove it) (- Any... (3 Replies)
Discussion started by: shail_boy
3 Replies

8. Shell Programming and Scripting

how can i remove comments in random positions in a file?(bash)

Suppose i have a file like this: #bla bla #bla bla bla bla bla Bla BLA BLA BLA #bla bla .... .... how can i remove all comments from every line,even if they are behind commands or strngs that are not comments? any idea how i could do that using awk? (2 Replies)
Discussion started by: bashuser2
2 Replies

9. Shell Programming and Scripting

If doc file exist remove

I need help running a script. I have the script looking into a folder and converting .doc files to .odt. The script works fine except that I want it to only run when .doc files are present. If I can do this then I can put .xls files and .ppt files in the folder and convert them when they are... (2 Replies)
Discussion started by: handband2
2 Replies
Login or Register to Ask a Question