You are not logged in.

#1 2006-05-18 12:27:43

pixel
Member
From: Living in the Server Room
Registered: 2005-02-21
Posts: 119

problem with cut

Let's say we got this string xfce4-clipman-plugin-0.4.1
I want to cut it to output xfce4-clipman-plugin
how i can do that?
cut -d - f1 outputs xfce4
Is there any way to tell cut to cut it at third dash?


Favorite systems: ArchLinux, OpenBSD
"Yes, I love UNIX"

Offline

#2 2006-05-18 12:33:08

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

Re: problem with cut

$ echo "xfce4-clipman-plugin-0.4.1" | cut -d '-' -f"1,2,3" 
xfce4-clipman-plugin

If you specify more than one field, it'll print out more than one.

Offline

#3 2006-05-18 13:28:42

pixel
Member
From: Living in the Server Room
Registered: 2005-02-21
Posts: 119

Re: problem with cut

Cerebral, but here we got another problem
xfce4-0.4.1 with your solution outputs xfce4-0.4.1 instead of just xfce4

I would like to output only the base name of the package regardless of the number of dashes inside it.


Favorite systems: ArchLinux, OpenBSD
"Yes, I love UNIX"

Offline

#4 2006-05-18 13:32:23

iBertus
Member
From: Greenville, NC
Registered: 2004-11-04
Posts: 2,228

Re: problem with cut

Not elagant, but it works:

pacman -Q | cut -d" " -f1 | cut -d"-" -f1

Offline

#5 2006-05-18 13:53:02

pixel
Member
From: Living in the Server Room
Registered: 2005-02-21
Posts: 119

Re: problem with cut

but is it possible to do that without using pacman, just plain old unix tools... cut, sed, paste


Favorite systems: ArchLinux, OpenBSD
"Yes, I love UNIX"

Offline

#6 2006-05-18 14:10:53

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

Re: problem with cut

ah, using sed, sure:

$ echo "xfce4-clipman-plugin-0.4.1" | sed "s/(.*)-.*/1/g"
xfce4-clipman-plugin

Offline

#7 2006-05-18 14:57:34

pixel
Member
From: Living in the Server Room
Registered: 2005-02-21
Posts: 119

Re: problem with cut

Cerebral wrote:

ah, using sed, sure:

$ echo "xfce4-clipman-plugin-0.4.1" | sed "s/(.*)-.*/1/g"
xfce4-clipman-plugin

Cerebral, thanks smile sed is great
any chance to do the same only with 'cut'?


Favorite systems: ArchLinux, OpenBSD
"Yes, I love UNIX"

Offline

#8 2006-05-18 16:05:04

stonecrest
Member
From: Boulder
Registered: 2005-01-22
Posts: 1,190

Re: problem with cut

Pretty sure it's not possible just with cut.


I am a gated community.

Offline

#9 2006-05-18 16:45:43

iBertus
Member
From: Greenville, NC
Registered: 2004-11-04
Posts: 2,228

Re: problem with cut

pixel wrote:

but is it possible to do that without using pacman, just plain old unix tools... cut, sed, paste

You don't need pacman, it was just an example to make sure it works.

You could always:

echo "xfce4-some-package 0.4.1" | cut -d" "  -f1 | cut -d"-" -f1

Offline

#10 2006-05-18 16:57:53

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: problem with cut

stonecrest wrote:

Pretty sure it's not possible just with cut.

Sure it is, check out what iBertus posted:

echo "xfce4-some-package 0.4.1" | cut -d" "  -f1 | cut -d"-" -f1 

you can also:

echo "xfce4-some-package 0.4.1" | tr ' ' '-' | cut -d'-' -f1 

Offline

#11 2006-05-18 17:16:23

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

Re: problem with cut

phrakture wrote:

you can also:

echo "xfce4-some-package 0.4.1" | tr ' ' '-' | cut -d'-' -f1 

That does _not_ work:

$ echo "xfce4-some-package 0.4.1" | tr ' ' '-' | cut -d'-' -f1
xfce4

It only prints the first field.

Offline

#12 2006-05-18 17:33:28

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: problem with cut

Ahhh, i thought he wanted just the first word...

I'd use sed... though, you may want to take $pkgrel into account:

echo "xfce4-clipman-plugin-0.4.1-1" | sed "s|(.*)-.*-.*|1|g"

Offline

#13 2006-05-19 12:18:32

ibrahim
Member
Registered: 2006-02-18
Posts: 53

Re: problem with cut

pixel wrote:

Let's say we got this string xfce4-clipman-plugin-0.4.1
I want to cut it to output xfce4-clipman-plugin
how i can do that?
cut -d - f1 outputs xfce4
Is there any way to tell cut to cut it at third dash?

echo foo-ooo-bar-345 | cut -d '-' -f 1-3

will give you

foo-ooo-bar

Is that what you want?

Offline

#14 2006-05-19 19:00:21

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: problem with cut

ibrahim wrote:
pixel wrote:

Let's say we got this string xfce4-clipman-plugin-0.4.1
I want to cut it to output xfce4-clipman-plugin
how i can do that?
cut -d - f1 outputs xfce4
Is there any way to tell cut to cut it at third dash?

echo foo-ooo-bar-345 | cut -d '-' -f 1-3

will give you

foo-ooo-bar

Is that what you want?

That wont work for all packages. You are splitting at the `-' and printing only the first to third instances of it and not all package names will have three hyphens.
Phrakture's solution is the best. Its saying take the whole field, not anything and anything with -* and -* (pkgver, pkgrel) and print the first field which was the 'not anything' field, or pkgname.

Offline

#15 2006-07-09 00:53:18

bogomipz
Member
From: Oslo, Norway
Registered: 2003-11-23
Posts: 169

Re: problem with cut

It always irritated me that you cannot tell cut to count from the end; "give me the second last field" or "give me all fields up to the third last". It should have had that feature.


All of your mips are belong to us!!

Offline

#16 2006-07-09 01:07:53

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: problem with cut

bogomipz wrote:

It always irritated me that you cannot tell cut to count from the end; "give me the second last field" or "give me all fields up to the third last". It should have had that feature.

python -c "print 'one two three four'.split(' ')[-2]"
python -c "print 'one two three four'.split(' ')[:-3]"

tongue

Offline

#17 2006-07-09 01:36:43

bogomipz
Member
From: Oslo, Norway
Registered: 2003-11-23
Posts: 169

Re: problem with cut

Yes, I'm aware that scripting languages usually have that kind of indexing/slicing - cut most definitely should have had it - this is all it does dammit, why doesn't it do it properly?


All of your mips are belong to us!!

Offline

#18 2006-07-10 17:06:27

SleepyDog
Member
Registered: 2004-10-15
Posts: 114

Re: problem with cut

echo 'one two three four' | awk '{print $(NF-2),$(NF-1),$NF}'

Offline

#19 2006-07-10 20:00:06

test1000
Member
Registered: 2005-04-03
Posts: 834

Re: problem with cut

what? this is a contest or something big_smile


KISS = "It can scarcely be denied that the supreme goal of all theory is to make the irreducible basic elements as simple and as few as possible without having to surrender the adequate representation of a single datum of experience." - Albert Einstein

Offline

Board footer

Powered by FluxBB