中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net  论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

游客:  注册 | 登录 | 命令行 | 会员 | 搜索 | 上传 | 帮助 »
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » stream editor (sed) for dos, win32,*nix
« [1] [2] »
作者:
标题: stream editor (sed) for dos, win32,*nix 上一主题 | 下一主题
twf_cc
初级用户




积分 262
发帖 50
注册 2004-7-10
状态 离线
『第 16 楼』:  

我不會寫 batch ,給你一個用 bash script 的方法 , 2.0X above
可用 cygwin 執行, Not test,
1) use sed
#!/bin/bash
for oldname in * ; do
newname=$(echo $i | sed '/.//')
mv $oldname $newname
done
2)  no sed , using bash
#!/bin/bash
for oldname in * ; do
newname=${oldname:1}
mv $oldname $newname
done
3)  give argument on command line , using cut,  usage:  $0  *.txt
#!/bin/bash
file=$1
while [ -n "$file" ] ; do
newname=$(echo $file |cut -b2- )
mv $file $newname
shift
done







2004-8-3 00:00
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
twf_cc
初级用户




积分 262
发帖 50
注册 2004-7-10
状态 离线
『第 17 楼』:  

typo ,
for oldname in * ; do
newname=$(echo $i | sed '/.//')
mv $oldname $newname
done
should be

for oldname in * ; do
newname=$(echo $i | sed 's/.//')
mv $oldname $newname
done



2004-8-3 00:00
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
twf_cc
初级用户




积分 262
发帖 50
注册 2004-7-10
状态 离线
『第 18 楼』:  

many many typo

for oldname in * ; do
newname=$(echo $i | sed 's/.//')
mv $oldname $newname
done

should be

for oldname in * ; do
newname=$(echo $oldname | sed 's/.//')
mv $oldname $newname
done



2004-8-3 00:00
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
Climbing
铂金会员

网络独行侠


积分 6962
发帖 2753
注册 2003-4-16
来自 河北保定
状态 离线
『第 19 楼』:  

我们现在不是在讨论使用Unix来解决问题,我只是在讨论使用sed来解决这个问题,我给出你条件,你不用关心如何使用batch来实现。

命题如下:
1、假设当前目录有一个文件名为 Aneed.ren,当然,在实际情况中会有很多文件。
2、使用dir /b命令,该命令会输出:Aneed.ren。
3、要求使用dir /b | sed "???" 命令输出这样一串字符串:ren "Aneed.ren" "need.ren"

我已经试了一天了,就是找不到怎么把两个字符串连接起来的命令。
我用:dir /b | sed "s/^/ren \"/g;s/$/\"/g"
输出:ren "Aneed.ren"
我用:dir /b | sed "s/^./\"/g;s/$/\"/g"
输出:"need.ren"

但我怎么将两个命令的输出结果连成一行呢?最理想的时候我使用命令:
dir /b | sed -n "s/^/ren \"/g;s/$/\"/gp;s/ren \"./\"/gp"
输出:
ren "Aneed.ren"
"need.ren"

注意,输出结果变为了两行,我知道变成两行的原因就出在第一个p上,p打印时应该是带换行符的,我怎么做才能在一个命令中将两个输出变为一行呢?请指教!

[ Last edited by willsort on 2005-10-14 at 16:06 ]



偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
2004-8-3 00:00
查看资料  发送邮件  访问主页  发短消息 网志  OICQ (653668)  编辑帖子  回复  引用回复
Climbing
铂金会员

网络独行侠


积分 6962
发帖 2753
注册 2003-4-16
来自 河北保定
状态 离线
『第 20 楼』:  

连蒙带骗再晕着,我终于使用sed实现了上述要求,但还是需要一个sed的script文件来配合,这个script文件我命名为ren.sed,内容如下:
:join
/ren/{N
s/\"\n/\" /
}
然后最终用到的命令行是:dir /b | sed -n "s/^/ren \"/g;s/$/\"/gp;s/ren \"./\"/gp" | sed -f ren.sed进一步改善,为了防止文件基本名只有一个字符的文件被改名,进一步修改ren.sed如下:
:join
# 下面一行验证文件基本名是否只有一个字符,将不符合条件的剔出去
# 但这里我却找不到用什么字符在\(?\)中表示任意一个字符的元字符,“.”在这里是无效的
# 所以只好用一个笨办法用[a-zzA-Z0-9_%!@#$-]来表示,但&字符却不能用
/ren \"\([a-zA-Z0-9_%!@#$-]\)\{2,\}[\.\"]/{
N
s/\"\n/\"/
p
}
DOS下的命令行又变为:dir /b | sed -n "s/^/ren \"/g;s/$/\"/gp;s/ren \"./\"/gp" | sed -n -f ren.sed



偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
2004-8-3 00:00
查看资料  发送邮件  访问主页  发短消息 网志  OICQ (653668)  编辑帖子  回复  引用回复
Climbing
铂金会员

网络独行侠


积分 6962
发帖 2753
注册 2003-4-16
来自 河北保定
状态 离线
『第 21 楼』:  

