CUT command not giving correct result inside loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CUT command not giving correct result inside loop
# 1  
Old 12-03-2016
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)
-------
Code:
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
-------------
Code:
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

Code:
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?

Last edited by Scrutinizer; 12-03-2016 at 09:24 PM.. Reason: code tags
# 2  
Old 12-03-2016
Hi,

Welcome to Unix forum!

Code:
cut -d"|" -f1 file

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

What is your expected output ?

Quote:
TAB_DAY
TAB_WEEK
TAB_MONTH
# 3  
Old 12-03-2016
Hi,
You can not use 'for' to read a line in file, here a correct way:
Code:
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.
# 4  
Old 12-03-2016
Try something like:
Code:
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:
Code:
echo "$s"

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 12-04-2016
@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.

Code:
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.

Last edited by Don Cragun; 12-04-2016 at 03:24 AM.. Reason: Add CODE tags.
# 6  
Old 12-04-2016
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:
Code:
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.

Last edited by disedorgue; 12-04-2016 at 08:20 AM.. Reason: corrected error paste (suppress '<$file' in line 'done') and quoting var assignment Thanks MadeInGermany
This User Gave Thanks to disedorgue For This Post:
# 7  
Old 12-04-2016
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.
Code:
OLDIFS=$IFS
IFS=$OLDIFS

But quoting is a must in command arguments
Code:
if [ -n "$var" ]; then echo "$var"; fi


Last edited by MadeInGermany; 12-04-2016 at 07:17 AM..
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command giving different result for different users for same command

Hello, I am running below command as root user #nodetool cfstats tests | grep "Memtable switch count" Memtable switch count: 12 Where as when I try to run same command as another user it gives different result. #su -l zabbix -s /bin/bash -c "nodetool cfstats tests | grep "Memtable switch... (10 Replies)
Discussion started by: Pushpraj
10 Replies

2. Shell Programming and Scripting

Sql command inside shell script runs without giving anything back as outout

#!/bin/sh # This script returns the number of rows updated from a function echo "The execution is starting ....." sqlplus -silent $UP <<EOF set serveroutput on set echo off set pagesize 0 VAR no_rows_updated NUMBER; EXEC :no_rows_updated :=0; DECLARE CURSOR c_update is SELECT * FROM... (4 Replies)
Discussion started by: LoneRanger
4 Replies

3. AIX

Lsof command giving while loop

Hello, There is a process in AIX which is actually a oracle database user session but is running very slow When I use lsof it give below output lsof /proc/21955180 In while loop:256 In while loop:256 In while loop:256 In while loop:256 Value of I :183 np:1024 Please... (1 Reply)
Discussion started by: Vishal_dba
1 Replies

4. Shell Programming and Scripting

How to execute a command inside a while loop?

How do we execute a command inside a while loop? (7 Replies)
Discussion started by: Little
7 Replies

5. Shell Programming and Scripting

How i can put the result of a command inside a bash variable?

#!/bin/bash #... for i in `ls -c1 /usr/share/applications` do name="cat $i | grep ^Name= | cut -d = -f2" echo $name #... done Now inside name as output is present: while i want only the result of the command. Ideally i would like obtain that information using only bash ... or... (8 Replies)
Discussion started by: alexscript
8 Replies

6. Shell Programming and Scripting

Print the whole line which contains the result of the command cut

Hey everyone I have a file 'agenda' which contains: Object Day Month Year Birthday 09 02 2012 i want to extract from a script the line which contains the day the user typed. for example if he type 09 the line is showed using... (4 Replies)
Discussion started by: Goldstein
4 Replies

7. Shell Programming and Scripting

Not correct processing of “\ “ in names of dirs inside shell script (tar command - system backup scr

Hello, Recently, I've started with shell scripting, and decided to write a script for my system backup using tar. When I was dealing with tar execution inside shell script I found this, inside shell we have the following code: tar $TAR_PARAMS $ARCHIVE_FILE $EXCLUDE $BACKUP_STARTwith... (6 Replies)
Discussion started by: ilnar
6 Replies

8. Shell Programming and Scripting

if statement is not giving correct op

Hi I am using a awk command but not getting required o/p. input file a.txt 2 ak 3 cb 4 de 5 gh 6 ij awk program BEGIN { x=0 } {if ($1>3) {x=x+1}{print $0} } END { print "I found " x " line have value more than 3" } output 2 ak 3 cb 4 de 5 gh 6 ij (3 Replies)
Discussion started by: aaysa123
3 Replies

9. Shell Programming and Scripting

AWK not giving me correct output.

i have a line like this in my script IP=`get_IP <hostname> | awk '{ print $1 }' echo $IP the problem is get_IP <hostname> returns data formated as follows: ip 1.1.1.1 name server_name the code above returns 1.1.1.1 server_name and i just need the 1.1.1.1 I have tried to add "|... (5 Replies)
Discussion started by: mcdef
5 Replies

10. Shell Programming and Scripting

C Shell - Command Inside a Loop

I have a command nested in some while loops to parse some data that looks something like this. while ($condition) while ($condition) ... gzcat /dir/$fileName.gz | grep $searchString > out_file end end On the first loop, the command is executed properly (and takes maybe 10... (3 Replies)
Discussion started by: hobbers
3 Replies
Login or Register to Ask a Question