LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script: File Manipulation (https://www.linuxquestions.org/questions/programming-9/shell-script-file-manipulation-377340/)

simon_qwl 10-27-2005 04:35 AM

shell script: File Manipulation
 
hi,everyone!i want write a shell script which can delete certain line in a file based on the user's selection.
user will give the string pattern in command line and the shell script will delete any matched lines in a file.i tried to use 'sed' to achieve this,however sed command considers the '$1' is a string pattern rather than a varaible. it will search the file with string pattern '$1'.
thanks a lot for any helps!

homey 10-27-2005 05:10 AM

Hi,
Something like this ...
Code:

#!/bin/bash

echo
echo ""
echo "Enter string: "
read info
echo

file="/home/file.txt"
stuff=`cat $file | grep -n "$info" | cut -d: -f1`
cat $file | sed -e ''$stuff'd'


eddiebaby1023 10-29-2005 03:40 PM

Quote:

Code:

stuff=`cat $file | grep -n "$info" | cut -d: -f1`
cat $file | sed -e ''$stuff'd'


Please don't be a process hog (and your quoting leaves a bit to be desired ;-) ):
Code:

stuff=`grep -n "$info" $file | cut -d: -f1`
sed -e "/$stuff/d" $file

Simon, if you use double quotes around the $1, you'll find sed will work perfectly.
Code:

sed "/$1/d" file
Learn how to use quotes properly and you'll find scripting becomes much easier.


All times are GMT -5. The time now is 06:18 AM.