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

发布于 2024-11-15 16:54:27 字数 1111 浏览 2 评论 0原文

我必须从通过命令行作为参数给出的每个文件中删除至少包含一个数字的每个单词。这是我的代码:

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

如果我只有一个文件,它会完美地工作,但如果我有更多文件,它会为每个文件提供相同的结果。在每个文件中运行脚本后,将会出现第一个文件的结果。

有人能告诉我我错过了什么吗?

编辑2:

这就是我现在正在运行的:

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

我正在谈论 while 循环的完成和 for 循环的完成;但即使没有,我的脚本仍然运行。

编辑:

基本上是相同的事情,只是有点不同。我必须删除每个文件中每一行的第二个和第四个单词(单词仅包含字母数字字符)。它无法正常工作,我找不到错误。这是我的代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-11-22 16:54:27

while 循环条件应检查是否没有参数,如果有则应继续。所以正确的形式是

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

现在,你真正想要的是获取每个参数并用它做一些事情。这自动意味着一个for循环。所以你真正应该做的是

for file in $@; 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
    sed -i "s/[^ ]*[0-9][^ ]*//g" "$file"
done

存在可以对 sed 进行更多扩展,其中 -i 开关仅限于 GNU sed。除此之外,您可能还想保留该文件的备份,以防出现问题。
但我想这是另一个话题了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文