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
从输入表获取数据的Shell脚本:-_Shell - Fatal编程技术网

从输入表获取数据的Shell脚本:-

从输入表获取数据的Shell脚本:-,shell,Shell,我是shell脚本新手,希望使用shell脚本从表中获取具体细节 要求:- 输入文件: model fuel type economy swift petrol 18 swift diesel 25 i20 petrol 22 i20 diesel 15 alto petrol 20 alto diesel Null 输出文件: model petrol dies

我是shell脚本新手,希望使用shell脚本从表中获取具体细节

要求:-

输入文件:

model   fuel type   economy
swift    petrol       18
swift    diesel       25
i20      petrol       22
i20      diesel       15
alto     petrol       20
alto     diesel      Null
输出文件:

model   petrol  diesel
swift   18        25
i20     22        15
alto    20       Null

我想在一行中使用正则表达式来实现这一点。需要帮助。

您可以使用
awk

$ awk 'NR>1{                                      # Skip first line
         a[$1][$2]=$3                             # Store file content into an array
       } 
       END{                                       # After the file is parsed
         OFS="\t";                                # Set tabulation as output field separator
         print "model","petrol","diesel";
         for(i in a){                             # Go through all array elements
            print i,a[i]["petrol"],a[i]["diesel"] # Print content from the array
         }
      }' file
model   petrol  diesel
alto    20      Null
swift   18      25
i20     22      15

感谢您提供的信息,但是我正在尝试从文件中自动获取标题,仍然在寻找使用awk命令实现值echo“Model,
cat file | awk-F,{print$2}'| grep-v FuelType | sort-nr | uniq | tr'\n','
”输出:汽油-柴油型号,因此如果添加任何新的燃油类型,它将自动被视为收割台。目前我面临的挑战是无法获得将值放入第2列和第3列的逻辑,即汽油和柴油您所说的内容在您的问题中没有明确询问。。。无论如何,这与上面的代码没有太大区别。作为练习,您可以自己更新代码。提示:使用第二个
for
循环和
printf
语句(而不是
print