如何使用shell脚本从文件中删除某些单词?

I have to delete every word containing at least one number from each file given through the command line as parameter. This is my code:

while [ "$*" != "" ]; do
    if [ ! -f $1 ]
        then echo "$1 not file"
    else
        sed -ie  "s/[^ ]*[0-9][^ ]*//g" $1
    fi
    shift
done

It works perfectly if I have only one file, but if I have more it gives the same result for each. After I run the script in each file there will be the result of the first one.

Can someone tell me what I'm missing?

EDIT2:

This is what I'm running now:

while [ "$#" -ne 0 ]; do
for file in "$@"; do
    if [ ! -e "$file" ]; then
        printf "file doesn't exist: %s\n" "$file"
        continue;
    fi
    if [ ! -f "$file" ]; then
        printf "not a file: %s\n" "$file"
        continue;
    fi
done
    sed -i "s/[^ ]*[0-9][^ ]*//g" "$file"
done

I was talking about the done for the while loop and the done for the for loop; but even without that my script keeps running.

EDIT:

Basically the same thing just a bit different. I have to delete the second and fourth word from each line from each file (word only contain alphanumeric characters). Its' not working properly and I cant find the error. This is my code:

while [ "$*" != "" ]; do
    if [ ! -f $1 ]
        then echo "$1 not file"
    else
        sed -ie  's/^\( *[^ ]+\) +[^ ]+\(.*\)/\1\2/
                  s/^\( *[^ ]+\)\( +[^ ]+\) +[^ ]+\(.*\)/\1\2\3/g' $1
    fi
    shift
done

#0

the while loop condition should check if there are no arguments, and if there are it should continue. So the right form would be

while [ "$#" -ne 0 ]; do

Now, what you really want is to get each argument and do something with it. That automatically implies a for loop. So what you really should be doing is

for file in $@; do

Now, a file can have spaces in it, so getting that filename and checking if it is really a file should be quoted, and you should also check for existance first

for file in "$@"; do
    if [ ! -e "$file" ]; then
        printf "file doesn't exist: %s\n" "$file"
        continue;
    fi
    if [ ! -f "$file" ]; then
        printf "not a file: %s\n" "$file"
        continue;
    fi
    sed -i "s/[^ ]*[0-9][^ ]*//g" "$file"
done

I could expand more on sed where the -i switch is restricted only to GNU sed. Apart from that you may also want to keep a backup of that file in case something goes wrong.
But this is another topic I guess.

推荐文章

[mysql]linux mysql 基础命令操作

[mysql]linux mysql 基础命令操作

推荐文章

英国城市SQL

英国城市SQL

推荐文章

什么是unix命令,用于删除行的前N个字符?

什么是unix命令,用于删除行的前N个字符?

推荐文章

WPF映像外部资源

WPF映像外部资源

推荐文章

如何私有化部署车牌识别?

如何私有化部署车牌识别?

推荐文章

使用Powershell打印与RegEx匹配的代码行号

使用Powershell打印与RegEx匹配的代码行号

推荐文章

java用正则表达式删除字符串前、后的空格。

java用正则表达式删除字符串前、后的空格。

推荐文章

在VS 2005中启动调试时w3wp崩溃

在VS 2005中启动调试时w3wp崩溃

推荐文章

Java验证邮箱和手机号码正则表达式

Java验证邮箱和手机号码正则表达式

推荐文章

Cookie与session详解

Cookie与session详解

推荐文章

如何从varchar隐式转换为Varbinary?

如何从varchar隐式转换为Varbinary?

推荐文章

MediumText和ASP/MySQL的数据损坏

MediumText和ASP/MySQL的数据损坏

推荐文章

在sqldatasource中不需要编写任何代码就可以获得行数吗?

在sqldatasource中不需要编写任何代码就可以获得行数吗?

推荐文章

Use CSS Grid to create a self-centering full-width element

Use CSS Grid to create a self-centering full-width element

推荐文章

我应该在web应用程序的Java bean中添加对PropertyChangeSupport和PropertyChangeListener的支持吗?

我应该在web应用程序的Java bean中添加对PropertyChangeSupport和PropertyChangeListener的支持吗?

推荐文章

traceview的紧框架等价

traceview的紧框架等价