You are not logged in.

#1 2010-11-29 22:04:00

inp3dance
Member
Registered: 2008-06-23
Posts: 106

sed help pls

Hi,

I have a bounch of files wich are named like "something_sXX_otherthing_YYYY", where sXX can be s01,s02....s99, YYYY is 0001,0002, .... 9999.
I'd like to extract the sXX part from the filenames to sort them, can somebody help me how to do in a bash script?

like if I have wildlife_s02_lion_0003.jpg, i'd like to get the "s02" string.

I tried with sed 's/[^s0-91-9]//g', but that way it extracts the also other numbers and s characters.

Thank you!

Offline

#2 2010-11-29 22:36:08

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: sed help pls

//blue/0/~/ cat file
foo_s01_cat_0001.jpg
bar_s02_dog_0002.jpg
baz_s03_bear_0003.jpg
//blue/0/~/ sed -r 's/^.*_(s[0-9]+)_.*$/\1/' file
s01
s02
s03

i'm sure someone can golf it down further though...

Last edited by brisbin33 (2010-11-29 22:38:04)

Offline

#3 2010-11-30 00:11:40

harryNID
Member
From: P3X-1971
Registered: 2009-06-12
Posts: 117

Re: sed help pls

How exactly are you wanting to sort them? Are you wanting to rename files  or just see a visual sorted list of them?

If it's a visual then do you really need to extract "Sxx" just to sort them?

How about just letting sort do it for you?

Example:

[harry ~]$ cat file
bar_s47_dog_0024.jpg
foo_s01_snail_0001.jpg
bar_s02_dog_0002.jpg
baz_s03_dog_0003.jpg
bar_s02_cow_0001.jpg
baz_s03_bird_0001.jpg

Use sed to get rid of the underscores temporarily and make them blanks. Now we have clear fields to work with.

[harry ~]$ cat file | sed 's/_/ /g'
bar s47 dog 0024.jpg
foo s01 snail 0001.jpg
bar s02 dog 0002.jpg
baz s03 dog 0003.jpg
bar s02 cow 0001.jpg
baz s03 bird 0001.jpg

Use sort to first, sort by the first field (dictionary order), the third field (also dictionary order), and finally by the fourth field (numeric order). Then another quick sed to replace the underscores and your in business.

Full Command:

[harry ~]$ cat file | sed  s'/_/ /g' | sort -k1,1d -k3,3d -k4,4n | sed 's/ /_/g'
bar_s02_cow_0001.jpg
bar_s02_dog_0002.jpg
bar_s47_dog_0024.jpg
baz_s03_bird_0001.jpg
baz_s03_dog_0003.jpg
foo_s01_snail_0001.jpg

Also, if all you really want is the "Sxx" then how about: (Regex could be finer tuned but but gets the job done in this situation.)

[harry ~]$ grep -Eo '[Ss][[:digit:]]+' file
s47
s01
s02
s03
s02
s03

Also note you will have to modify the above commands to work with files instead of text.

ls -1  Your_Photo_Directory | sed  s'/_/ /g' | sort -k1,1d -k3,3d -k4,4n | sed 's/ /_/g'

ls -1 Your_Photo_Directory | grep -Eo '[Ss][[:digit:]]+'

etc.

In solving a problem of this sort, the grand thing is to be able to reason backward. That is a very useful accomplishment, and a very easy one, but people do not practice it much. In the everyday affairs of life it is more useful to reason forward, and so the other comes to be neglected. There are fifty who can reason synthetically for one who can reason analytically.  --Sherlock Holmes

Offline

#4 2010-11-30 01:42:05

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: sed help pls

brisbin33 wrote:
//blue/0/~/ cat file
foo_s01_cat_0001.jpg
bar_s02_dog_0002.jpg
baz_s03_bear_0003.jpg
//blue/0/~/ sed -r 's/^.*_(s[0-9]+)_.*$/\1/' file
s01
s02
s03

i'm sure someone can golf it down further though...

To golf this down, you need to use the correct tool!

awk -F'_' '{print $2}' < file

But, it gets even easier if you goal is to sort them...

sort -t'_' -k2 < file

Last edited by falconindy (2010-11-30 01:48:13)

Offline

#5 2010-11-30 11:33:09

inp3dance
Member
Registered: 2008-06-23
Posts: 106

Re: sed help pls

I want to move them to specific directories.

Also I want to thany you all for help,  I learned a lot about sed and grep big_smile

brisbin33:
sed -r 's/^.*_(s[0-9]+)_.*$/\1/'   - what the $ mean here?

Offline

#6 2010-11-30 12:42:46

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: sed help pls

^ and $ are common signs in regular expressions to mark the "beginning of line" and "end of line".

You're not giving enough information of the "question" for us to give the correct "answer". For example, can "wildlife_s02_lion_0003.jpg" be "wild_life_s02_lion_0003.jpg" or even "wild_s15732_s02_lion_0003.jpg"? Depending the question, the answer could be really simple or less simple.

It's better if you give us an relatively _thorough_ example, and rephrase what you really want to achieve.

falconindy wrote:
brisbin33 wrote:

i'm sure someone can golf it down further though...

To golf this down, you need to use the correct tool!

awk -F'_' '{print $2}' < file

To use the correct tool, you want to cut it!

cut -d_ -f2 < file

Last edited by lolilolicon (2010-11-30 12:47:18)


This silver ladybug at line 28...

Offline

#7 2010-11-30 14:29:37

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: sed help pls

lolilolicon wrote:
falconindy wrote:
brisbin33 wrote:

i'm sure someone can golf it down further though...

To golf this down, you need to use the correct tool!

To use the correct tool, you want to cut it!

cut -d_ -f2 < file

oh. snap.

Offline

Board footer

Powered by FluxBB