I have a string variable as
我有一个字符串变量
columns = "name string,age int,address string,dob timestamp"
I want to remove the datatypes. ie I want to remove the words coming after space and a comma. The output should be as
我想删除数据类型。即我想删除空格和逗号之后的单词。输出应为
name,age,address,dob
1 个解决方案
#1
2
Assuming bash
shell and extglob
shell option is available - see pattern matching manual
假设bash shell和extglob shell选项可用 - 请参阅模式匹配手册
$ columns='name string,age int,address string,dob timestamp'
$ echo "${columns// +([^,])/}"
name,age,address,dob
With sed
用sed
$ echo "$columns" | sed 's/ [^,]*//g'
name,age,address,dob
With awk
to process fields separated by ,
用awk分隔的进程字段,
$ echo "$columns" | awk -F, -v OFS="," '{for(i=1; i<=NF; i++){split($i,n," "); $i=n[1]}} 1'
name,age,address,dob
If all columns contain two words separated by space, one can use space or comma as delimiter and filter out unwanted fields
如果所有列都包含由空格分隔的两个单词,则可以使用空格或逗号作为分隔符并过滤掉不需要的字段
$ echo "$columns" | awk -F' |,' -v OFS=',' '{print $1,$3,$5,$7}'
name,age,address,dob
#1
2
Assuming bash
shell and extglob
shell option is available - see pattern matching manual
假设bash shell和extglob shell选项可用 - 请参阅模式匹配手册
$ columns='name string,age int,address string,dob timestamp'
$ echo "${columns// +([^,])/}"
name,age,address,dob
With sed
用sed
$ echo "$columns" | sed 's/ [^,]*//g'
name,age,address,dob
With awk
to process fields separated by ,
用awk分隔的进程字段,
$ echo "$columns" | awk -F, -v OFS="," '{for(i=1; i<=NF; i++){split($i,n," "); $i=n[1]}} 1'
name,age,address,dob
If all columns contain two words separated by space, one can use space or comma as delimiter and filter out unwanted fields
如果所有列都包含由空格分隔的两个单词,则可以使用空格或逗号作为分隔符并过滤掉不需要的字段
$ echo "$columns" | awk -F' |,' -v OFS=',' '{print $1,$3,$5,$7}'
name,age,address,dob