Feed python with read line in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Feed python with read line in shell
# 1  
Old 04-02-2019
Feed python with read line in shell

Hello,
I am running under ubuntu 14.04.
I have a python file bringing requested data. When I run, it asks me to enter query info. After that, it gives the data if anything matched can be found.

What I need to accomplish is, instead of entering the requested data from keyboard, to put all query keywords into a file and to run the python inside the shell script.
What I tested:

Code:
#!/bin/bash
while read -r line
do
python test.py
sleep 2 #enter what you read from keywords file 2seconds later
echo "$inputline" #this command is not working as a part of `python test.py` command
###sed -i "/^echo.*mpu_opps$/s/\"/$inputline\"/2 /system/etc/99oc #
done<keywords


Thank you
Boris
# 2  
Old 04-02-2019
I have no idea what you are trying to do.

You do three things every time you read a line from a file:
  1. run a python script that reads no input,
  2. sleep for two seconds, and
  3. echo the contents of a variable that has never been defined to the standard output of your script.
In a more normal sequence of operations, one might expect to see something more like:
Code:
python test.py < keywords

or, if the data in keywords needs to be massaged in some way before being passed on to python:
Code:
while read -r line
do	# do whatever needs to be done to $line before passing it on to python
	... ... ...
	# pass on the modified line to python
	printf '%s\n' "$line"
done < keywords | python test.py

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-02-2019
Thank you Don,
Works perfect. I was not hoping that pipe can pass the output of the previous previous process like this way.

Code:
while read -r line
do	# do whatever needs to be done to $line before passing it on to python
	... ... ...
	# pass on the modified line to python
	printf '%s\n' "$line"
done < keywords | python test.py

Thank you again
Boris

Last edited by baris35; 04-02-2019 at 09:38 PM..
# 4  
Old 04-02-2019
What previous process?

Did you try using:
Code:
python test.py < keywords

like I suggested in post #2? If so, did it work? If not, in what way did it fail?

If the above works and you have some other process that is creating the contents of the file named keywords, then piping the output of that command directly into python test.py is highly likely to work if that process writes the data that is stored into keywords to its standard output.

With very few exceptions, if the command sequence:
Code:
producer > temp_file
consumer < temp_file

works, then those two commands be replaced by:
Code:
producer | consumer

and get the same results. Or, if you need to keep the data that is being passed between producer and consumer, you can use:
Code:
producer | tee temp_file | consumer

and often still get faster results that running the two processes synchronously.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 04-03-2019
Thank you Don,
I am trying to gather imdb details of some movies.
Regarding, your first answer, it works for the first line of keywords file but no problem..
Regarding your next answer, python test.py > keywords also reads only first line but no problem.
I can solve it.

PS: Changed like this way:
Code:
count_raw=$(wc -l < keywords)
for (( i=1; i<=$count_raw; i++ ))
do
python test.py < keywords
sed -i '1d' keywords
done<keyword

If I do not delete active line after the process, script reads only one line and stops. That's why I used sed
Thank you so much
Boris

Last edited by baris35; 04-03-2019 at 04:24 AM..
# 6  
Old 04-03-2019
That still doesn't sound quite right, manipulating the input file while "reading" from it. Why should python test.py read just one line from a file, and the stop reading? What happens if you run it interactively and supply input from the keyboard? Does it take several keywords separated by <new line> chars, i.e. individual lines, or do you have to run it once for every single keyword you enter?
# 7  
Old 04-03-2019
Hello Rudic,
Yes, Python code works for just one entry.

python test.py
Quote:
Enter IMDB ID or Title: IrmaLaDouce <-- user-entry-in-blue
Getting information, Please Wait....
{"rating": "7.4", "genres": ["Comedy", "Romance"], "description": "Irma la Douce is a movie starring Jack Lemmon, Shirley MacLaine, and Lou Jacobi................. "id": "tt0057187"}
keywords
Code:
Irma La Douce
As Good As It Gets
Shawshank Redemption
..
..

Now, in shell, it gives like this:

Code:
./test.sh

Output:
Code:
{"rating": "7.4", "genres": ["Comedy", "Romance"], "description": "Irma la Douce is a movie starring Jack Lemmon, S$
{"rating": "7.7", "genres": ["Comedy", "Drama", "Romance"], "description": "As Good as It Gets is a movie starring $
{"rating": "9.3", "genres": "Drama", "description": "The Shawshank Redemption is a movie starring Tim Robbins, Morg$

Now it's nice with your helps.

Thank you
Boris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies

2. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

3. Shell Programming and Scripting

Want to remove a line feed depending on number of tabs in a line

Hi! I have been struggling with a large file that has stray end of line characters. I am working on a Mac (Lion). I mention this only because I have been mucking around with fixing my problem using sed, and I have learned far more than I wanted to know about Unix and Mac eol characters. I... (1 Reply)
Discussion started by: user999991
1 Replies

4. 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

5. Shell Programming and Scripting

How to skip the first line of the script in shell using python?

How to skip first line of the script in shell, using python. (3 Replies)
Discussion started by: KarthikPS
3 Replies

6. Shell Programming and Scripting

Shell script to read multiple options from file, line by line

Hi all I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis. The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
Discussion started by: haggismn
1 Replies

7. Shell Programming and Scripting

Unix, awk to read .ksh feed

I need some advice, I have live feed containing xml messages which means there is new messages every minute. I need a script that will run every 2 hours using the current time minus 2 hours ( which I able to do) However I have problem with the date formatting i.e. One date is... (3 Replies)
Discussion started by: INHF
3 Replies

8. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

9. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

10. Shell Programming and Scripting

replace last form feed with line feed

Hi I have a file with lots of line feeds and form feeds (page break). Need to replace last occurrence of form feed (created by - echo "\f" ) in the file with line feed. Please advise how can i achieve this. TIA Prvn (5 Replies)
Discussion started by: prvnrk
5 Replies
Login or Register to Ask a Question