Search code examples
bashshellgrepgithub-api

Add text to start of each line after grep


I am trying to extract name of all repositories in github and build a script file to clone all of them, using this bash script:

for i in {1..10}
do
curl -u USERNAME:PASS -s https://api.github.com/user/repos?page=$i | grep -oP '"clone_url": "\K(.*)"' > output$i.txt
done

this is my script outputs each repo name in single line, but I need to insert git clone to start of each line so I wrote this (added | xargs -L1 git clone), which doesn't work:

for i in {1..10}
do
curl -u USERNAME:PASS -s https://api.github.com/user/repos?page=$i | grep -oP '"clone_url": "\K(.*)"' | xargs -L1 git clone > output$i.txt
done

Solution

  • You can append string with xargs using echo

    for i in {1..10}
    do
    curl -u use_name:pass -s https://api.github.com/user/repos?page=$i | grep -oP '"clone_url": "\K(.*)"' |  tr -d '"' | xargs -n 1 echo 'git clone'
    done
    

    Also, you can do this using Perl.

    for i in {1..10}
    do
    curl -u user_name:pass -s https://api.github.com/user/repos?page=$i | grep -oP '"clone_url": "\K(.*)"' |  tr -d '"' | perl -ne 'print "git clone $_"' > output$i.txt
    done
    

    enter image description here