Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 剪切grep并对textfile中的值进行排序和打印_Bash_Shell_Perl - Fatal编程技术网

Bash 剪切grep并对textfile中的值进行排序和打印

Bash 剪切grep并对textfile中的值进行排序和打印,bash,shell,perl,Bash,Shell,Perl,我有一个包含以下信息的文本文件 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 我试图得到员工的姓名和工作经验的数量,然后打印出来,比如

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

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
我试图得到员工的姓名和工作经验的数量,然后打印出来,比如(“员工姓名总工作经验年数:工作经验”)

这是我目前拥有的

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 total years of work experience : 0
susan  total years of work experience : 1

提前感谢

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

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
echo“兼职员工”
IFS=,read-r-a数组;做
[[“${array[3]}”==partTimeStaff]| |继续
printf“%s总工作年限:%s\n”“${array[1]}”“${array[4]}”
完成

而且都是100%bash,没有外部工具,也没有子shell

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

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
echo“兼职员工”
IFS=,read-r-a数组;做
[[“${array[3]}”==partTimeStaff]| |继续
printf“%s总工作年限:%s\n”“${array[1]}”“${array[4]}”
完成

而且都是100%bash,没有外部工具,也没有子shell

此任务使用
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

此任务使用
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
与:

与:


下面是一个解决方案,循环查找每个类别名称,并为每个类别生成所需的输出。我已将其放入函数中,因此您可以直接调用它:

编辑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
编辑:为了响应同行的压力
我对该功能进行了一些简化,尽管在文件格式上稍微有点不灵活(sed
必须更精确地匹配输入字段)。输出如上所述(小标题上有缩进):


下面是一个解决方案,循环查找每个类别名称,并为每个类别生成所需的输出。我已将其放入函数中,因此您可以直接调用它:

编辑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
编辑:为了响应同行的压力
我对该功能进行了一些简化,尽管在文件格式上稍微有点不灵活(sed
必须更精确地匹配输入字段)。输出如上所述(小标题上有缩进):

只是为了好玩:

( IFS=$'\n,'; printf '%.0s%s total years of work experience:%.0s%.0s %s\n' $(grep 'partTimeStaff' info.txt ) )
只是为了好玩:

( IFS=$'\n,'; printf '%.0s%s total years of work experience:%.0s%.0s %s\n' $(grep 'partTimeStaff' info.txt ) )


对Perl是LSB的一部分,因此应该具有足够的可移植性,它正是为此而设计的!非常感谢你的建议和帮助。Perl是LSB的一部分,因此应该具有足够的可移植性,它正是为此而设计的!谢谢你的建议和帮助注意,如果一个男人的名字包含了
partTimeStaff
,你会有一些惊喜。好吧,我不知道有谁有这样的名字,但谁知道呢?注意,如果一个人的名字包含
partTimeStaff
,你会有一些惊喜。好的,我不知道有谁有这样的名字,但谁知道呢?练习1:计算生成的子壳的数量,除以行数,然后与0.5进行比较。附加问题:你认为这是一个好方法吗?子shell不需要任何费用,至少在我的系统上是这样的。。。我的衡量标准是可以编辑的,可以理解的,而且工作做得很好。你的一行也不错。好的@gniourf\u gniourf我花了一些时间在一个更有效的解决方案上<如果您的
sort
恰好足够聪明,可以拥有
-u
开关,那么code>sort | uniq最好写成
sort-u
。很遗憾,
mary ann
billie joe
désireée
不能成为员工的一部分<代码>:)Oh,管道
grep
sed
通常不是最好的选择!练习1:计算生成的子壳数,除以行数,并与0.5进行比较。附加问题:你认为这是一个好方法吗?子shell不需要任何费用,至少在我的系统上是这样的。。。我的衡量标准是可以编辑的,可以理解的,而且工作做得很好。你的一行也不错。好的@gniourf\u gniourf我花了一些时间在一个更有效的解决方案上<如果您的
sort
恰好足够聪明,可以拥有
-u
开关,那么code>sort | uniq最好写成
sort-u
。很遗憾,
mary ann
billie joe
désireée
不能成为员工的一部分<代码>:)Oh,管道
grep
sed
通常不是最好的选择!
%.0s
很聪明。没想到。很高兴你喜欢我有趣的解决方案
:)
这个
%.0s
很聪明。没想到。很高兴你喜欢我有趣的解决方案。
:)