1834130 Members
3128 Online
110064 Solutions
New Discussion

Scripting

 
SOLVED
Go to solution
Arman Mahboobi
Advisor

Scripting

Hi,

I would like to reformat a file by removing all the ,*, fields from the attached txt file. Your scripting knowledge would be muchly appreciated.
7 REPLIES 7
Arman Mahboobi
Advisor

Re: Scripting

Oh, and also I would like to convert fields
221 to Q0
**I to O6 (where * wild)
***R to O4 (where * wild)
***G to P1 (where * wild)

Many thanks
James R. Ferguson
Acclaimed Contributor

Re: Scripting

Hi:

This will remove all occurances of the string comma_asterisk_comma:

# sed -e 's/,\*,//g' filename

Is that what you want?

Regards!

...JRF...
Arman Mahboobi
Advisor

Re: Scripting

Sorry, I would like to replace the ,*, fields with a pipe instead of removing it.
James R. Ferguson
Acclaimed Contributor

Re: Scripting

Hi Again:

# sed -e 's/,\*,/|/g' filename

...JRF...
Arman Mahboobi
Advisor

Re: Scripting

Hi James,
Thankyou very much for your help.
Thats getting near to what I need, all I need now is changing the following fields
22I to Q0 (field 2)
**I to O6 (field 2 where * is wild)
***R to O4 (field 6 where * is wild)
***G to P1 (filed 9 where * is wild)
James R. Ferguson
Acclaimed Contributor
Solution

Re: Scripting

Hi:

OK. We'll use 'sed' to translate the ,*, strings to a pipe as already done. At this point is appears that you want to make translations on field number 2, 6, and 9 if I understand correctly.

Hence, pipe the output of the 'sed' to an 'awk' script. Essentially, we will need to examine fields 2, 6, and 9 for anyone of the conditions you cited and substitute.

Using one of the lines in your file:

IIF ,*,22I,*,2,*, ,*,OS,*,036R,*, ,*, ,*,068G,*, ,*,

...THEN: sed -e 's/,\*,/|/g'
...produces:

IIF |22I|2| |OS|036R| | |068G| |

...which we pipe to an 'awk' script, the beginnings of which might look like:

awk -F\| 'BEGIN{OFS="|"}; $2~/..I/ {$2="xxx"};$6~/...R/ {$6="yyy"};{print $0}'

...which with the above data would yield:

IIF |xxx|2| |OS|yyy| | |068G| |

...I've used "xxx" and "yyy" for clarity of substitution. For brevity, I didn't write a complete 'awk' script, only enough to show conceptually my suggestion.

Regards!

...JRF...



Arman Mahboobi
Advisor

Re: Scripting

Man,

How can I thankyou, If you were in Australia I would buy you a beer.