这是示例文件:

someText here some other columns (exist but I don't intend to read them)
anotherText there some other columns (exist but I don't intend to read them)
YouAreGood here some other columns (exist but I don't intend to read them)
HeIsNormal there some other columns (exist but I don't intend to read them)

我想检查第二列是否是here,然后做点什么。

这是我的尝试,我不确定如何正确:

#!/usr/bin/bash

awk '{print $2}' sample | while read -r line
do
    if [ "$line" == 'here' ]
    then
        echo "$line"
    fi
done

第二个文件中的预期输出:

someText
YouAreGood

我考虑使用awk '{print $2}' sampleif [ "$line '{print $2}'" == 'here' ],但它将始终执行else块。

分析解答

看起来这个awk应该对您有用:

awk '$2 == "here" {print $1}' file

someText
YouAreGood