LinuxÒÁµéÔ°ÂÛ̳'s Archiver

hardier ·¢±íÓÚ 2006-8-4 11:19

ÇëÎÊÒ»¸ö¹ØÓÚÎļþ¸ÄÃûµÄÎÊÌâ

ij¸öĿ¼ÏÂÃæÓÐһЩÎļþ£¬ÎÒÏë°ÑijЩ¾ßÓÐÌØ¶¨¹Ø¼ü×ÖµÄÎļþ¸ÄÃû£¬ÇëÎʸÃÈçºÎ´¦Àí£¿
¾ßÌåµØËµ£¬±ÈÈçĿ¼ÏÂÓÐa.txt,_2_a.txt,_3_c.txtÎļþ,ÎÒÏë°ÑÒÔ"_"¿ªÍ·µÄÎļþÌáÈ¡³öÀ´µ¥¶À´¦Àí£ºÊ×ÏÈÏÈ´æÏÂÁ½¸ö"_"Ö®¼äµÄÊýÖµ×÷ΪÎļþ¼ÐÃû³Æ£¬ÔÙ°Ñɾ³ý"_*_"ǰ׺µÄ¸ÃÎļþ´æµ½Õâ¸öÎļþ¼ÐÏ£¬¾ÍÊÇ_2_a.txt±ä³É a.txt´æµ½/2/Îļþ¼ÐÏ£¬_3_c.txt±ä³É c.txt´æµ½/3/Îļþ¼ÐÏ¡£

ÇëÎʸÃÈçºÎ±à³Ì´¦Àí£¿Ð»Ð»£¡

hardier ·¢±íÓÚ 2006-8-4 11:26

ÊDz»ÊÇÓ¦¸ÃÊ×ÏÈ"for file in `find \-name "xx""` do"ÃüÁîÕÒµ½ËùÓеÄ_¿ªÍ·µÄÎļþ£¿µ«ÊÇÔõÃ´Ð´ÄØ£¿

dearvoid ·¢±íÓÚ 2006-8-4 11:35

i dont think its diffcult. try and then ask

hardier ·¢±íÓÚ 2006-8-4 11:55

[QUOTE=dearvoid]i dont think its diffcult. try and then ask[/QUOTE]лл¡­¡­ÎÒÊÔÁËÁ½¸öСʱ£¬¿ÉÊÇÁ¬×î¼òµ¥µÄµÚÒ»²½£¬ÕÒµ½"_"¿ªÍ·µÄÎļþ¶¼Ã»ÓÐʵÏÖ¡£¡£ÒòΪ֮ǰ²»´óÁ˽âshell±à³Ì¡£ÄܸøµãÌáʾÂð£¿//bow

hardier ·¢±íÓÚ 2006-8-4 15:48

[QUOTE=dearvoid]i dont think its diffcult. try and then ask[/QUOTE]ÎÒÊÔÁËÕâ¸ö£º
for file in *
do
case $file in
_*_* )
        
echo $file
     ;;
esac
done

¿ÉÒÔÌáÈ¡_2_a.txtÖ®ÀàµÄÎļþÁË£¬µ«ÊÇÔõô°Ñǰ׺ȥµô²¢ÇÒ´æÔÚ/2/ÏÂÄØ£¿

li-jiahuan ·¢±íÓÚ 2006-8-5 02:20

[code]
[0 No.2019 root@deb /tmp/work]# ls
_2_a.txt  _3_c.txt

[0 No.2020 root@deb /tmp/work]# for f in _*;do
dir=$(echo $f | sed 's/_\(.*\)_.*/\1/')
file=$(echo $f | sed 's/.*_//')
mkdir $dir
mv $f $dir/$file
done

`_2_a.txt' -> `2/a.txt'
`_3_c.txt' -> `3/c.txt'

[0 No.2021 root@deb /tmp/work]# ls -R
.:
2/  3/

./2:
a.txt

./3:
c.txt

[0 No.2022 root@deb /tmp/work]#                                 
[/code]

hardier ·¢±íÓÚ 2006-8-5 10:24

лл£¡ÕæµÄÊÇÓÃsed°¡¡£ÎÒÔÙÑо¿Ñо¿£¬//bow£¡

hardier ·¢±íÓÚ 2006-8-5 11:32

ÎÒÓýű¾ÕÒµ½ÁË_**_ÖмäµÄÊý×Ö£¬¿ÉÊÇmkdirµÄʱºò³ö´íÁË..ÎªÊ²Ã´ÄØ£¿³ÌÐò¾ÍÊÇÔÚÄãµÄ»ù´¡ÉÏ×÷ÁËС¸Ä¶¯¡£
for f in _*; do
echo "file" $f
dir = $(echo $f | sed 's/_\([0-9]*\)_.*/\1/')
echo "dir" $dir
file = $(echo $f | sed 's/.*|//')
mkdir $dir
#mv $f $dir/$file
done
ÔËÐнá¹ûÈçÏ£º

$./test
file _1_a.txt
dir: =:No such file or directory
dir: 1:No such file or directory
dir
=:    cannot open `='(No such file directory)
mkdir: missing operand
try `mkdir --help' for more information.




[QUOTE=li-jiahuan][code]
[0 No.2019 root@deb /tmp/work]# ls
_2_a.txt _3_c.txt

[0 No.2020 root@deb /tmp/work]# for f in _*;do
dir=$(echo $f | sed 's/_\(.*\)_.*/\1/')
file=$(echo $f | sed 's/.*_//')
mkdir $dir
mv $f $dir/$file
done

`_2_a.txt' -> `2/a.txt'
`_3_c.txt' -> `3/c.txt'

[0 No.2021 root@deb /tmp/work]# ls -R
.:
2/ 3/

./2:
a.txt

./3:
c.txt

[0 No.2022 root@deb /tmp/work]#
[/code][/QUOTE]

hardier ·¢±íÓÚ 2006-8-5 11:46

ÎÒ´íÁË¡­¡­=×óÓÒ²»ÄÜÓпոñ¡­¡­Ò»µÎº¹

li-jiahuan ·¢±íÓÚ 2006-8-5 11:55

Ò²²»ÊÇ·ÇÒªsed
there is more than one way to do it

[code]
[0 No.2002 huan@deb ~]$ str='_2_a.txt'

[0 No.2003 huan@deb ~]$ dir=${str/_/};dir=${dir%_*};echo $dir
2

[0 No.2004 huan@deb ~]$ file=${str##*_};echo $file
a.txt

[0 No.2005 huan@deb ~]$      
[/code]

[code]
[0 No.2016 huan@deb ~]$ perl -e '$_ = "_2_a.txt"; print "dir: $1\nfile:$2\n" if (/_(.*)_(.*)/) '
dir: 2
file:a.txt

[0 No.2017 huan@deb ~]$            
[/code]

Ò³: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.