Operating System - HP-UX
1826268 Members
3679 Online
109692 Solutions
New Discussion

Re: Replacing special characters by spaces

 
Subha Krishnaswami
New Member

Replacing special characters by spaces

I need to do the following with the help of a Korn Shell script:
--My input file has multiple fields containing Cutomer Detail entity information
--I need to examine Customer Name attribute using the following logic:
if the first 3 characters of the CUST_NM field are blanks or underscores, THEN ???IS_VALID_CUST_NAME_F??? attribute is set to 0.
if the first character of the CUST_NM field is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, THEN ???IS_VALID_CUST_NAME_F??? attribute is set to 0.
if the first character of the CUST_NM field is asterisk, THEN ???IS_VALID_CUST_NAME_F??? is set to 0.
IF ???IS_VALID_CUST_NAME_F??? is non-zero, convert any of the following special characters in the CUST_NM field to a space: ???{???, ???}???, ??????, ???$???, ???@???, ???=???, ???,???(comma), ???.???(period), ???`???, ????????? (double quote), ???/???, ???\???, ???|???, ???&???, ???)???, ???#???, ???(???, ???*???, ???!???, ???~???, ???^???, ??? ???, ????????? (single quote).
--Modified Customer Name attribute and IS_VALID_CUST_NAME_F attribute along with other Customer Detail attributes must be written to an output file.

I know I can use awk/sed/tr to accomplish what I need. I am not that much familiar on how to use them. Any help in this regard will be appreciated.

Thanks in Advance

3 REPLIES 3
Stuart Abramson_2
Honored Contributor

Re: Replacing special characters by spaces

This isn't quite what you want, but it's all I have time for!:

Run this:

# awk -f cust.awk cust.data | sed 's/~/ /'

Where:

cust.awk is:

/^[0-9*]/ { Valid = 0; print $0, Valid }
/^ / { Valid = 0; print $0, Valid }
/^___/ { Valid = 0; print $0, Valid }
/^[A-z]/ { Valid = 1; print $0, Valid }

and, cust.data is:

Customer1 Data1
_Customer2 Data2
3Customer3 Data3
4Custom{r4 Data3
*CustomVcustomer6 data6
Acustom~r7 Data7

The output is:

# awk -f cust.awk cust.data | sed 's/~/ /'

Customer1 Data1 0
_Customer2 Data2 1
3Customer3 Data3 0
4Custom{r4 Data3 0
*CustomVcustomer6 data6 1
Acustom r7 Data7 1


Wouter Jagers
Honored Contributor

Re: Replacing special characters by spaces

In order to help you efficiently, more info is needed.

- What is the delimiter in your datafile ?
- What is the field number of CUST_NM ?
- How many fields are there in total ?

Even better would be a sample record, of course.

If you can get the CUST_NM field to be the first, things will be easier. Otherwise you might have to do a bit of counting..

Let us know
an engineer's aim in a discussion is not to persuade, but to clarify.
Subha Krishnaswami
New Member

Re: Replacing special characters by spaces

Thanks for your responses. I was able to complete the coding with the help of my co-worker.

I am posting the solution if anyone is interested.

AWK script:
BEGIN { FS = "\t"; OFS = "\t" }
{
if ( $21 ~ /^ / || $21 ~ /^___/ || $21 ~ /^[0-9]/ || $21 ~ /^\*/ )
{
$21 = $21
$73 = 0
}
else
for ( i = 1; i <= length($21); i++ ) {
if ( substr($21,i,1) ~ /[{}<>$@=,."\/\\&)(#*!~']/ ) {
$21 = substr($21,1,i - 1) " " substr($21,i + 1)
}
}
print $0
}

I called the awk script from a korn shell script using the follwoing syntax:
awk -f "name of awk script" input-file >output-file.

BTW, input file is tab delimited, "Customer name" attribute is field number 21 and "IS_VALID_CUST_NAME_F' is filed number 73.

Thanks again for the feedback.