1

On a linux server i have a script that does a curl and returns an output as below:

Script:

/usr/bin/curl -k -s https://example.com:18080/seriessnapshot?substringSearch=OpenFin%20Memory | cut --characters=44-51 | sort --unique | sed -e 's/iessnaps//g' -e '/^$/d'

Output:

AP711671
AP714628
AP715911
AP716960
AP717267
AP717938
AP718017
AP718024
AP721570
AP721875
AP722002
AP722622

I need to then build a URL from the output for each AP number, so for example i would need the output to return as below for each AP number:

http://apRandomNumber.com:1025/

everything but the AP is static, the only dynamic part of the URL would be the AP number.

Would it be possible to do this from the same script i use to return just AP numbers and if so how can i incorporate it into that script?

1
  • Pipe it into another sed
    – roaima
    May 11 '20 at 11:20
2

The simple and easy way is to replace your sed command with

sed -n -E 's|^AP([[:digit:]]+)$|http://ap\1.ztb.icb.commerzbank.com:1025/|p' 
  • -n suppresses the printing of lines so we have better control over which lines actually get printed at the end
  • -E enables extended regular expressions which make the rest easier
  • ^AP([[:digit:]]+)$ matches a whole line starting with AP and follewed by numbers, it puts the part between the () into \1. If you would have a more complex pattern with several () parts they would end in \2 etc.
  • the value/content of \1 is then inserted directly into the replacement
  • p at the end prints the line (so it prints only those lines where the substitution actually took place)
14
  • 1
    or put it in another -e ... instead of piping.
    – pLumo
    May 11 '20 at 12:23
  • Legend! pipe worked and i am getting the output as described in the question above. Are you able to explain to me the different sections of the sed command and what they do?
    – Nikhil
    May 11 '20 at 14:43
  • Hi thanks, that worked! it removed/ignored the iessnaps. Sorry for the trouble could you please explain how from the comment above what the extra '/^AP/s at the beginning does? if we already have ^AP(.+)$ after it.
    – Nikhil
    May 11 '20 at 16:48
  • @Nikhil The extra /AP/ isn't even necessary :-)
    – nohillside
    May 11 '20 at 18:17
  • you are correct. It was not needed. Thank you for all your help!! much appreciated
    – Nikhil
    May 12 '20 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.