For loop variable to take value linewise, not word-wise


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop variable to take value linewise, not word-wise
# 1  
Old 11-06-2008
Lightbulb For loop variable to take value linewise, not word-wise

Hi All,

Please consider the below scenario.

Code:
/home/pranav> cat > abc.txt
abc def
xyz 123
/home/pranav>
/home/pranav> cat > test_for.ksh
#!/bin/ksh

for i in `cat abc.txt`
do
echo $i
done
/home/pranav>
/home/pranav> ksh test_for.ksh
abc
def
xyz
123
/home/pranav>

The point I am tryign to make is that, 'abc' 'def' are in the same line, the for variable is taking these values separately.

I want that the for variable, [$i in the above example] should take up values like 'abc def' and 'xyz 123' and not each wod separately.

Note: The loop should as many times as there are lines in the file [2 times aove example] and not the number of words [4 times as shown above].
# 2  
Old 11-06-2008
Code:
#!/bin/ksh

while i=$(line)
do
  echo $i
done < abc.txt


Last edited by jlliagre; 11-06-2008 at 08:10 AM..
# 3  
Old 11-06-2008
while read line
do
echo $line
done < file_name
# 4  
Old 11-06-2008
MySQL thanks mate!

it did just what I needed.. will egt back in case of any further extensions to the same.

till then.. ciao!
# 5  
Old 11-06-2008
Can be stripped down a little further:
Code:
while read
do
    echo $REPLY
done < file_name

# 6  
Old 11-20-2008
I have a problem with 'while'
I am trying to set variables by 'while' and it is fine inside, but after complettig the loop all changes are lost:
Code:
> bb="kkkk-111\nlllll-22222\nbbbb-4444"
> echo "$bb"
kkkk-111
lllll-22222
bbbb-4444
> nn=""
> echo "$bb"|while read ln; do 
  nn=$nn", "$(echo $ln|cut -d- -f2); 
  echo $nn; 
done; 
echo "otside: \n$nn"
, 111
, 111, 22222
, 111, 22222, 4444
otside:
>

After 'while' internal changes are lost!
Is it how it should be?
It seems as 'while' is processing in separated shell.
Is here any way to make it works for lokal variables?

The 'for ..' loop works different"
Code:
> nn=""
> for ln in $(ec "$bb"|nawk 'NF {print "\""$0"\"";}'); do 
  nn=$nn", "$(ec $ln|sed 's/"//'|cut -d- -f2); 
  ec $nn; 
done; 
ec "otside: \n$nn"
, 111"
, 111", 22222"
, 111", 22222", 4444"
otside:
, 111", 22222", 4444"
>

# 7  
Old 11-20-2008
Your script should just work with ksh.
I guess you are using bash which use a subshell for the wrong (IMHO) side of a pipe ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash directory loop, but only choose those folders with specific word in it

Hello, how in bash i can get directory loop, but only choose those folders with specific word in it, so it will only echo those with specific word #!/bin/bash for filename in /home/test/* do if ; then echo $filename; fithx! (4 Replies)
Discussion started by: ZerO13
4 Replies

2. Shell Programming and Scripting

For loop to break apart word

notimes=5 word=excellency the word excellency contains 10 letters. 10 letters divided by 2 = 5. which means, 5 two-groups of letters are in the word excellency. i need to perform a function on each group of letters. but the only thing i can think of is the following, which i just know... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. UNIX for Dummies Questions & Answers

Loop and variable not exactly variable: what's wrong

Hello guys, This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example): #!/bin/bash IP=`shuf -n 1 IP.txt` # I figured this would be easier to select random lines... (4 Replies)
Discussion started by: bobylapointe
4 Replies

6. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

7. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

8. Shell Programming and Scripting

Loop for row-wise averaging of multiple files using awk

Hello all, I need to compute a row-wise average of files with a single column based on the pattern of the filenames. I really appreciate any help on this. it would just be very difficult to do them manually as the rows are mounting to 100,000 lines. the filenames are as below with convention as... (2 Replies)
Discussion started by: ida1215
2 Replies

9. Shell Programming and Scripting

Extracting a word from a variable

Hi Guys, Need you quick assistance on the below, trying to extract a word from a variable i.e. acmi101acmi102acmi103acmi104 When i use the following code awk '{gsub(/cmi102/,"")};1' it leaves a space in the variable, need to get rid of the space that it leaves. Any ideas. the above... (3 Replies)
Discussion started by: eo29
3 Replies

10. Shell Programming and Scripting

Substituting a word by different word in variable

Hello Exprets, I have a requirement where i have to subtitue a word with another word. $eachline| sed 's/*$//' a) $eachline will hold a value a path for example user/oracle/Test_admin/myfolder/bakup/part_bkptemp_part_bkp_repeated/list.txt b) $eachline| sed 's/*$//' will give me the... (3 Replies)
Discussion started by: aks_1902
3 Replies
Login or Register to Ask a Question