我有一个格式如下的CSV文件:
id @ word @ information @ other information
Run Code Online (Sandbox Code Playgroud)
有时,第一列重复出现:
001 @ cat @ makes a great pet @ mice
002 @ rat @ makes a great friend @ cheese
003 @ dog @ can guard the house @ chicken
004 @ cat @ can jump very high @ fish
Run Code Online (Sandbox Code Playgroud)
您可以看到,第一行和最后一行在第2列中有重复数据.我想删除这些重复项(如果第2列完全相同)并合并第三列中包含的信息以及第四列中包含的信息.结果是这样的:
001 @ cat @ ? makes a great pet ? can jump very high @ ? mice ? fish
002 @ rat @ makes a great friend @ cheese …Run Code Online (Sandbox Code Playgroud) 我正在用sed进行查找和替换,用BASH变量替换BASH变量$a(当在新行的开头时)$b:
sed -i "s#^$a#$b#" ./file.txt
Run Code Online (Sandbox Code Playgroud)
这取代了所有匹配^$a.如何只替换^a整个文件中的第一个匹配项?
如何删除第二列中包含3个以上字符的CSV文件中的所有行?例如:
cave,ape,1
tree,monkey,2
Run Code Online (Sandbox Code Playgroud)
第二行在第二列中包含3个以上的字符,因此将被删除.
我有一些awk用于解析CSV文件的脚本.我注意到,如果一个单元格为空,awk只需移动到下一个单元格.这意味着,如果我要求它读取第4列,但该单元格为空,则会打印第5列的数据,例如:
echo "1@2@3@@5" | awk -F "@*" '{print $4}'
Run Code Online (Sandbox Code Playgroud)
我的预期结果是它什么都不打印,因为第4列是空的.
awk跳过第4列?awk不忽略空列?我需要检查一个数字是否大于或等于100且小于200.
如果100 = <x <200那么
Lua中的正确语法是什么?
与从最长到最短的排序行类似,如何将文件中的所有行从最短到最长排序?例如"
This is a long sentence. This is not so long. This is not long.
那就变成:
This is not long. This is not so long. This is a long sentence.
我有一串"单词",如下所示:fIsh mOuntain rIver.单词用空格分隔,我在字符串的开头和结尾添加了空格,以简化"单词"的定义.
我需要更换含有任何的话A,B或C与1含有任何文字X,Y或Z用2,并与所有剩余的词3,如:
the CAT ATE the Xylophone
Run Code Online (Sandbox Code Playgroud)
首先,替换包含A,B或C与1字符串相关的单词变为:
the 1 1 the Xylophone
Run Code Online (Sandbox Code Playgroud)
接下来,替换包含X,Y或Z与2字符串相关的单词变为:
the 1 1 the 2
Run Code Online (Sandbox Code Playgroud)
最后,它替换所有剩余的单词3,例如:
3 1 1 3 2
Run Code Online (Sandbox Code Playgroud)
最终输出是一个只包含数字的字符串,其间有空格.
$5?fish可以是单词.定义单词开头和结尾的唯一特征是空格.ZebrA简单地替换为1.如何用数字替换包含这些特定字符的所有单词,最后用3?替换所有剩余单词?
我有一个文档,在方括号内包含一些文本,例如:
The fish [ate] the bird.
[This is some] text.
Here is a number [1001] and another [1201].
Run Code Online (Sandbox Code Playgroud)
我需要删除方括号和方括号内的所有信息,例如:
The fish the bird.
text.
Here is a number and another .
Run Code Online (Sandbox Code Playgroud)
sed -r 's/\[[+]\]//g' file.txt,但这没用.如何删除模式中的任何内容[<anything>]?
我有这样一个文件:
all <div class="first">these</div> <div class="second">words</div> <div class="second">are</div> <div class="second">marked</div> <div class="second">but</div> these words are not.
<div class="first">this</div> is <div class="second">another</div> <div class="second">example</div> with <div class="second">some</div> unmarked words.
Run Code Online (Sandbox Code Playgroud)
我需要在前后都有空格的所有单词周围放置大括号,例如,输出将是:
all <div class="first">these</div> <div class="second">words</div> <div class="second">are</div> <div class="second">marked</div> <div class="second">but</div> {these} {words} {are} not.
<div class="first">this</div> {is} <div class="second">another</div> <div class="second">example</div> {with} <div class="second">some</div> {unmarked} words.
Run Code Online (Sandbox Code Playgroud)
all 因为之前没有空间,所以没有给予支撑.not.并words.没有被替换,因为之后没有空格.我尝试了许多不同的东西awk,但没有什么工作正常.这是我能得到的最接近的:
awk '{ gsub(/.[[:blank:]][[:alpha:]][[:blank:]]*/, "{&}"); }1'
Run Code Online (Sandbox Code Playgroud)
a,b,c,d,e …我需要删除出现在括号{和}括号外的所有数据.例如,这是一行$variable:
The fish {{went}} to the {{restaurant}} to eat some {fish} for lunch.
Run Code Online (Sandbox Code Playgroud)
在删除配对{和}s 之外的所有内容之后的输出将是:
{{went}}{{restaurant}}{fish}
Run Code Online (Sandbox Code Playgroud)
我发现后删除方括号外的所有数据,这是类似的,并与方括号的交易,但我试图修改两个答案做工失败,因为两者[并{可以在代码中有多种含义,无论是作为符号显示在原始数据,或作为东西sed或者awk或正则表达式使用.这是我尝试的,基于另一篇文章中的答案.
awk -F '\{\}\{\}' '{for (i=2; i<NF; i+=2) printf "[%s]%s", $i, OFS; print ""}' <<< "$variable"
sed -e 's/^[^\{]*//;s/\}[^\{]*\[/\} \[/g;s/[^{]*$//;' <<< "$variable"
Run Code Online (Sandbox Code Playgroud)
如何进行适当的修改,以便其中一个删除括号外的所有数据?