The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Simple loop eugenes18t UNIX for Dummies Questions & Answers 5 07-09-2009 12:01 PM
Simple using For loop vincyoxy Shell Programming and Scripting 2 03-18-2009 03:40 AM
simple while loop ali560045 Shell Programming and Scripting 10 12-26-2007 10:44 AM
simple for loop ali560045 Shell Programming and Scripting 3 12-17-2007 01:39 AM
a simple while loop syno Shell Programming and Scripting 5 04-08-2006 07:45 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-30-2009
foal_11 foal_11 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 4
simple for loop/cat issue

ok..
so problem is:

I have a file that reads:
cat 123

Code:
1 and 2
3 and 4
5 and 6

I was using for loops to run through this information.

Code:


Code:
for i in `cat 123`
do
echo $i
done

shouldn't the output come as

Code:
1 and 2
3 and 4
5 and 6 ...(1)

rather, its coming as :

Code:
1
and
2
3
and
4

..... something like this...using ksh here...

lemme know what the issue is..I want to display output as (1)...no code alternatives pls..just was wondering what was wrong in this code..

thanks..!

Last edited by vgersh99; 09-30-2009 at 07:08 PM.. Reason: code tags, PLEASE!
  #2 (permalink)  
Old 09-30-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
#!/bin/ksh

while read i
do
   echo "${i}"
done < 123



---------- Post updated at 06:08 PM ---------- Previous update was at 06:07 PM ----------

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
  #3 (permalink)  
Old 09-30-2009
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
  
 

Join Date: Dec 2007
Posts: 426
Quote:
Originally Posted by foal_11 View Post
shouldn't the output come as

Code:
1 and 2
3 and 4
5 and 6 ...(1)

rather, its coming as :

Code:
1
and
2
3
and
4
no. this is because its using the default characters for $IFS(internal field separator) which is white space. set IFS='\n' prior to your loop and you should get what you expect. I would wrap your variable in double quotes though.
  #4 (permalink)  
Old 09-30-2009
foal_11 foal_11 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 4
works..!

thanks frank...worked just fine..!

vgersh99 --- will remember the tags next time..
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:50 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0