You are not logged in.

#1 2007-12-03 00:00:39

Flasher
Member
From: Bavaria / Germany
Registered: 2005-04-24
Posts: 126

How to cut Strings found by grep?

Hello,

I have a simple problem wink

I'm searching after the pkgver in the PKGBUILD file.
No problem: cat PKGBUILD | grep 'pkgver='

For example I get: 'pkgver=2.6.23.8'

But I only want the string '2.6.23.8'

I tested tail, head, basename lol  and sed (complex) but I couldn't find any solution.

I think there must be a simple solution wink

Thanks for help!

Best regards,

Flasher

Offline

#2 2007-12-03 00:08:21

MrWeatherbee
Member
Registered: 2007-08-01
Posts: 277

Re: How to cut Strings found by grep?

Flasher wrote:

I think there must be a simple solution wink

A few ways. Try this:

echo pkgver=2.6.23.8 | cut -d= -f2

Offline

#3 2007-12-03 00:09:16

nj
Member
Registered: 2007-04-06
Posts: 93

Re: How to cut Strings found by grep?

You can use cut to break it at the equals sign and then take the second field.

cat PKGBUILD | grep pkgver= | cut -d = -f 2

Offline

#4 2007-12-03 00:17:34

byte
Member
From: Düsseldorf (DE)
Registered: 2006-05-01
Posts: 2,046

Re: How to cut Strings found by grep?

grep ^pkgver= PKGBUILD | cut -d= -f2

awk -F = '/^pkgver=/ { print $2 };' PKGBUILD


1000

Offline

#5 2007-12-03 00:20:50

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: How to cut Strings found by grep?

Offtopic, but I have to point out the useless use of cat - do this instead:

grep pkgver= PKGBUILD | ...

smile

Offline

#6 2007-12-03 00:25:20

MrWeatherbee
Member
Registered: 2007-08-01
Posts: 277

Re: How to cut Strings found by grep?

tomk wrote:

Offtopic, but I have to point out the useless use of cat - do this instead:

grep pkgver= PKGBUILD | ...

smile

Here ya go:

http://sial.org/howto/shell/useless-cat/

Offline

#7 2007-12-03 03:36:11

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: How to cut Strings found by grep?

One-liner in sed (don't even need to use grep!):

$ sed -n "/pkgver=/ { s/pkgver=//; p }" PKGBUILD
      ^   ^           ^            ^ Tells sed to print the line
      ^   ^           ^ This part strips out pkgver=
      ^   ^This part does the grep for you
      ^ This part tells sed to print nothing unless told

Last edited by Cerebral (2007-12-03 03:41:13)

Offline

#8 2007-12-03 07:24:41

Bebo
Member
From: Göteborg, Sweden
Registered: 2006-06-07
Posts: 207

Re: How to cut Strings found by grep?

Another sed version:

sed -n "s/^pkgver=\(.*\)$/\1/p" PKGBUILD

Or you can do

. PKGBUILD

and then just use $pkgver directly smile

Offline

#9 2007-12-03 07:46:44

elide
Member
From: Russia
Registered: 2007-12-02
Posts: 40

Re: How to cut Strings found by grep?

Bebo wrote:

Or you can do . PKGBUILD and then just use $pkgver directly

And what about "rm -rf /" somewhere in PKGBUILD ? (;

grep -Po "(?<=^pkgver=)(\d+\.?)+" PKGBUILD

Last edited by elide (2007-12-03 07:48:44)

Offline

#10 2007-12-03 12:33:01

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: How to cut Strings found by grep?

Bebo wrote:

Another sed version:

sed -n "s/^pkgver=\(.*\)$/\1/p" PKGBUILD

Actually, you don't need to bother with storing the result:

sed -n "s/^pkgver=//p" PKGBUILD

works, and is simpler.

Offline

#11 2007-12-03 12:48:21

Bebo
Member
From: Göteborg, Sweden
Registered: 2006-06-07
Posts: 207

Re: How to cut Strings found by grep?

@Cerebral: Gah, yes, of course smile I'm usually using sed to a bit more complex patterns, so it didn't strike me that I could do it simpler smile

Offline

#12 2007-12-03 13:29:03

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: How to cut Strings found by grep?

Bebo wrote:

@Cerebral: Gah, yes, of course smile I'm usually using sed to a bit more complex patterns, so it didn't strike me that I could do it simpler smile

lol - same here.  Take a look at my first version - totally more complex than necessary. XD

Offline

Board footer

Powered by FluxBB