如何在csh脚本中跳过文本首行,从第二行读取数据并按指定格式输出
解决方案
可用csh实现脚本
#!/bin/csh # 配置输入文件路径 set input_file = "Sample.txt" # 保存原有字段分隔符,避免破坏shell环境 set old_ifs = $IFS set IFS = "|" # 清空历史输出文件,避免内容追加 > output.txt # 跳过表头行,逐行处理数据 tail -n +2 $input_file | while read f1 f2 f3 f4 f5 f6 echo "Value 1 is: $f4" >> output.txt echo "Value 2 is: $f5" >> output.txt echo "Value 3 is: $f6" >> output.txt end # 恢复原有字段分隔符 set IFS = $old_ifs
使用说明
- 给脚本添加执行权限:
chmod +x your_script_name.csh - 直接运行脚本即可生成符合格式要求的
output.txt文件 - 若输入文件存在多行数据,脚本会自动将所有行的对应值依次写入输出文件
如果希望写法更简洁,也可以直接在csh中调用awk实现,逻辑更清晰不易出错:
#!/bin/csh awk -F '|' 'NR>1 {print "Value 1 is: "$4"\nValue 2 is: "$5"\nValue 3 is: "$6}' Sample.txt > output.txt
内容的提问来源于stack exchange,提问作者Saurabh
相关产品推荐
相关产品推荐

