从ksh读取文本文件时跳过行

时间:2013-12-25 16:27:45

标签: shell unix scripting ksh

我有一个文本文件,一行一行。 我正在通过KornShell(ksh)读取此文件并获取这些名称并在循环中执行一些操作。 我想在文本文件中添加一些注释以便于阅读(即,以#开头的行是注释,无需阅读)。 所以,我想要的是读取不以#符号开头的行。

在ksh中,我读的是这样的:

while read base
do
---
---
done<file

我尝试使用grep,但是没有用。 我想要正确的语法来实现它在ksh。

2 个答案:

答案 0 :(得分:1)

你可以这样做(read.sh):

#!/bin/ksh

while read line
do
    [[ $line = \#* ]] && continue

    echo $line
done < read.sh

答案 1 :(得分:0)

这个怎么样(编辑为包含完整的代码片段):

while read base
do
    # skip comments
    [ -z "`echo $base | grep '^#'`" ] || continue
    # handle remaining lines here
done<file

other answer包含更简洁的ksh - ish解决方案。

相关问题