user391986
user391986

Reputation: 30886

how can I parse this string out in linux?

[ec2-user@domU-11-21-89-34-70-33 bin]$ ./elastic-beanstalk-describe-applications
ApplicationName | ConfigurationTemplates | DateCreated | DateUpdated | Description | Versions
---------------------------------------------------------------------------------------------
cc |  | Mon Dec 09 00:18:03 +0000 2013 | Mon Dec 09 00:18:03 +0000 2013 | N/A | git-12301561af82aa81a15e7392e7052b6541a384f6d-1391446824430, git-0f63961a916b08fdfed3ec4c9491037029050f78-1391444770972, git-0e43769a916b08fdfed3ec4c9491037029050f78-1391444302590 ...

I need to extract "12301561af82aa81a15e7292e7052b6541a384f6d" from the first "git-12301561af82aa81a15e7392e7052b6541a384f6d-1391446824430" what would be the best way to do this?

Also the string could be quite long, the ... is a repeating pattern of different git shas.

Upvotes: 1

Views: 1000

Answers (3)

John B
John B

Reputation: 3646

You can use regex matching in BASH to find what you need.

while read line; do
    if [[ $line =~ git-([A-F|a-f|0-9]+)- ]]; then
        echo ${BASH_REMATCH[1]}
        break
    fi
done < <(./elastic-beanstalk-describe-applications)

Upvotes: 0

fedorqui
fedorqui

Reputation: 289525

grep -Po is the best way to go, as anubhava shows in his answer. However, with awk you can do this:

$ awk -F- '/git/{print $2}' file
12301561af82aa81a15e7392e7052b6541a384f6d

Which is similar to cut but checking just the line you want:

$ cut -d'-' -f2 file | tail -1
12301561af82aa81a15e7392e7052b6541a384f6d

Upvotes: 1

anubhava
anubhava

Reputation: 784998

You can pipe above command with:

grep -oP 'git-\K[A-Fa-f\d]+'

That gives this output:

12301561af82aa81a15e7392e7052b6541a384f6d
0f63961a916b08fdfed3ec4c9491037029050f78
0e43769a916b08fdfed3ec4c9491037029050f78

In case you want only first line then use:

grep -oP 'git-\K[A-Fa-f\d]+' | head -1

to get:

12301561af82aa81a15e7392e7052b6541a384f6d

Upvotes: 3

Related Questions