开发者

Deleting a PARTICULAR word from a file using shell script

Hi I am having a problem in deleting a particular set of words from a file using Shell script. Here goes my problem,

My file: group.dat

Sample lines

ADMIN

ADMINISTRATION

ADMINISTRATOR

My Script: groupdel.sh

#!/bin/s开发者_Go百科h
groupf="<pathtofile>/group.dat"
tmp="<pathtofile>/te"
delgrp()
{
        echo "Enter the group to be deleted"
        read gname
        echo "-------------------"
        for gn in `cat $groupf`
        do
                if [ "$gname" = "$gn" ]
                then
                        sed -e "s/$gname//g" $groupf > $tmp&&mv $tmp $groupf
                        echo "deleted group"
                        cat $groupf
                        exit 1
                fi
        done
}
echo "Welcome to Group delete wizard"
delgrp

Output: Enter the group to be deleted

ADMIN

deleted group

ISTRATION ISTRATOR

Problem: My problem is I dont want the script to delete ADMINISTRATION or ADMINISTRATOR but to delete only ADMIN, any help how to achieve it.

Thanks in Advance


#!/bin/sh
groupf="<pathtofile>/group.dat"
tmp="<pathtofile>/te"
delgrp()
{
    echo "Enter the group to be deleted"
    read gname
    echo "-------------------"
    sed -e "/^$gname[[:blank:]]/d" "$groupf" > "$tmp" && mv "$tmp" "$groupf"
    echo "deleted group $gname"
    cat "$groupf"
    return 0
}
echo "Welcome to Group delete wizard"
delgrp

Assuming that the group name is at the beginning of the line and there are other things on the line and you want to delete the whole line, use the regular expression and command as shown.

There's no need for a loop since sed will iterate over the lines of the file for free.

You should return from a function rather than exit from it. Zero means success. One indicates an error or failure.

Always quote variable names that contain filenames.


If the file is one group per line, and the group name is the only thing on the line, use anchors in your regular expression:

s/^$gname:.*//g

If you have Perl installed, you can probably simplify this a bit with something like this:

if grep -q "^${gname}:" $groupf ; then
    perl -ni -e "print unless /^${gname}:/" $groupf
    echo "Group deleted."
else
    echo "No such group $gname."
fi

Or even

grep -v "^${gname}:" $groupf > $tmp && \
    cp -f $tmp $groupf && rm -f $tmp

which will copy all lines except the matching one to the temporary file, and then copy the tempfile over the original file, replacing it.

Note that I suggest using a cp rather than a mv in order to retain the permissions of the original file; mv will result in the edited file having permissions set according to your umask with no concern for the original permissions.

So, for the complete script:

#!/bin/sh
groupf="<pathtofile>/group.dat"
tmp="<pathtofile>/te"
delgrp()
{
    echo -n "Enter the group to be deleted: "
    read gname
    echo "-------------------"
    if grep -q "^${gname}:" $groupf ; then
        grep -v "^${gname}:" $groupf > $tmp
        cp -f $tmp $groupf
        rm -f $tmp
    else
        echo "No such group '$gname'"
    fi
}
echo "Welcome to Group delete wizard"
delgrp

That should work reliably.


You can use \W to denote the start and end of a word, if they are separated properly:

sed -e "s/\(\W\)$gname\(\W\)/\1\2/g" $groupf > $tmp&&mv $tmp $groupf


Awk is a readable alternative to sed:

awk -v to_delete="$gname" -F: '$1 == to_delete {next} {print}'


Why you don't use sed ?

sed 's/^word$//g' 

Also you can use regex to specify multiple words

sed 's/word1|word2//g'

I didn't try this, but this is what you need. Just take a look on Internet on the sed syntax.

Regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