String 使用sed搜索并替换字符串中的第一个单词(字符串除以空格)
输入字符串:String 使用sed搜索并替换字符串中的第一个单词(字符串除以空格),string,sed,String,Sed,输入字符串:“a-random\u name/this/is/a/path\u to-file 0” 输出字符串:“arandomname/this/is/a/path\u to-file 0” 使用sed只替换第一个单词。word是一个随机字符串。试试这个简单的awk方法 awk '{gsub(/-|_/,"",$1); print}' <<< '"a-random_name /this/is/a/path_to-file 0"' 说明: "arandomname /th
“a-random\u name/this/is/a/path\u to-file 0”
输出字符串:
“arandomname/this/is/a/path\u to-file 0”
使用sed只替换第一个单词。word是一个随机字符串。试试这个简单的awk方法
awk '{gsub(/-|_/,"",$1); print}' <<< '"a-random_name /this/is/a/path_to-file 0"'
说明:
"arandomname /this/is/a/path_to-file 0"
基于默认空格分隔符提取第一列,并使用gsub
函数删除-
和
Sed
版本:
"arandomname /this/is/a/path_to-file 0"
我不确定这是不是有效的方法,但它会帮助你
sed -e 's/-\|_//1' -e 's/-\|_//1' <<< '"a-random_name /this/is/a/path_to-file 0"'
sed-e的/-\\\\\\\\\\\\\\\\\\\\//1'-e的/-\\\\\\\\\\\\\\\\\\//1'与GNU sed:
sed ':a;s/^\([^-_ ]*\)[-_]/\1/;ta;' file
这是可行的,但是如果有使用sed的替代解决方案,sed命令将只适用于两次出现的\uuu
或-
@SLePort,是的,我知道,但我指的是awk
,但是sed
我尝试过