1829662 Members
10159 Online
109992 Solutions
New Discussion

Cut command

 
SOLVED
Go to solution
Ricardo Bassoi
Regular Advisor

Cut command


Hi,

I??m writting a script ( tra.sh ) to read an input file ( file.txt ) and creates an output ( output.txt ). The problem is that the cut command that I??m using at the VAR4 is giving me a strange value, like this:
RJ-05000-RIO and not all the contents. How to read all and set the correct value at VAR4 ?

Regards,


R.Bassoi
If you never try, never will work
6 REPLIES 6
Steven Sim Kok Leong
Honored Contributor

Re: Cut command

Hi,

The fault likes with this:

for VARS in $(cat file.txt)

This is because spacingsl be used to delimit the VARS when you cat file.txt, apart from newlines.

Do this instead:

#!/usr/bin/ksh

cat file.txt|while read a b c d e f g
do
VARS="$a $b $c $d $e $f $g"
VAR1=$(echo $VARS|cut -d";" -f1)
VAR2=$(echo $VARS|cut -d";" -f2)
VAR3=$(echo $VARS|cut -d";" -f3)
VAR4=$(echo $VARS|cut -d";" -f4)
VAR5=$(echo $VARS|cut -d";" -f5)
echo "CRECENTR,AREADG=INTRA-RJ-2,CARRIER=031,F_MOV=0,LOC_RF=$VAR4,OPERA_RF=31-
TELEMAR,PREFINT=$VAR1,PREFURB=$VAR2$VAR3,TARIF_RF=0100;" >> output.txt
done
exit 0

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: Cut command

Hi,

Also remember to add double-quotes (") for your VAR1 to VAR5 i.e.

VAR1="..."
VAR2="..."
... and so forth

Hope this helps. Regards.

Steven Sim Kok Leong
Justo Exposito
Esteemed Contributor

Re: Cut command

Hi Ricardo,

This is because "for VARS in $(cat file.txt);do" is cutting by spaces try doing a sed before in order to substitute spaces by other character like "#":
sed 's/ /#/g' file.txt > file1.txt
for VARS in $(cat file1.txt);do

An then into the for undo the changes with:
VARSok=`echo | sed 's/#/ /g'`

An then use the new variable VARSok to cut.

Regards,

Justo.
Help is a Beatiful word
Ricardo Bassoi
Regular Advisor

Re: Cut command


Tks Steve,

I forgot: Its possible to remove ( when is present ) the first 0 from the VAR1 ?

Tks,
If you never try, never will work
Steven Sim Kok Leong
Honored Contributor

Re: Cut command

Hi,

Yes, since there is only one zero and it is always at the first position:

echo $VAR1|cut -c2-

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor
Solution

Re: Cut command

Hi,

Sorry, I misread your question. Do this:

VAR1=`bc <$VAR1 + 0
EOF`

Hope this helps. Regards.

Steven Sim Kok Leong