Abhinandh Kakarla
Abhinandh Kakarla

Reputation: 23

Extract version using shell commands

I am trying to extract version from below mentioned URL's using shell commands like, grep, awk, sed, cut which ever is most suitable

https://abcd/efgh/1.1.3/hijkl/mnop    
https://abcd/efgh/hijkl/2.3.4.5/mnop    
https://abcd/3.4/efgh/hijkl/mnop

I am looking to extract the version(numbers with dot) alone from the URL, where the position may vary as in above example. Looking for suggestions.

Expected output to be :

1.1.3
2.3.4.5
3.4

Upvotes: 2

Views: 805

Answers (3)

RavinderSingh13
RavinderSingh13

Reputation: 133770

With awk, written and tested with shown samples in GNU awk.

awk 'match($0,/([0-9]+\.){1,}[0-9]+/){print substr($0,RSTART,RLENGTH)}' Input_file

Explanation: Adding detailed explanation for above.

awk '                               ##Starting awk program from here.
match($0,/([0-9]+\.){1,}[0-9]+/){   ##using match function to match regex of ([0-9]+\.){1,}[0-9]+ in current line. 
  print substr($0,RSTART,RLENGTH)   ##Printing sub string of matched regex above, starting index is RSTART till value of RLENGTH here.
}
' Input_file                        ##Mentioning Input_file name here.

Upvotes: 1

Daweo
Daweo

Reputation: 36828

I would use GNU AWK following way, let file.txt content be

https://abcd/efgh/1.1.3/hijkl/mnop    
https://abcd/efgh/hijkl/2.3.4.5/mnop    
https://abcd/3.4/efgh/hijkl/mnop

then

awk 'BEGIN{RS="[/\n]"}/^[.[:digit:]]+$/' file.txt

output

1.1.3
2.3.4.5
3.4

Explanation: I specify row seperator (RS) as either / or newline (\n) then print only rows (i.e. parts between / or newline and / or / and newline) which contain only . or digits - in order to achieve such effect I use ^ and $ denoting start and end of record.

Upvotes: 0

anubhava
anubhava

Reputation: 786241

You may use this grep:

grep -Eo '[0-9]+(\.[0-9]+)+' file

1.1.3
2.3.4.5
3.4

Upvotes: 2

Related Questions