I'm looping over lines in a file. I just need to skip lines that start with "#". How do I do that?
#!/bin/sh
while read line; do
if ["$line doesn't start with #"];then
echo "line";
fi
done < /tmp/myfile
Thanks for any help!
while read line; do
case "$line" in \#*) continue ;; esac
...
done < /tmp/my/input
Frankly, however, it is often clearer to turn to grep
:
grep -v '^#' < /tmp/myfile | { while read line; ...; done; }
expr
or suffix removal (e.g., [ -z "${line%%#*}" ]
), but these would be equally or less readable than the case
option, I think.
#
, use grep -v '^\s*#' < /tmp/myfile
instead - this is in line with the case
solution, given that read
strips leading and trailing whitespace.
Commented
Sep 19, 2012 at 4:59
grep
) might be if [[ $line =~ ^# ]]; then continue; fi
.
Commented
Sep 19, 2012 at 6:50
set -o lastpipe
is used). Other shells may run the while loop in the current shell by default.
This is an old question but I stumbled upon this problem recently, so I wanted to share my solution as well.
If you are not against using some python trickery, here it is:
Let this be our file called "my_file.txt":
this line will print
this will also print # but this will not
# this wont print either
# this may or may not be printed, depending on the script used, see below
Let this be our bash script called "my_script.sh":
#!/bin/sh
line_sanitizer="""import sys
with open(sys.argv[1], 'r') as f:
for l in f.read().splitlines():
line = l.split('#')[0].strip()
if line:
print(line)
"""
echo $(python -c "$line_sanitizer" ./my_file.txt)
Calling the script will produce something similar to:
$ ./my_script.sh
this line will print
this will also print
Note: the blank line was not printed
If you want blank lines you can change the script to:
#!/bin/sh
line_sanitizer="""import sys
with open(sys.argv[1], 'r') as f:
for l in f.read().splitlines():
line = l.split('#')[0]
if line:
print(line)
"""
echo $(python -c "$line_sanitizer" ./my_file.txt)
Calling this script will produce something similar to:
$ ./my_script.sh
this line will print
this will also print
-i
, it will useinteractive_comments
.