Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File substitution using awk 1

Status
Not open for further replies.

slimmer

Programmer
Oct 21, 2002
11
US
We are processing a file using the gsub function to do a substitution of a column, removing special characters and replacing them with a null ("") value. I need to modify this script to also replace a space found in the 2nd column with the null value.

The problem: if the column contains all spaces, I want to leave it as is. The change should only be applied to the column if the data is not completely spaces but contains other values i.e. INT C should be changed to INTC.

I hoped to use the existing awk command and add the check for space and replace the values with null; write that file to a temp file and feed it into another gsub command that checks for nulls in the 2nd column and make it spaces. I have not worked with awk much and am not sure if this can be done. Do I have to create a file from the first iteration and then feed it into a separate awk program or can it be done in one step?

Below is the command currently using:

gsub(/[\/\*\.]/, "", $2); print $0

If I add: gsub(/[\/\*\.\ ]/, "", $2) ====> can I print to a file and then feed the file into another gsub command?

Any help is appreciated.
 
You don't say, but I assume the fields are delimited by something other than a space. If so, the following (untested) script may do what you want

gsub(/[\/\*\.]/, "", $2);if ($2 !~ /^ *$/) gsub (/ */,"",$2);print

Otherwise, post a sample of your input data. CaKiwi
 
Thank you! This worked great.

If possible, will you please explain what the if syntax is saying?

If the second column is not (unsure at this point) then execute the gsub command.

Thank you again.
 
The ~ operator compares a variable against a regular expression. The !~ returns true if the variable does not match the regular expression. The expananation for regular expression /^ *$/ is

^ expression must start at the beginning of the string
<space> match a space
* match any number of the preceeding character (space)
$ expression must end at the end of the string.

Hope this helps. CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top