我有这个文件:
John Green', 'Age: 32', 'State: New York', 'Total cars: 2', 'Manufacter: General Motor', 'Model: Pontiac', 'Year: 2000', 'Manufacter: Ford Motor', 'Model: Endeavour', 'Year: 2010
Peter Jones', 'Age: 20', 'State: Florida', 'Total cars: 0
Richard Smith', 'Age: 44', 'State: Illinois ', 'Total cars: 1', 'Manufacter: Toyota', 'Model: Yaris', 'Year: 2005
Brian Brown', 'Age: 42', 'State: Texas', 'Total cars: 0
Vincent Osmnod', 'Age: 39', 'State: Maryland', 'Total cars: 1', 'Manufacter: Fiat', 'Model: 500X', 'Year: 2015
我可以使用 awk 提取第 4 个字段
,如果我想获取', '
每行的出现次数,我使用脚本:
grep -o -n "', '" file | cut -d : -f 1 | uniq -c
这个命令还给了我行数
9 1
3 2
6 3
3 4
6 5
所以我可以单独得到结果
我想要的输出是:
Total cars: 2 |9 1
Total cars: 0 |3 2
Total cars: 1 |6 3
Total cars: 0 |3 4
Total cars: 1 |6 5
我试过这个脚本:
#!/bin/bash
FILENAME=$1
count=0
while read LINE
do
OUTP1=$(awk -F"', '" '{print $4" |"}' $LINE)
OUTP2=$(grep -o -n "', '" $LINE1 | cut -d : -f 1 | uniq -c)
echo "$OUTP1 $OUTP2"
done < $FILENAME
它给了我这个输出:
awk: cannot open John (No such file or directory)
3 1
6 2
3 3
6 4