simple for loop/cat issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simple for loop/cat issue
# 1  
Old 09-30-2009
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  
Old 09-30-2009
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  
Old 09-30-2009
Quote:
Originally Posted by foal_11
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  
Old 09-30-2009
works..!

thanks frank...worked just fine..!

vgersh99 --- will remember the tags next time..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue with cat command on a for loop

Good day to all, I'd like to ask for your advice with regards to this. Scenario : I have here a file named TEST.tmp wherein the value inside is below; "ONE|TWO|FIVE|THREE|FOUR|SIX~SEVEN~EIGHT" "NINE" But when I'm trying to use this in a simple command like; for TESTING in $(cat... (4 Replies)
Discussion started by: asdfghjkl
4 Replies

2. Shell Programming and Scripting

Script with ^M causes display issue with cat or more

I have a script to do a couple simple but repetitive commands on files that are provided to us. One of the things is to get rid of the line feeds. This is the section that is causing problems, i even cut this section into its own file to make sure nothing else was affecting it. #!/usr/bin/bash... (4 Replies)
Discussion started by: oly_r
4 Replies

3. Shell Programming and Scripting

Simple cat and echo question

Apologies, probably a really simple problem: I've got a text file (nh.txt) with this in it: user1 email1 email2 user2 email1 email2 etc With the following basic script: for tline in $(cat nh.txt) do echo "**********" echo $tline done ... (3 Replies)
Discussion started by: nelmo
3 Replies

4. Shell Programming and Scripting

for loop with whole line using cat

Hi all, I need to create loop script to read full line and append a variable to each line. cat file I need the output like below 10.0.0.1,136 1 24 048800 id N4 No_Light 10.0.0.1,137 1 25 048900 id N4 No_Light 10.0.0.1,140 1 28 048c00 id N4 No_Light 10.0.0.1,262 1 38 048e80... (13 Replies)
Discussion started by: ranjancom2000
13 Replies

5. Shell Programming and Scripting

Issue in cat command

We have shell script (in ksh) which reads the records from a csv file line by line and does some operation. We have below command to read the file (CSV has the absolute path of files stored on a server something like SER/IMP/TEST/2010/12/123465.1). P_FILES=`cat $P_BATCH_FILE` for i in $P_FILES... (2 Replies)
Discussion started by: shreevatsau
2 Replies

6. Emergency UNIX and Linux Support

cat issue

I have list of files in my current directory abc.txt 123.csv 234.csv 245.csv 145.csv 123_ex_c.sv I don't want to open first and last file. i.e (abc.txt and 123_ex_csv) I tried cat *.csv, but it won't work. Can anyone tell me the proper regex only in cat Thanks Pritish ... (2 Replies)
Discussion started by: pritish.sas
2 Replies

7. Shell Programming and Scripting

Problem with IF - CAT - GREP in simple shell script

Hi all, Here is my requirement I have to search 'ORA' word in out.log file,if it is present then i need to send that file (out.log) content to some mail id.If 'ORA' word is not in that file then i need to send 'load succesful' message to some mail id. The below the shell script is not... (5 Replies)
Discussion started by: mak_boop
5 Replies

8. Shell Programming and Scripting

cat and loop

Hi I have a simple code that I want to execute. out=out.txt for f in `cat list.txt | head -1`; do echo $f >> $out echo "sleep 5" >> $out done cat list.txt | head -1 wget -q -O - 'http://test.com:15100/cgi-bin/search cat out.txt wget sleep 5 -q sleep 5 -O (10 Replies)
Discussion started by: soemac
10 Replies

9. Shell Programming and Scripting

Issue with Unix cat command

Hi Experts, I am finding the performance of cat command is very wierd, it is taking more time to merge the files into a single file. We have a situation where we would be merging more than 100 files into a single file, but with cat command it is running slow. I tried doing with paste, join... (13 Replies)
Discussion started by: RcR
13 Replies

10. UNIX for Dummies Questions & Answers

Using cat command in a for loop

If I have a data file containing entries like-> abc abc:123 and I use a for loop: for I in `cat data-file` do echo $I done the output would contain 2 lines -> abc.... and abc:123 but I want it to be on only one line. How can I do this? thanks (1 Reply)
Discussion started by: sleepster
1 Replies
Login or Register to Ask a Question