Skip to content
Advertisement

Writing multiple lines to columns instead

I have an error report that contains lots of phone numbers, I need to see if any of this data exists in our database.

Data required –

Telephone number – Routing Code – Customer ID

I’m using APIs that gather data from various sources.

The first request is to use the telephone number we already know to grab the same telephone number so I can print it in the terminal. It’s silly and a waste of resources but I wasn’t sure how to only print telephone numbers that have a valid response when querying the database.

JavaScript

Raw response

JavaScript

I add a filter to only show the telephone number.

JavaScript

Filtered Response –

JavaScript

Next, I’m getting the routing code of the number –

JavaScript

Raw Response –

JavaScript

Again I added a filter to display only the routing code –

| grep nppc | sed ‘s/^[^0-9]*//’

Filtered output –

JavaScript

Finally, I need to retrieve the customer ID –

JavaScript

Raw Response –

JavaScript

Add the filter –

JavaScript

Filtered Output –

JavaScript

So the final output looks like this –

JavaScript

At this point, I’m stuck on how to combine this data into a single line separated by commas and export it to a file. There can be 10k+ numbers to process so each result would need to be written to a new line. Example –

JavaScript

The full code –

JavaScript

Advertisement

Answer

Use command substitution: by placing the command in $(...) you run the command and capture its output in a variable that you can use as needed.

Do only one curl call and save the result in a variable:

JavaScript

Then use that variable in your greps and store the results in another variable:

JavaScript

Then echo these as you like:

JavaScript

That said, you could also look into jq to parse the JSON. sed and grep are probably very brittle.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement