我有一个包含以下信息的文本文件
info.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
提前致谢
答案 0 :(得分: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
答案 1 :(得分: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,没有外部工具,也没有子shell!
答案 2 :(得分:2)
使用perl:
$ 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
答案 3 :(得分:2)
只是为了好玩:
( IFS=$'\n,'; printf '%.0s%s total years of work experience:%.0s%.0s %s\n' $(grep 'partTimeStaff' info.txt ) )
答案 4 :(得分: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
}