0

I want to automatically parse a text file with a content like this:

car 12345 W
train 54321 D

To be integrated in a new bash file. The content after that should look like that:

curl http://example.com/?vehicle=car&number=12345&period=W
curl http://example.com/?vehicle=train&number=54321&period=D

My problem is that I really don't now how to realize that or which program to use, sed, awk, etc..

What do I have to do?

1
  • 2
    bash, awk, sed... all fine.. what's the problem. please show some code. Commented Sep 14, 2013 at 19:35

3 Answers 3

5

Here's probably the funniest answer: assume your file is file.txt, then just do this:

printf 'curl http://example.com/?vehicle=%s&number=%s&period=%s\n' $(<file.txt)

It:

  • is 100% pure bash,
  • has no explicit loops
  • is the shortest answer
  • is very funny

Hope this helps :)

Sign up to request clarification or add additional context in comments.

1 Comment

This will fail for large files, because it will exceed the maximum number of arguments.
4

Try this:

while read vehicle number period ; do
    echo curl http://example.com/\?vehicle="$vehicle"\&number="$number"\&period="$period"
done < input.txt

1 Comment

+1 It would actually look better if you place everything around doublequotes and just use ${var} formats.
2

Maybe use sed to interleave the URL arguments with other parts. Guessing from the sample you posted:

sed 's/\([a-z]*\) \([0-9]*\) \([A-Z]\)/curl http:\/\/example.com\/?vehicle=\1\&number=\2\&period=\3/'

1 Comment

@ceving If you say so, I believe you even without a proof. My comment is then directed towards whomever did.

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.