CUT command not giving correct result inside loop

Hi,

i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues.

Src file(jjj.txt)
-------

TAB_DAY|(((date_part('year',TAB_DAY.PROCESS_DATE) * 10000) + (date_part('month', TAB_DAY.PROCESS_DATE) * 100)) + date_part('day', TAB_DAY.PROCESS_DATE))|180
TAB_WEEK|((date_part('year',TAB_WEEK.PROCESS_DATE) * 100) + date_part('week', TAB_WEEK.PROCESS_DATE))|35
TAB_MONTH|((date_part('year',TAB_MONTH.PROCESS_DATE) * 100) + date_part('month', TAB_MONTH.PROCESS_DATE))|180

Without Loop
-------------

cat $jjj.txt|cut -d"|" -f1
TAB_DAY
TAB_WEEK
TAB_MONTH
cat $jjj.txt|cut -d"|" -f2
(((date_part('year', TAB_DAY.PROCESS_DATE) * 10000) + (date_part('month', TAB_DAY.PROCESS_DATE) * 100)) + date_part('day', TAB_DAY.PROCESS_DATE))
((date_part('year', TAB_WEEK.PROCESS_DATE) * 100) + date_part('week', TAB_WEEK.PROCESS_DATE))
((date_part('year', TAB_MONTH.PROCESS_DATE) * 100) + date_part('month', TAB_MONTH.PROCESS_DATE))
cat $jjj.txt|cut -d"|" -f3
180
35
180

But the same in for loop not working ....giving biased result

file=/home/dbadmin/jjj.txt
for i in `cat /home/dbadmin/jjj.txt`
do
echo $i
s=`echo $i|tr -s ' '|cut -d"|" -f1`
q=`echo $i|tr -s ' ' |cut -d"|" -f2`
x=`echo $i |tr -s ' '|cut -d"|" -f3`
 
echo $s
echo $q
echo $x
done

not working.....any help?

Hi,

Welcome to Unix forum!

cut -d"|" -f1 file

gives following output for given input as per post #1.

What is your expected output ?

Hi,
You can not use 'for' to read a line in file, here a correct way:

file=/home/dbadmin/jjj.txt
while read i
do
echo "$i"
s=`echo "$i"|tr -s ' ' |cut -d"|" -f1`
q=`echo "$i"|tr -s ' ' |cut -d"|" -f2`
x=`echo "$i"|tr -s ' ' |cut -d"|" -f3`

echo $s
echo $q
echo $x
done < $file

Regards.

Try something like:

while IFS=\| read s q x
do
  printf "%s\n" "$s" "$q" "$x"
done < $file

--
Note: it is best to quote variable expansions to avoid interpretation by the shell:

echo "$s"
1 Like

@disedorgue:We can read line from file using For loop.
however your code is not working.The 2ns value coming wrong as its pulling data from /home/dbadmin path.

TAB_DAY|(((date_part('year', TAB_DAY.PROCESS_DATE) * 10000) + (date_part('month', TAB_DAY.PROCESS_DATE) * 100)) + date_part('day', TAB_DAY.PROCESS_DATE))|180
TAB_DAY
(((date_part('year', TAB_DAY.PROCESS_DATE) 2.sh 3.sh ALL_TABLES.txt catalog chitta cldh032.txt c.sh dead.letter Designer_goldcopy.py -h jeremy jjj.txt k.sh lost+found PART_TABLES.txt part.txt preupgrade rfunctions Schema_file.txt swmsetup VDWD VDWDv_vdwd_node0012_catalog vertica-7.1.1-12.x86_64.RHEL5.rpm 10000) + (date_part('month', TAB_DAY.PROCESS_DATE) 2.sh 3.sh ALL_TABLES.txt catalog chitta cldh032.txt c.sh dead.letter Designer_goldcopy.py -h jeremy jjj.txt k.sh lost+found PART_TABLES.txt part.txt preupgrade rfunctions Schema_file.txt swmsetup VDWD VDWDv_vdwd_node0012_catalog vertica-7.1.1-12.x86_64.RHEL5.rpm 100)) + date_part('day', TAB_DAY.PROCESS_DATE))
180

@Scrutinizer:it is working ,but can i get this in For loop.

My solution work with correction of scrutinizer echo "$s" .
Why do you want use FOR loop ?
We use a FOR loop to process a list not a line.
Otherwise, a way to process file line by line with FOR loop:

OLDIFS=$IFS
IFS='
'
file=/home/dbadmin/jjj.txt
for i in $(<$file)
do
echo "$i"
s=`echo "$i"|tr -s ' ' |cut -d"|" -f1`
q=`echo "$i"|tr -s ' ' |cut -d"|" -f2`
x=`echo "$i"|tr -s ' ' |cut -d"|" -f3`

echo "$s"
echo "$q"
echo "$x"
done
IFS=$OLDIFS

But it's A VERY VERY BAD IDEA to use this.
Regards.

1 Like

The for loop should end with a simple done .
Indeed a for loop is made to cycle through a list in memory; if the list is made from a file then the whole file must fit into memory.
In contrast, a while-read loop reads a file line by line. Most efficient if you can read each field into a distinct variable.
Quoting a lone $var in an assignment is not needed.

OLDIFS=$IFS
IFS=$OLDIFS

But quoting is a must in command arguments

if [ -n "$var" ]; then echo "$var"; fi
1 Like

Hi, why would you want to use a for loop? Just use a while loop instead; it is more suited for this application.

Thanks disedorgue.....it's really useful......got my answer