7

given an input stream with following lines:

123
456
789
098
...

I would like to call

curl -s http://foo.bar/some.php?id=xxx

with xxx being the number for each line, and everytime let an awk script fetch some information from the curl output which is written to the output stream. I am wondering if this is possible without using the awk "system()" call in following way:

cat lines | grep "^[0-9]*$" | awk '
    {
        system("curl -s " $0 \
        " | awk \'{ #parsing; print }\'")
    }'
2
  • I find this question interesting in situations where the input come from a "real" stream, not a file. For instance a sensing device that generate data (lines) periodically e.g. on /dev/ttyUSB0. In such cases, the proposed answers (reading from a file) are applicable only if storing data from the stream a temporary file and periodically process the file (what if data arrive while processing ?). But a more direct approach (using a pipe) might be better... Commented Nov 19, 2014 at 12:21
  • Note the cat | grep | awk can be simplified to just awk '/^[0-9]+$/ {}' file. Also, what kind of parsing do you want to do? Maybe it is not necessary to use awk at all and a while read loop suffices. Commented Jun 24, 2015 at 8:37

3 Answers 3

1

You can use bash and avoid awk system call:

grep "^[0-9]*$" lines | while read line; do
    curl -s "http://foo.bar/some.php?id=$line" | awk 'do your parsing ...'
done
Sign up to request clarification or add additional context in comments.

Comments

0

A shell loop would achieve a similar result, as follows:

#!/bin/bash
for f in $(cat lines|grep "^[0-9]*$"); do 
    curl -s "http://foo.bar/some.php?id=$f" | awk '{....}'
done

Alternative methods for doing similar tasks include using Perl or Python with an HTTP client.

Comments

0

If your file gets dynamically appended the id's, you can daemonize a small while loop to keep checking for more data in the file, like this:

while IFS= read -d $'\n' -r a || sleep 1; do [[ -n "$a" ]] && curl -s "http://foo.bar/some.php?id=${a}"; done < lines.txt

Otherwise if it's static, you can change the sleep 1 to break and it will read the file and quit when there is no data left, pretty useful to know how to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.