zeehasham jafri
zeehasham jafri

Reputation: 51

How to pass multiple variables in grep

I have a json file that is download using curl. It has some information of a confluence page. I want to extract only 3 parts that downloaded information - that is page: id, status and title. I have written a bash script for this and my constraint is that I am not sure how to pass multiple variables in grep command

id=id         #hardcoded 
status=status    #hardcoded

echo Enter title you are looking for: #taking input from user here read title_name echo echo echo Here are details

curl -u username:password -sX GET "http://X.X.X.X:8090/rest/api/content?type=page&start=0&limit=200" | python -mjson.tool | grep -Eai "$title_name"|$id|$status" 

Upvotes: 0

Views: 80

Answers (1)

user1934428
user1934428

Reputation: 22217

Aside from a typo (you have an unbalanced quote - please always check the syntax for correctness before you are posting something), the basic idea of your approach would work in that

grep -Eai "$title_name|$id|$status" 

would select those text lines which contain those lines which contain the content of one of the variables title_name, id or status.

However, it is a pretty fragile solution. I don't know what can be the actual content of those variables, but for instance, if title_name were set to X.Z, it would also match lines containing the string XYZ, since the dot matches any character. Similarily, if title_name would contain, say, a lone [ or (, grep would complained about an unmatched parentheses error.

If you want to match the string literally and not be taken as regular expressions, it is better to write those pattern into a file (one pattern per line) and use

grep -F -f patternfile

for searching. Of course, since you are using bash, you can also use process substitution if you prefer not using an explicit temporary file.

Upvotes: 1

Related Questions