如何在两列上使用循环并仅在第二列包含特定字符串时才打印第一列

Sae*_*eed 2 bash awk

这是示例文件:

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)
Run Code Online (Sandbox Code Playgroud)

我想检查第二列是否为here,然后执行某些操作。

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

#!/usr/bin/bash

awk '{print $2}' sample | while read -r line
do
    if [ "$line" == 'here' ]
    then
        echo "$line"
    fi
done
Run Code Online (Sandbox Code Playgroud)

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

someText
YouAreGood
Run Code Online (Sandbox Code Playgroud)

我想过使用awk '{print $2}' sample, or if [ "$line '{print $2}'" == 'here' ],但它else总是会执行块。

anu*_*ava 5

看起来这awk应该适合你:

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

someText
YouAreGood
Run Code Online (Sandbox Code Playgroud)