Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/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
String 使用sed搜索并替换字符串中的第一个单词(字符串除以空格)_String_Sed - Fatal编程技术网

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
我尝试过