Search

Tech Tip's : Do's and dont's

Month

March 2016

Convert rows into space-delimited line and vice versa

While dealing with text processing jobs we would like to convert a selection of rows separated by \n and convert it to space delimited single line. 
We will do this with ‘tr’ – unix tool that translates, deletes, squeezes characters from standard input and writes the same to standard output. “Awk” is a powerful tool used to perform pattern scannind and processing language and it is one of the important commands, a powerful linux user should be aware of its usage.

 

To convert rows into a space-delimited line and save to a file:

$ cat intput.txt

ankit
shah
is
my
name
I
am
awesome

$ cat input.txt | tr “\n” ” ” | tee output.log

ankit shah is my name I am awesome

To convert spaces to line break (newline) and save to a file:

$ cat intput.txt

Hi how are you doing

sed -e ‘s/\s\+/\n/g’ input.txt | tee output.txt

Hi
how
are
you
doing

Advertisements

Find email id of any github user.

Hi all,

Recently, while browsing github, I wanted to find email id of a github user and imagined if there is a possible way to find this person as I am interested in learning more about the work on his github account and would like to contribute to the same.

Luckily I found that the github api is very insecure and presents the email id of a github user with ease.

So, here’s a script that will help you determine email id of any github user.
============================================
#The script for finding email address of any user.

function _md5()
{
  if which md5sum > /dev/null; then
    echo -n $1 | md5sum | cut -d ” ” -f1
  else
    md5 -q -s $1
  fi
}

USAGE=”$(basename “$0”) [-e] [-g] user — Find the email address of any GitHub user
Where:
    -h, –help Help: display this help message
    -e Event log: show all emails that appear in the user’s event log
    -g Gravatar match: attempt to match an event email to the user’s Gravatar ID”

if [ “$1” == “-h” ] || [ “$1” == “–help” ] || [ -z “$1” ] ; then
  echo “$USAGE”
  exit
fi

if [ “$1” == “-e” ] || [ “$1” == “-g” ] ; then
  if [ -z “$2” ] ; then
    echo “$USAGE”
    exit
  fi

  USER=$2
  EVENTRESPONSE=`curl -s https://api.github.com/users/$USER/events/public`
  EMAILS=`echo “$EVENTRESPONSE” | grep “\”email\”:” | sed -e’s/[,|”]//g’ | sort | uniq -c | sort -n | awk ‘{print $(NF)}’ | grep -v null`
 
  if [ “$1” == “-g” ] ; then
    PROFILERESPONSE=`curl -s https://api.github.com/users/$USER`
    GID=`echo “$PROFILERESPONSE” | grep “\”gravatar_id\”:” | sed -e’s/[,|”]//g’ | awk ‘{print $(NF)}’`
    for EMAIL in $EMAILS ; do
      if [ $GID == `_md5 $EMAIL` ] ; then
        echo “$EMAIL”
      fi
    done
  else
    if [ -n “$EMAILS” ] ; then
      echo “$EMAILS”
    fi
  fi
  exit
fi

USER=$1
PROFILERESPONSE=`curl -s https://api.github.com/users/$USER`
EMAIL=`echo “$PROFILERESPONSE” | grep “\”email\”:” | sed -e’s/[,|”]//g’ | awk ‘{print $(NF)}’ | grep -v null`

if [ -z “$EMAIL” ] ; then
  EVENTRESPONSE=`curl -s https://api.github.com/users/$USER/events/public`
  EMAIL=`echo “$EVENTRESPONSE” | grep “\”email\”:” | sed -e’s/[,|”]//g’ | sort | uniq -c | sort -n | awk ‘{print $(NF)}’ | grep -v null | tail -n1`
fi

if [ -n “$EMAIL” ] ; then
  echo “$EMAIL”
  exit
fi

exit 1

===============================================

Save the above script in a bash file or download it here : –  Find_github_email.sh

Usage: bash

Create a free website or blog at WordPress.com.

Up ↑