TheMightyLlama
TheMightyLlama

Reputation: 1273

Regex: Everything up to the fourth space with sed

I'm developing a small script which gets information from a reflog and attempts to separate out the date and branch name.

The date parsing seems to work well up to the fourth space here with sample text but doesn't work with sed.

I've only started guessing at the attempt on the parsing of the release as I want to fix the sed issue first.

Sample input: Jul 2 15:16:41 2012 modif-for-release@{Thu Nov 27 14:24:56 2014} branch: Created from refs/remotes/origin/modif-for-release

Expected output: Jul 2 15:16:41 2012

I'm on a mac btw.

function getRelease(){
git for-each-ref --count=1000 --shell \
--format="%(refname)" 'refs/remotes/origin/*release*'|
while read entry; do
    #GET BRANCHES THEN...#

    git reflog --date="local" --pretty='%cd %gd %gs' "$y" | grep "branch:" > reflog.txt;
    string=$(cat reflog.txt);
    date=$(echo "$string" | sed -e -i '^(.*?)(.\d{4})'); #http://regexr.com/39vqa
    branch=$(echo "$string" | sed -e 's/[^ ]* //');
    echo ${date};
    echo ${branch};    
done
}

Upvotes: 0

Views: 60

Answers (3)

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

As for your sample input, you can use sed like this as well:

sed 's/ modif-for-release.*$//'

Example:

sed 's/ modif-for-release.*$//' <<< "Jul 2 15:16:41 2012 modif-for-release@{Thu Nov 27 14:24:56 2014} branch: Created from refs/remotes/origin/modif-for-release"
Jul 2 15:16:41 2012

Upvotes: 0

fedorqui
fedorqui

Reputation: 290075

For this specific case, when the number of spaces is limited, you may want to use cut:

cut -d' ' -f1-4 file

or

date=$(cut -d' ' -f1-4 <<< "$string")

this print from the field 1 to the fiel4, being them defined by the delimiter space.

Test

$ cat a
Jul 2 15:16:41 2012 modif-for-release@{Thu Nov 27 14:24:56 2014} branch: Created from refs/remotes/origin/modif-for-release
$ cut -d' ' -f1-4 a
Jul 2 15:16:41 2012

If you really want to use sed, catch the groups and print them back:

$ sed -r 's/(([^ ]+ ){3})([^ ]+).*$/\1\3/' a
Jul 2 15:16:41 2012
$ sed -r 's/([^ ]+ [^ ]+ [^ ]+ [^ ]+).*$/\1/' a
Jul 2 15:16:41 2012

Upvotes: 1

Jotne
Jotne

Reputation: 41460

This should do:

awk '{print $1,$2,$3,$4}' file

Upvotes: 0

Related Questions