2

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
0

3 Answers 3

3

You may use this grep:

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

1.1.3
2.3.4.5
3.4
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.