Birion
February 1st, 2012, 09:59 AM
Hi, I've been trying to write a bash script that reads a file with single words delimited by newlines, looking for the first line that does not start with a specific character (e.g. #), does something based on the line, and puts # at the beginning of the line to mark it done.
$ cat ~/example.txt
#cat
#dog
cat
dog
fish
SOURCE_FILE=~/example.txt
while read line
do
ISDONE=`echo $line | sed "s/^(.).*/\$1/" -` # this line may be wrong, since I'm not at a computer that can run bash, so I can't remember exactly (and the computer that can is currently Internet-less and far away), but it's correct in the script
if [[ $ISDONE -eq "#" ]]
then
continue
else
case $line in
... # do stuff here based on the contents of the line
esac
# here's the problem
# I wanted to put something like
sed "$line s/^/#/"
# but I kept hitting some errors
exit
fi
done < $SOURCE_FILE
so that after running the script once, the file should look like
$ cat ~/example.txt
#cat
#dog
#cat
dog
fish
$ cat ~/example.txt
#cat
#dog
cat
dog
fish
SOURCE_FILE=~/example.txt
while read line
do
ISDONE=`echo $line | sed "s/^(.).*/\$1/" -` # this line may be wrong, since I'm not at a computer that can run bash, so I can't remember exactly (and the computer that can is currently Internet-less and far away), but it's correct in the script
if [[ $ISDONE -eq "#" ]]
then
continue
else
case $line in
... # do stuff here based on the contents of the line
esac
# here's the problem
# I wanted to put something like
sed "$line s/^/#/"
# but I kept hitting some errors
exit
fi
done < $SOURCE_FILE
so that after running the script once, the file should look like
$ cat ~/example.txt
#cat
#dog
#cat
dog
fish