要最终完成前面所要求的将一个目录下所有文件改名时去掉第一个字符的命令应该是:
dir /b | sed -n "s/^/ren \"/g;s/$/\"/gp;s/ren \"./\"/gp" | sed -n -f ren.sed > %tmp%\temp.bat
call %tmp%\temp.bat
del %tmp%\temp.bat这个命令不知道要比用lmod复杂多少倍,所以说在DOS下做什么跟你所选用的工具有很大的关系,我不是说sed不好,但至少在这个特定的要求下它要比lmod差远了,当然,可能高手有更高明的实现办法,我拭目以待。在这里感谢twf_cc朋友的不断鼓励和耐心解释,他鼓励我彻底的学习了一下正则表达式在搜索和替换中的应用。



偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
2004-8-3 00:00
查看资料  发送邮件  访问主页  发短消息 网志  OICQ (653668)  编辑帖子  回复  引用回复
twf_cc
初级用户




积分 262
发帖 50
注册 2004-7-10
状态 离线
『第 22 楼』:  

dir /b | sed -n "s/^/ren \"/g;s/$/\"/gp;s/ren \"./\"/gp"
sorry ,I cannot understand  this
I ran this command the sed complain
dir /b  | sed "s/.//" ===>  geen.ren
but I dont know how to combine it to rename in batch
ok ,strip newline in a text  with sed
say a file contain some content like this
a
b
c
d
I want to make it `abcd'
I would like to use
sed -e ":a" -e "N;s/\n//;ta"  file
I run this on cygwin and cmd.exe no problem, it is a lebel :a to a sed command N ,Next line ,  replace
\n ==> newline ; go back to command until newline disapper. It is a loop
sed got some pattern space , hold space,but I cant explain clear with english or chinese
I learn it base on a book sed & awk 2 years ago, the book show it with picture , I got a long
time to understand it , some command like x ,I never use
using sed in cmd.exe to I am not very expericenced,sorry
may be dir /b | sed -n "s/^/ren \"/g;s/$/\"/gp;s/ren \"./\"/gp" |  sed -e ":a" -e "N;s/\n//;ta"
just guess

usually I run Cygwin with all XP command
if batch can make something like  bash
put the command result to the varilable ,     
set something  = " we run the command in here and the result is for something"
using for loop , I know batch have
for %%i in (*.txt) do
set something  = " we run the command in here and the result is for something"
set something = ``dir /b | sed "s/.//" ''
ren %i %something

that is the easy  way  I think  



2004-8-3 00:00
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
twf_cc
初级用户




积分 262
发帖 50
注册 2004-7-10
状态 离线
『第 23 楼』:  

every tools got their use,  sed is very power with scripting , of course,
it may not be use in everywere , It just a tool for us to solve problem .
some PS for my bash script
It may change

刚才测试一下我随意写的 bash 脚本,
1,2 没有问题,运行後脚本本身亦改了名 ,3 出现问题, while
loop 改副档较好,所以 重新post , 哈哈,都是 not test 问题, so

#!/bin/bash
#usage :$0 file(s)
for oldname in $@
do
newname=$(echo $oldname | cut -b2-)
mv $oldname $newname
done







2004-8-3 00:00
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
wchong
银牌会员




积分 1186
发帖 510
注册 2004-7-30
状态 离线
『第 24 楼』:  

SORRY!我不太懂。

2004-8-4 00:00
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
Climbing
铂金会员

网络独行侠


积分 6962
发帖 2753
注册 2003-4-16
来自 河北保定
状态 离线
『第 25 楼』:  

这位twf_cc大侠的英文实在太好,所以很多时候都是中英文混合表达思想,英文比中文表达的还要明白,这还是为了照顾我们初学者,否则就是全英文啦。pf!当然,这位朋友肯定是Unix高手,不知道为什么浪迹到了DOS论坛。



偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
2004-8-4 00:00
查看资料  发送邮件  访问主页  发短消息 网志  OICQ (653668)  编辑帖子  回复  引用回复
lxmxn
版主




积分 11386
发帖 4938
注册 2006-7-23
状态 离线
『第 26 楼』:  


  太强了。

  特意把这个帖子顶起来,让大家学习一下sed命令的使用语法。


2006-10-26 11:57
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
electronixtar
铂金会员





积分 7493
发帖 2672
注册 2005-9-2
状态 离线
『第 27 楼』:  

看完了两页天书的路过




C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
2006-10-26 21:08
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
9527
银牌会员

努力做坏人


积分 1185
发帖 438
注册 2006-8-28
来自 北京
状态 离线
『第 28 楼』:  

我从来就是路过,根本就没有仔细看,因为没有时间,做个记号在说吧...留着以后在看



我今后在论坛的目标就是做个超级坏人!!!
2006-10-26 22:14
查看资料  发短消息 网志  OICQ (329429)  编辑帖子  回复  引用回复
ceii
初级用户





积分 115
发帖 45
注册 2007-3-3
状态 离线
『第 29 楼』:  

我觉得19楼的问题本来可以很简单的,为什么搞得那么复杂?
dir /b | sed -r "s/.(.*)/ren & \1/"

2007-3-3 10:39
查看资料  发短消息 网志   编辑帖子  回复  引用回复
vkill
金牌会员





积分 4103
发帖 1744
注册 2006-1-20
来自 甘肃.临泽
状态 离线
『第 30 楼』:  

英语太好了

2007-3-3 11:45
查看资料  发送邮件  访问主页  发短消息 网志   编辑帖子  回复  引用回复
« [1] [2] »
请注意:您目前尚未注册或登录,请您注册登录以使用论坛的各项功能,例如发表和回复帖子等。


可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题



论坛跳转: