How to read the output of a command line by line and pass it as a variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read the output of a command line by line and pass it as a variable?
# 1  
Old 09-26-2017
How to read the output of a command line by line and pass it as a variable?

Hi,

I have some 2000 names in a table like below.
Code:
Java
Oracle/SQL
ANSI SQL
SQL,DWH,DB
DB&Java

And by using for loop in my code i am able to get a single word but if there is any special character or space then it is considering as a next line.

I have to execute the below queries in looping.

Code:
course=select course from table group by 1 order by 1;

Code:
create table table2 as
select * from table where course=$course;
Query3
Query4...

Above set of queries used to execute with different course each time.

I tried using while loop with no luck and unsure what exactly is missing in my code.

Thanks.
# 2  
Old 09-26-2017
""

Quote:
Originally Posted by Samah
I tried using while loop with no luck and unsure what exactly is missing in my code.
I would perhaps be able to tell you that, provided i could see it. As it is my crystal ball is currently in repair and i cannot see the code on your computer from here.

I can still tell you how to write a while-loop, though, because it should provide exactly what you want:

Code:
....
InFile="/path/to/your/inputfile"
line=""

while read line ; do
     echo "the line read: $line"
done < "$InFile"

This will read the content of the (text-)file "/path/to/your/inputfile" and output it line by line with a prefixed text of "the line read: ". The content of the line in question is in the variable "line" which you can use to base your own code on.

Notice that this will fail if the file is DOS-style formatted (that is, has CR/LF-line separators instead of the UNIX-style newline characters).

If the file contains things you want to filter out before processing it (like i.e. shell-style comments) you can use a filter to pipe into the while-loop like this (replace "<b>" and "<t>" with literal blank/tab characters, the sed-command removes leading/trailing whitespace, comments and empty lines):
Code:
....
InFile="/path/to/your/inputfile"
line=""

sed 's/#.*//;s/^[<b><t>]*//;s[<b><t>]*$//;/^$/d' "$InFile" |\
while read line ; do
     echo "the line read: $line"
done

I hope this helps.

bakunin
# 3  
Old 09-27-2017
Thanks for the quick response!

I am using netezza sql query like below, and the table contains some 2k records in it.
I want to read the result record by record and pass it in the next set of sql steps.
Code:
course=select course from table group by 1 order by 1;
N=0
for course in `nzsql -host -d db -Atc "course=select course from table group by 1 order by 1;"` ; do
   
      test[$N]="$course"
      echo "$N = $course"     #to confirm the entry
       
  let "N= $N + 1"
create table table2 as
select * from table where course=$course;
One more query here..Query2
Query3

when i use the above for loop it is reading the string line by line but if i have space or special characters between the string then it is considering as next line.
table contains the data like below.
Code:
Java
Oracle/SQL
ANSI SQL
SQL,DWH,DB
DB&Java

Above piece of code is taking the first line Java but when coming to the next line, it is splitting and taking the first word in $course.
# 4  
Old 09-27-2017
Your code sample seems to mix sql and shell code and thus doesn't make sense to me, but that may be my fault. Did you consider deploying bakunin's proposal, i.e. replacing your for loop with a while read loop? As the for construct splits arguments on spaces (although not on "/")
# 5  
Old 09-27-2017
Thanks Rudic and Bakunin for your inputs!

Actually i am able to read it from file but unable to get it from the query result.
# 6  
Old 09-27-2017
Did you consider piping the query results into a while read ... loop? Alternatively, recent shells (your version of which you fail to mention) offer "process substitution" which is another mechanism to feed a command's / process' output into the stdin of other commands.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-27-2017
Tried the below code but it couldn't worked fro me.
Code:
IFS=''
while read course
do
echo $course
done < <(course=`nzsql -host -d db -Atc "select course from table group by 1 order by 1;"`)

For now, i have created a a file from query result and reading from the file.

And i have 2k course names in the table/file, so i have to schedule it in batch wise like 100 courses at a time for the execution on my database.I never tried this before and would it be possible for you to suggest a way how to schedule this requirement as batch jobs
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Ksh: Read line parse characters into variable and remove the line if the date is older than 50 days

I have a test file with the following format, It contains the username_date when the user was locked from the database. $ cat lockedusers.txt TEST1_21062016 TEST2_02122015 TEST3_01032016 TEST4_01042016 I'm writing a ksh script and faced with this difficult scenario for my... (11 Replies)
Discussion started by: humble_learner
11 Replies

3. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

4. Shell Programming and Scripting

Passing the value of variable which is read from command line in called script

Hi, I am calling a Perl script in my shell script. When Perl script is executed it asks for a answer to be entered by user from terminal. How can i pass that value from my shell script ?? I know I can change perl script to default the answer but i dont have access to do that so only option i... (5 Replies)
Discussion started by: varun22486
5 Replies

5. Shell Programming and Scripting

read line and run a different command according to the output

Hi. I'm trying to write a script that reads a line on a file and runs a different command for a different line output. For example, if it finds the word "Kuku" on the line it sends mail to Kuku@kuku.com. Otherwise, it sends mail to Lulu@lulu.com. TIA. (2 Replies)
Discussion started by: Doojek9
2 Replies

6. Shell Programming and Scripting

How to read a file line by line and store it in a variable to execute a program ?

Hello, I am quite new in shell scripting and I would like to write a little scritp to run a program on some parameters files. all my parameters files are in the same directory, so pick them up with ls *.para >>dirafter that I have a dir file like that: param1.para param2.para etc... I... (2 Replies)
Discussion started by: shadok
2 Replies

7. Shell Programming and Scripting

Blank as variable/output when run from command line

When I run this: PDHDURL=`/usr/bin/curl --silent http://www.phdcomics.com/comics.php | /usr/bin/grep -o http://www.phdcomics.com/comics/archive/.*.gif | head -1` echo -e "$PHDURL" It is totally blank. However, when I just run it from the terminal: /usr/bin/curl --silent... (2 Replies)
Discussion started by: killer54291
2 Replies

8. Shell Programming and Scripting

Pass command line argument to variable

Hi, I had written a shell script to pass command line argument to variable in a function. Here is my code: main if ; then .$1 echo $1 get_input_file else echo "input file $1 is not available" fi get_input_file() { FILE = "$1" echo $FILE } (10 Replies)
Discussion started by: Poonamol
10 Replies

9. UNIX for Dummies Questions & Answers

Assigning the output of a command to a variable, where there may be >1 line returned?

Hello I am using unix CLI commands for the Synergy CM software. The command basically searches for a folder ID and returns the names of the projects the folder sits in. The result is assigned to a variable: FIND_USE=`ccm folder -fu -u -f "%name"-"%version" ${FOLDER_ID}` When the command... (6 Replies)
Discussion started by: Glyn_Mo
6 Replies

10. Shell Programming and Scripting

read a file as input and pass each line to another script

Hi, I am trying to write a ftp script which will read a file for filenames and ftp those files to another server. Here's my ftp script, but it's scanning the current directory for file names. My question is how can I pass multiple files (these files will have the name of data files that need to... (0 Replies)
Discussion started by: sajjad02
0 Replies
Login or Register to Ask a Question