ksh - odd output from for command...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh - odd output from for command...
# 1  
Old 08-14-2008
ksh - odd output from for command...

Help needed - I am sure there is something to do with tabs or white space that is killing me here..

I have a file that is generated from an oracle sql script - when I cat the file I see the below

601888725 14-AUG-08-10:20 3
601888726 14-AUG-08-10:37 1
601888727 14-AUG-08-10:37 1
601888728 14-AUG-08-10:49 3

What I want to do is to read in the file in a 'for' command so I can read each row/column.

When I use the following command I would expect to see 4 lines returned:
for row in `cat tempfile`; do echo $row; done

However - the output I get is:
601888725
14-AUG-08-10:20
3
601888726
14-AUG-08-10:37
1
601888727
14-AUG-08-10:37
1
601888728
14-AUG-08-10:49
3


Why is this happening and what can I do to work around it?
I want to know that id 601888725 - has date of 14-AUG-08-10:20 and group of 3 - but cannot read this is in properly....
# 2  
Old 08-14-2008
The use of cat in backticks and echo to print the lines back out is more likely to be the source of your problem. Maybe the following works more like you would hope and expect.

Code:
while read row; do
  print "$r"
done <tempfile

# 3  
Old 08-14-2008
can you use the split_for.ksh(bold text)

> cat split_for.ksh
for row in `awk '{printf "%s\n%s\n%s\n",$1,$2,$3}' split_for.txt`
do
echo "$row"
done

> cat split_for.txt
601888725 14-AUG-08-10:20 3
601888726 14-AUG-08-10:37 1
601888727 14-AUG-08-10:37 1
601888728 14-AUG-08-10:49 3

> sh split_for.ksh
601888725
14-AUG-08-10:20
3
601888726
14-AUG-08-10:37
1
601888727
14-AUG-08-10:37
1
601888728
14-AUG-08-10:49
3
# 4  
Old 08-14-2008
Dear frustrated1,

Your "problem" is that for regards line break as any other whitespace separator so every word is counted... instead try something like

Code:
while read line; do echo $line;done < tempfile

And You can take it from there, ie split $line in the parts You want them


/Lakris
Smilie

PS I have to add; of course for works just as well, if You know that the format of input is consistent, You can use arrays or just a indexed loop, since every third $row is a number, every third a date, etc.

Last edited by Lakris; 08-14-2008 at 08:41 AM..
# 5  
Old 08-14-2008
A related complication is that some variants of echo will do its own backslash interpretation and what not. That's precisely why ksh has print -- use that instead.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Odd output from sar

We're experiencing some intermittent freezes on one of our systems and I'm trying to figure out what is happening. We're running Solaris 10 zones mounting shares from netapp through nfs. On the zone that freezes we have sar running and are getting this output: SunOS prodserver 5.10... (3 Replies)
Discussion started by: Jyda
3 Replies

2. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

3. Shell Programming and Scripting

ksh : need to store the output of a awk command to a array

I have awk command : awk -F ' ' '{ print $NF }' log filename And it gives the output as below: 06:00:00 parameters: SDS (2) no no no no doc=4000000000). information: (6 Replies)
Discussion started by: ramprabhum
6 Replies

4. UNIX for Dummies Questions & Answers

ksh script - not getting output from ls

Hi! I'm a complete noob, and I'm trying to write a little script that takes a directory pathname as input from the CL, checks whether it exists, and if not creates it, then shows it worked using ls. Everything works beautifully except in the first instance, wherein the directory is created, ls... (10 Replies)
Discussion started by: sudon't
10 Replies

5. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

6. Shell Programming and Scripting

mail command behave odd

hi, The following mail cmd executed successfully. mailx -s 'subject' user@company.com < testfile.dat However When i include this mail cmd in shell script it behave odd. Getting an error message mailx comand not found. (2 Replies)
Discussion started by: zooby
2 Replies

7. UNIX for Dummies Questions & Answers

Uniq command behaving odd

My file has k s j v l k a s f l k s a d f j l a s (3 Replies)
Discussion started by: phoenix_nebula
3 Replies

8. UNIX for Advanced & Expert Users

Odd message from find command

Running the find command as: find /abc -follow -ls gives, for some files, the message, which I have never seen before: find: /abc/def/123.txt: No error Does it mean that find found the file with out error. If it found it without error then why did it not output the "ls" particulars as with... (2 Replies)
Discussion started by: twk
2 Replies

9. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

10. UNIX for Dummies Questions & Answers

odd character (^M) from .sh when chagining to .ksh

// AIX 5.3 I am trying to use .sh after changing it to .ksh Obviously, it doesn't like the file extension change. I am seeing a lot of odd characters (^M) like below: Init_Part2 ()^M^M {^M^M AWTRACE "AW SET"^M^M set | grep -e CFG_ -e OUTDIR_ENV^M^M AWTRACE "AW SET"^M^M ^M^M if ;... (2 Replies)
Discussion started by: Daniel Gate
2 Replies
Login or Register to Ask a Question