1

我有一个包含以下信息的文本文件

信息.txt

1,susan,12345678,partTimeStaff,1
2,john,54243214,fullTimeStaff,3
3,mary,53214567,contractStaff,3
4,gerald,14546752,partTimeStaff,0
5,joe,14234567,fullTimeStaff,2
6,joshua,11234567,contractStaff,2

我正在尝试获取员工的姓名和工作经验的数量并将其打印出来(“staffName 工作经验总年数:WorkExperience”)

这是我目前拥有的

printOutName=$(cat "info.txt" | cut -d, -f2,4,5 | sort -r -t, -k2 | grep "partTimeStaff" | cut -d, -f1)
printOutYOfExp=$(cat "info.txt" | cut -d, -f2,4,5 | sort -r -t, -k2 | grep "partTimeStaff" | cut -d, -f3)

echo "part Time Staff"
echo -e "$printOutName total years of work experience: $printOutYOfExp"

我注意到输出下面显示的输出有问题

part Time Staff
gerald 
susan  total years of work experience : 0
1

预期产出

part Time Staff
gerald total years of work experience : 0
susan  total years of work experience : 1

提前致谢

4

5 回答 5

3

用于awk此任务,因为它有利于处理分隔数据。

$ awk -F, '/partTimeStaff/{print $2" total years of work experience : "$5}' info.txt
susan total years of work experience : 1
gerald total years of work experience : 0
于 2013-10-29T17:33:32.350 回答
2

您可以逐行解析文件,将每个字段放入数组字段中,选择所需的行,然后输出格式化的字段,如下所示:

echo "part Time Staff"
while IFS=, read -r -a array; do
    [[ "${array[3]}" == partTimeStaff ]] || continue
    printf "%s total years of work experience: %s\n" "${array[1]}" "${array[4]}"
done < info.txt

这都是 100% 的 bash,没有外部工具,也没有 subshel​​l!

于 2013-10-29T17:33:30.267 回答
2

使用

$ perl -F, -lane 'print "$F[1] total years of work experience: $F[4]" if $F[3] eq "partTimeStaff"' info.txt
susan total years of work experience: 1
gerald total years of work experience: 0
于 2013-10-29T17:43:35.920 回答
2

只是为了好玩:

( IFS=$'\n,'; printf '%.0s%s total years of work experience:%.0s%.0s %s\n' $(grep 'partTimeStaff' info.txt ) )
于 2013-10-29T17:49:57.383 回答
1

这是一个循环查找每个类别名称并为每个类别生成所需输出的解决方案。我已经把它放在一个函数中,所以你可以直接调用它:

编辑2:让它更短!

countstaff(){
    TYPE=$(cut -d, -f4 info.txt | sort -u )
    for T in $TYPE; do 
        echo "--- $T ---"
        printf "%s total years of work experience: %s\n" $(grep ${T} info.txt | cut -d, -f2,5 | tr ',' ' ')
    done
    }

输出:

--- contractStaff ---
mary total years of work experience: 3
joshua total years of work experience: 2
--- fullTimeStaff ---
john total years of work experience: 3
joe total years of work experience: 2
--- partTimeStaff ---
susan total years of work experience: 1
gerald total years of work experience: 0

编辑:为了应对同侪压力<coff @gniourf_gniourf coff/>,我稍微简化了该功能,尽管文件格式的灵活性稍差(sed必须更精确地匹配输入字段)。输出如上(小标题上有缩进):

countstaff(){
    FILEN="info.txt"  # can change this to "$1" to change file at run time
    TYPE=$(cut -d, -f4 "$FILEN" | sort | uniq)
    for T in $TYPE; do 
        echo "--- $T ---"
        printf "  %s - worked days: %s\n" $(grep "$T" "$FILEN" | sed -E 's/^[0-9]+,([A-Za-z]+).*,([0-9]+)$/\1 \2/g')
    done
}
于 2013-10-29T17:49:39.367 回答