bash - Split export format in bash
问题描述
Given a file with the following syntax:
export 'my_var1=value1' 'my_var2=value2' 'my_var3=value3'
export 'my_var3=value3' 'my_var4=value4'
Are there any way to split it in bash order to get the variables names?
my_var1
my_var2
my_var3
I thought first spliting by line and then by space. But "values" can contain spaces. I think the key is in the quotes.
Thanks.
解决方案
awk 可以很好地解决这个问题,并且相当容易阅读。
awk -F "export '|' '|'$" '{for(i=1;i<NF;i++){if(split($i,arr,"=")==2)print arr[1]}}' file
解释:
-F "export '|' '|'$"
- 三种模式(export '
, ' '
, '$
)中的任何一种都用于分隔每一行。
if(split($i,arr,"=")==2)print arr[1]
- 对于每个标记,用 分隔=
,如果有两个标记,则打印第一个。
编辑:正如评论中指出的那样,当值可以有超过 2 个标记时,需要制作 '= 分隔符和处理:
awk -F "export '|' '|'$" '{for(i=1;i<NF;i++){if(split($i,arr,"\x27=")!=0)print arr[1]}}' file
推荐阅读
- java - 根据条件将常数值分配给不同变量中的一个
- angular-material - 为什么我的不行
加载时是否选择了指定值? - java - htm2pdf itextpdf外部css不生效
- c# - 如何删除列表的元素
与另一个列表相关 ? - c++ - C++中使用向量初始化链表
- sql - 撤销用户对 DROP 表的权限
- php - 订单总额的变量名称是什么(Back-office prestashop 1.7.5)?
- spring-boot-admin - Spring Boot Admin:过滤通知的示例
- python - 在单词级别测量分类器的准确性
- javascript - 如何在javascript函数参数中回显带有特殊字符的php字符串