1753779 Members
7521 Online
108799 Solutions
New Discussion юеВ

Re: Scripting help

 
SOLVED
Go to solution
Scott G. Gallup
Advisor

Scripting help

I require some scripting assistance. I have an ascii file with rows of fixed length. The first 3 char's of each line need to be changed to fill 4 characters. This I can manage - but now I need to remove one blank character from each line. If the first 3 char's are "XXX " then I want to delete the 22nd character. If the first 3 char's are any other value than I want to delete the 9th character.
Any ideas, examples or suggestions will be most appreciated.
Thanks,
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Scripting help

Hi:

This can be an awk one-liner:

cat myfile | awk '{ if ($0 ~ /^XXX/) { i = 22} else {i = 9} print substr($0,1,i - 1)substr($0,i + 1) }' > newfile
If it ain't broke, I can fix that.
H.Merijn Brand (procura
Honored Contributor

Re: Scripting help


perl -pe 'substr ($_,m/^XXX /?21:8,1)=""' infile
Enjoy, Have FUN! H.Merijn
Mark Greene_1
Honored Contributor

Re: Scripting help

>>If the first 3 char's are "XXX " then I want to delete the 22nd character. If the first 3 char's are any other value than I want to delete the 9th character. <<

STRINGS=`echo $ROW|cut -b1-3`
if [ ${STRINGS} = "XXX" ]; then
NEW_START=`echo $ROW|cut -b1-21`
NEW_END=`echo $ROW|cut -b23-`
NEW_ROW=$NEW_START$NEW_END
else
NEW_START=`echo $ROW|cut -b1-8`
NEW_END=`echo $ROW|cut -b10-`
NEW_ROW=$NEW_START$NEW_END
fi

HTH
mark
the future will be a lot like now, only later
Mark Greene_1
Honored Contributor

Re: Scripting help

wow, I really, really, really need to make some time to learn perl. :-(

defintely go with the perl or awk one-liners!

mark
the future will be a lot like now, only later
Scott G. Gallup
Advisor

Re: Scripting help

Thanks to everyone who responded. Clay's awk one-liner worked like a charm. The perl script and strings script work also.

Thanks again