Databases
cancel
Showing results for 
Search instead for 
Did you mean: 

Look at flat file base on the CTL with cut

SOLVED
Go to solution
Nicolas Dumeige
Esteemed Contributor

Look at flat file base on the CTL with cut

Hello,

I need to extract specific column of a file.

The column are defined position-wise in a control file.

I don't want to do any calculation on the position to get the length of a field in order to use substr awk function.

I'm looking for a way to do something like :
cut -c1-5 -c16-23 -c6-15 -c163-171 $FILE

The thing is cut only accept one filed extraction at a time.

Can someone provide a mean to do this ?

Thanks

Nicolas

All different, all Unix
3 REPLIES
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Look at flat file base on the CTL with cut

Hi Nicolas,

You can seperate the fields using comma in cut?.

cut -c1-5,16-23,6-15 $FILE

Is it not you wanted?.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Nicolas Dumeige
Esteemed Contributor

Re: Look at flat file base on the CTL with cut

A RTFM case
Silly me ...
All different, all Unix
A. Clay Stephenson
Acclaimed Contributor

Re: Look at flat file base on the CTL with cut

Well there is one potential problem with the list to feed cut, all the components will be bunched together in one output field. If you want to separate into four distinct fields then you need to do something a little different.

cut -c1-5,16-23,6-15,163-171 $FILE

The above command will produce one output field and unless there are built-in spaces in your input line, it may not be useful.

Now let's suppose that you actually want 4 distinct variables loaded:

#!/usr/bin/sh

cut_em()
{
typeset S="${1}"
shift
while [[ ${#} -ge 1 ]]
do
echo "${S}" | cut "${1}" | tr -d "\n"
if [[ ${#} -gt 1 ]]
then
echo " \c"
fi
shift
done
echo
return 0
} # cut_em

cat ${FILE} | while read X
do
cut_em "${X}" -c1-5 -c16-23 -c6-15 -c163-171 | read A B C D
echo "A: ${A} B: ${B} C: ${C} D: ${D}"
done


Note that the cut_em function uses the same argument convention as your initial approach but now we have 4 distinct variables loaded.

It would be faster to do all this in awk and let it parse your argument list but you wanted cut.
If it ain't broke, I can fix that.