Need Help on For Loop to pass space separated value as one value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help on For Loop to pass space separated value as one value
# 1  
Old 07-02-2014
Need Help on For Loop to pass space separated value as one value

Hi,

I am having a file say list1 with a output like below

Code:
jun 12 18:23
may 20 18:23

Now i want to pass the above two values into for loop,I have written a script like this.

Code:
#!/bin/bash

a=`cat list1`
for i in $a
do
echo "HI $i"
done

expected output:

Code:
HI jun 12 18:23
HI may 20 18:23

But i am getting output like below,

Code:
HI jun
HI 12
HI 18:23
HI may
HI 20
HI 18:23


Last edited by Don Cragun; 07-02-2014 at 05:50 AM.. Reason: Add CODE tags.
# 2  
Old 07-02-2014
'for' will loop over items based on IFS, which is whitespace by default.

Since you actually appear to want to process the file by line rather than just by field, it's probably easiest to just use a while/read loop. Something like:
Code:
while read line
do
   echo "HI ${line}"
done < list1

This User Gave Thanks to CarloM For This Post:
# 3  
Old 07-02-2014
Thanks a lot CarolM....
# 4  
Old 07-02-2014
You can also use sed for example:
Code:
sed 's/^/HI /' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass function parameter to do loop?

Hi All, I have created one function for KSH and was running well with one parameter input since I just had to use $1 to get the parameter. Now I want to do loop for each parameter(actually filenames) . I have try to use do loop, but $i does not resolve to parameter instead it resolves to 1,... (5 Replies)
Discussion started by: mysocks
5 Replies

2. HP-UX

Unable to pass a space inside a variable shell scripting

Can anyone help me in solving this ? p=`date` e=`echo $p | awk '{print $2,$3}'` # echo $p Wed Aug 4 12:00:08 IST 2013 but when I am echoing the value of e it is giving me with one space. As shown below: # echo $e Aug 4 I need this value to be exact as found in... (6 Replies)
Discussion started by: Kits
6 Replies

3. UNIX for Dummies Questions & Answers

How convert space separated list to matched columns?

Hi I have been racking my (limited) brains to get this to work without success I have a file output which is a list of lists - ie a single column of data that is separated by space into sub lists below - I need to both split this so that each list is in a separate column (eg tab or semicolon... (8 Replies)
Discussion started by: Manchesterpaul
8 Replies

4. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

5. Shell Programming and Scripting

Attempting to pass my array as a nested parameter to loop. A little lost.

I want to pass this array as a parameter. IFS=$'\n' fortune_lines=($(fortune | fold -w 30 )) Inside of this line screen -p 0 -S ${SCREEN_SESSION} -X stuff "`printf "say ${fortune_lines}\r"`" And I am lost at this point. I am thinking something like this? Then make it loop.. ... (7 Replies)
Discussion started by: briandanielz
7 Replies

6. Shell Programming and Scripting

How to loop through space separated values?

How do I loop thru space separated values in a variable? I hate to use very complicated counter increment logic for this kind of simple problem. Expected result(using ksh) $>echo "aaa bbbb cccc" | <looping code here> var=aaa var=bbbb var=cccc $>echo "aaa bbbb cccc" | while IFS=" "... (12 Replies)
Discussion started by: kchinnam
12 Replies

7. Shell Programming and Scripting

how to pass a variable to an update sql statement inside a loop

hi all, i am experiencing an error which i think an incorrect syntax for the where clause passing a variable was given. under is my code. sqlplus -s ${USERNAME}/${PASSWORD}@${SID} << END1 >> $LOGFILE whenever sqlerror exit set serveroutput on size 1000000 declare l_rc ... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

8. Shell Programming and Scripting

Passing space separated value to a function - error

Taking inputs in the script which is space separated and passing this to a function and i have assigned like below And then when I use for loop for the inputs i got from the user, it is taking only the first argument. Enter Names : Bala Sundar Sridhar read names namesCheck $names function... (7 Replies)
Discussion started by: balamv
7 Replies

9. Shell Programming and Scripting

Problem while using grep (multi-level) with the space-separated filepath.

Hi, I've been trying to use grep to find out all the files which have two particular patterns in it (both pattern1 AND pattern2). I have a script to do the same, in which I'm getting the output of the first grep (with -l option) which contains the list of file paths and I'm trying to search for... (3 Replies)
Discussion started by: NanJ
3 Replies

10. Shell Programming and Scripting

pass a variable to sed p in a loop?

Hi, Thanks for looking,,,, (kornshell) tmp3 is a list of line numbers I want to print the lines from my list code: while read j do echo $j #works fine echo $filename #works fine #sed "'$jp'" "$filename" ... (7 Replies)
Discussion started by: JohnMario
7 Replies
Login or Register to Ask a Question