[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: need help with bash command



On Fri, 21 Nov 2008 09:10:08 +0800
Ding Honghui <hhding.gnu@gmail.com> wrote:

> tôba wrote:
> > Hello,
> > 
> > Can you help me with a bash command?
> > I a directory, I want to extract with a command line for jpg name
> > files. I have a lot of:
> > 
> > icon_3620_0_1.jpg
> > icon_3616_0_1.jpg
> > icon_3563_0_1.jpg
> > 
> > I want to extract the number betwen icon_*_0_1.jpg AND I want to use
> > the entire filename too in the same command.
> > 
> > The goal is to create a directory called 3620 and move the
> > icon_3620_0_1.jpg file into this directory.
> > 
> > So, I did like this for test:
> > 
> > # for a in `ls | sed -e s'/_/ /g' | awk '{print $2}'`; do echo
> > icon_$a_0_1.jpg; done
> > 
> > I hope it should give me:
> > 
> > icon_3620_0_1.jpg
> > icon_3616_0_1.jpg
> > icon_3563_0_1.jpg
> > 
> > like the original
> > 
> > BUT in my surprise, it shows:
> > 
> > icon_.jpg
> > icon_.jpg
> > icon_.jpg
> > icon_.jpg
> > 
> > Why?
> > Where is the $a_0_1 gone? Surely I missed something but I cannot
> > find it.
> > Can you tell me how to reproduce the exact filename so?
> > 
> > Best regards,
> > 
> > --
> > Tôba
> > 
> > 
> 
> Hello, do you means like that?
> for a in `ls | sed -e s'/_/ /g' | awk '{print $2}'`; do echo
> icon_${a}_0_1.jpg; done
> 
> A better way maybe is:
> dhh@mud:~/test/jpg$ touch icon_3563_0_1.jpg  icon_3616_0_1.jpg
> icon_3620_0_1.jpg
> dhh@mud:~/test/jpg$ ls
> icon_3563_0_1.jpg  icon_3616_0_1.jpg  icon_3620_0_1.jpg
> dhh@mud:~/test/jpg$ ls | awk -F_ '{print $2}'
> 3563
> 3616
> 3620
> dhh@mud:~/test/jpg$

Yes, I did like you suggested and it's already done now.

Best regards,

--
Tôba
> 


Reply to: