Shell script inputs to python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script inputs to python
# 1  
Old 03-05-2018
Shell script inputs to python

Hi
I am trying to pass 2 input parameters from shell script to python API end point ,but not passing what i expected when print those inputs .Please advise

Code:
data.txt

 " 7554317" ,xx5e1
 " 7554317" ,xx96
 " 7554317" ,xxd6
 " 554317" ,xde
 
cat $sites/data.txt |sort |uniq >$sites/a.txt

for i in `cat $sites/a.txt`
do
#echo "test: $i"
 id=`echo $i|awk -F "," '{print $1}'`
 tid=`echo  $i|awk -F "," '{print $2}'`

echo "id : $id"
echo "tid : $tid"

python test.py /data'{" Id":"'"${id}"'""group":"get", "item":"'"${ tid}"'"],"fields":[" Id","type"," Parameters","xx"]}'

done

Getting output
id: " 7554317"
tid: 
id:
tid: xx5e1
id: " 7554317"

# 2  
Old 03-05-2018
I'm assuming you don't want the quotes around variable id but you do want to keep any leading (or internal) spaces(s).

Remove echo if python invocation is how you want.

Code:
sort $sites/data.txt | uniq | while IFS=$',\t\n' read id tid
do
    # remove quotes from around id
    id=${id#*\"}
    id=${id%\"*}

    echo "id: \"${id}\""
    echo "tid: \"${tid}\""

    echo python test.py /data'{" Id":"'"${id}"'", "group":"get", "item":"'"${tid}"'"],"fields":[" Id","type"," Parameters","xx"]}'
done

Output:
Code:
id: " 554317"
tid: "xde"
python test.py /data{" Id":" 554317""group":"get", "item":"xde"],"fields":[" Id","type"," Parameters","xx"]}
id: " 7554317"
tid: "xx5e1"
python test.py /data{" Id":" 7554317""group":"get", "item":"xx5e1"],"fields":[" Id","type"," Parameters","xx"]}


Last edited by Chubler_XL; 03-05-2018 at 06:44 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 03-05-2018
Python does not need help from the shell or another utilities to do what it is shown:

Modify test.py to handle the parsing, the sorting, the processing of uniqueness and the tokenization of the data.txt.

Here's an example using a print() for visualization.

Code:
with open('data.txt') as f:
    records = set()
    for line in f:
        id, tid = line.strip().split('" ,')
        id = id[1:]
        records.add((id,tid))

for r in sorted(records):
    id, tid = r
    print("id: {}, tid: {}".format(id, tid))

These 2 Users Gave Thanks to Aia For This Post:
# 4  
Old 03-29-2018
Hi ,
When extracting data from API,there are 2 nested groups having same group name [group:url,title and item with different values].I want to merge these 2 nested groups to 1 or individual nested groups otherwise for each unique site more than 2 records .Please advise

Code:
 {
      "time": 1522224314,
      "customParameters": [
    
        {
          "group": "url",
          "item": "https://www.xxx.a/3990154.html"
        },
        {
          "group": "title",
          "item": "a90189275"
        }
      ],
      "site": "xx877",
      "type": "test",
      "userId": "xx1"
    },

# 5  
Old 03-29-2018
Hmmm. Since the output is JSON, I advise using python JSON-Library/Functions.

Build up your data structure as needed in python and dump JSON-Format via function.

As Aia said: "Python does not need help from the shell or another utilities"

If you are able to use Python in this case, you probably won't like to use inferior Shellcode.
# 6  
Old 03-30-2018
Hi ,
I have tried with python json library,but getting the following error

Traceback (most recent call last):
File "a11.py", line 4, in <module>
data = json.load(data_file)
File "/usr/lib/python2.7/json/__init__.py", line 291, in load
**kw)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 367, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))

Code:
test.py

import json

with open('/home/test/a.json') as data_file:
    data = json.load(data_file)

for a in data['events']:
    a['customParameters'] = a['customParameters'][1]

new_some_json_string = json.dumps(data, indent=2)
 
print(new_some_json_string)

data file :a.json 
 "events": [
{
      "time": 1522224314,
      "customParameters": [

        {
          "group": "url",
          "item": "https://www.xxx.a/3990154.html"
        },
        {
          "group": "title",
          "item": "a90189275"
        }
      ],
      "site": "xx877",
      "type": "test",
      "userId": "xx1"
    }
]

expecting the output 
site      type   userId   group  item            group1  item1
xx877   test   xx1       url       https...        title      a90189275

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX Shell Script to Remove MongoDB Document-Based on Many inputs

Here I am in a position to write a Unix Shell script(.sh) to remove MongoDB collection documents. I know how to remove based on a condition like below and it works for me. eval 'db.Collection.remove({TimeStamp:{$lte: "'$var'"}}) But I need to change the remove statement based on a new parameter... (1 Reply)
Discussion started by: senthilmadhanT
1 Replies

2. Shell Programming and Scripting

Capture run time of python script executed inside shell script

I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
Discussion started by: Adfire
2 Replies

3. Programming

Regarding Python Program with Shell Script

Hi All, I have written a shell script which is using the expect method, it is working fine in terminal window, and then I have executed via python script its also working fine in command prompt functioning properly, I used subprocess.Popen method to execute the shell script file, its working... (0 Replies)
Discussion started by: janaefx
0 Replies

4. UNIX for Dummies Questions & Answers

How to send keyboard inputs toa UNIX command executed from a shell script?

I have a unix command that prompts for 'y'. How do I run this from my shell script? (4 Replies)
Discussion started by: Sree10
4 Replies

5. UNIX for Dummies Questions & Answers

How to give multiple inputs to a shell script

Got struck while trying to write a shell script which should automatically give input. While running a script for eg: (adpatch.sh) It Prompts for Multiple inputs like: Do you currently have files used for installing or upgrading the database installed in this APPL_TOP ? need to give... (2 Replies)
Discussion started by: abdmoha
2 Replies

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

7. Shell Programming and Scripting

Need a shell script which takes two inputs and copy the files from one directory to other

Hi, I am using solari 10 OS which is having bash shell. I need a shell script which takes user home directory and name of the file or directory as a input and based on that copy the files accordingly to the other directory. example:I hava a machine1 which is having some files in a... (8 Replies)
Discussion started by: muraliinfy04
8 Replies

8. Shell Programming and Scripting

Passing variable from shell script to python script

I have a shell script main.sh which inturn call the python script ofdm.py, I want to pass two variables from shell script to python script for its execution. How do i achieve this ????? Eg: main.sh a=3 b=3; c= a+b exec python ofdm.py ofdm.py d=c+a Thanks in Anticipation (4 Replies)
Discussion started by: shashi792
4 Replies

9. Shell Programming and Scripting

passing argument to shell script that reads user inputs

Hi, Lets say I have a script "ss" which does this read abc echo $abc read pqr echo $pqr Now if I want to pass and argument to only "abc" how do I do it. If I do echo "somevalue" | ss, it does not prompt for pqr and its value comes out as blank. Any help is appreciated Thanks P (6 Replies)
Discussion started by: patjones
6 Replies

10. Shell Programming and Scripting

Hi Python and shell script,the script hangs

Hi I need to run a shell script from a TCL script,the shell script in trun will run a python script 1.Tcl script set filename "./GopiRun.sh" 2.GopiRun.sh python ./psi.py $MYSB/test_scripts/delivery/gpy1.py 3.I have my gpy1.py script. Here the problem i am facing is on running... (0 Replies)
Discussion started by: nathgopi214
0 Replies
Login or Register to Ask a Question