I\'m trying to remove a specific line from a file and then append the edited line to the file. I get last part right but my sed command to remove the old line is not working.
$
is a special character in sed meaning "end of line". If you want to match the literal $userinput
at the begging of a line use see '/^\$userinput/d'
If you want your shell to substitute the variable $userinput
with its content, use double quotes instead of single quotes:
sed "/^$userinput/d" file1.txt > file2.txt
or better yet
sed "/^${userinput}/d" file1.txt > file2.txt
That will remove lines starting with the value of ${userinput}
, if you want to only delete lines that are exactly the value of ${userinput}
, use
sed "/^${userinput}$/d" file1.txt > file2.txt
works for me:
╰─ ☭ cat file
bar
foobar
foo
asdf
qwer
foo
asdf
╰─ ☭ cat script.sh
#!/bin/bash
sed "/^${1}$/d" file > otherfile
╰─ ☭ ./script.sh foo
╰─ ☭ cat otherfile
bar
foobar
asdf
qwer
asdf
i did try that and found that the variable value wasnt properly replaced by the shell in the sed statement . so the sed command instead of searching for the value as the pattern was doing a search with the variable name as pattern
ie it was trying to search $userinput as pattern @ EOL
this was due to the presence of single quotes , try your command again but replace them with double quotes : it worked im my sys.
code ::
Nitin@Kaizen ~> cat new
#!/bin/bash
echo "the input file :"
cat INPUT2.txt ;
echo "enter line to remove" ;
read rem ;
date >> sed.log ;
sed "/$rem/d" INPUT2.txt >> sed.log;
echo "the output file :" ;
cat sed.log;
run ::
Nitin@Kaizen ~ > ./new
the input file :
aa1
chap1
mk.t
temp.txt
z1
z2
enter line to remove
temp.txt
the output file :
Thu Mar 28 08:18:07 IST 2013
aa1
chap1
mk.t
z1
z2
figured out how to fix it.
update(){
read -p "Enter the course number of the course you would like to update: " updateInput
grep -v ^$updateInput my_course.txt>updatedfile.txt
grep ^$updateInput my_course.txt>editedline.txt
if [ $? -eq 0 ]
then
while(true)
do
read -p "Status: " status
if [ $status -eq 0 ]
then
break
elif [ $status -eq 1 ]
then
break
else
printf "Enter either a 0 or 1\n"
fi
done
read -p "Grade: " grade
while(true)
do
case $grade in
A)
break
B)
break
;;
C)
break
;;
*)
read -p "Enter either an A, B, C: " grade
;;
esac
done
awk -F, '
/^CSC/{printf$1","$2","$3"," >> "updatedfile.txt"}' editedline.txt
printf $status","$grade"\n" >> updatedfile.txt
cat updatedfile.txt > my_course.txt
rm updatedfile.txt
rm editedline.txt
else
printf "This course doesn't exist in your courses list.\n"
fi
}
In the beginning of the code I use
grep -v regexexperssion file1>file2
to get all lines in the file without the one to be removed. Then I use grep to get the actual line to be edited from the file.
grep regexexperssion file1>file3
At the bottom in the awk I perform the edits using the user generated variables on the line in file3 and then I append that to file1, which was generated with the
grep -v regexexpression file1>file2
command
NOTE the above explanation does not match the actual code used above, BECAUSE i thought it would better for people viewing this letter for a more general explanation. Also I know that there is probably a much better way to solve this problem and that the code above could probably be shortened. Any input on how to improve this would be greatly appreciated. Thanks to all those who helped me.
In this case you need do this because $
means end of line regular expressions.
Try:
sed "/^${userinput}/d" file1.txt >> file2.txt
Also >>
will append to the file.