Jerry Cai
Jerry Cai

Reputation: 591

Obtain substring using awk

All, I want to use AWK to split the below String as first part.

How to write the AWK expression?

#!/bin/bash

function parsePackageName()
{
  #....... here I want to get the String "pp-demo-mid"
  #        from "pp-demo-mid-1.0-212.noarch"
}

Sample Inputs:

pp-demo-mid-1.0-212.noarch
pp-1.0-212.noarch
pp-xx-1.0-212.noarch

Desired Outputs:

pp-demo-mid
pp
pp-xx

that means , I need to parse the first part before version number .

Upvotes: 2

Views: 118

Answers (2)

jkshah
jkshah

Reputation: 11703

Here is an attempt using sed

sed -r 's/([^0-9]+)-.*/\1/'

([^0-9]+) will capture all non digit characters in \1

Upvotes: 2

Jotne
Jotne

Reputation: 41446

Something like this? It divides the string at the first -number.

awk -F"-[0-9]" '{print $1}' file
pp-demo-mid
pp
pp-xx

Or you can be more specific and devide after first -x.x, where x is a number

awk -F"-[0-9][.][0-9]" '{print $1}' file

Upvotes: 3

Related Questions