tr可以用两个字符替换一个字符吗?
我试图用"〜\n"替换"〜",但输出不会产生换行符.
$ echo "asdlksad ~ adlkajsd ~ 12345" | tr "~" "~\n"
asdlksad ~ adlkajsd ~ 12345
Run Code Online (Sandbox Code Playgroud) 如果在给定字符向量中的任何位置找到模式grep,R中是否有一个函数返回?TRUEFALSE
我看到的所有函数都返回了找到的每个元素的当前位置的向量.
我正在尝试使用printf和seq打印以下模式:
0000
0001
0002
0003
Run Code Online (Sandbox Code Playgroud)
我的问题是我曾经使用过:
seq 0 10 | xargs printf %04d
Run Code Online (Sandbox Code Playgroud)
我的所有输出格式化为相同的行likeo:
0000000100020003
Run Code Online (Sandbox Code Playgroud)
我仍然无法使用xargs.在这种情况下如何正确使用它?
我编写了代码来从给定的字符串中提取日期.特定
> "Date: 2012-07-29, 12:59AM PDT"
Run Code Online (Sandbox Code Playgroud)
它提取
> "2012-07-29"
Run Code Online (Sandbox Code Playgroud)
问题是我的代码看起来很冗长而且很麻烦.我想知道这样做是否更优雅.
raw_date = "Date: 2012-07-29, 12:59AM PDT"
#extract the string from raw date
index = regexpr("[0-9]{4}-[0-9]{2}-[0-9]{2}", raw_date) #returns 'start' and 'end' to be used in substring
start = index #start represents the character position 's'. start+1 represents '='
end = attr(index, "match.length")+start-1
date = substr(raw_date,start,end); date
Run Code Online (Sandbox Code Playgroud) 我想知道如何使用R的XML包到达特定节点.以下是使用R的内置数据集mtcars的示例.
fileName <- system.file("exampleData", "mtcars.xml", package="XML")
doc <- xmlTreeParse(fileName)
doc$doc$children$dataset
Run Code Online (Sandbox Code Playgroud)
运行上面的代码给了我ff.结果:
....
<record id="Fiat 128">32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1</record>
<record id="Honda Civic">30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2</record>
<record id="Toyota Corolla">33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1</record>
<record id="Toyota Corona">21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1</record>
<record id="Dodge Challenger">15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2</record>
<record id="AMC Javelin">15.2 …Run Code Online (Sandbox Code Playgroud) 我写了一个脚本(replace.sh),用〜\n替换〜.我想知道如何用多个参数调用这个脚本.
#!/bin/bash
for i in "$*"
do
sed 's/~/~\n/g' "$i"
done
Run Code Online (Sandbox Code Playgroud)
例如,我想调用./replace.sh text1 text2.它无法阅读
text1的内容是:1~1~1.调用脚本之后应该是
1 ~
1 ~
1 ~
Run Code Online (Sandbox Code Playgroud) 我有一个文本文档,我想要子串.我正在使用以下代码来子串:
substr(text, start,start+end)
Run Code Online (Sandbox Code Playgroud)
start有60个元素的向量.但是,上面的代码只返回相当于的substr(text,1109,1109+199).如何让它返回所有60个元素,即
substr(text,1109,1109+199)substr(text,11590,11590+199)样本数据
start
[1] 11009 11590 11972 15674 16274 16659 19866 20541 20963 24787 25376
[12] 25746 29458 30011 30363 34086 34702 35087 38643 39095 39416 42626
[23] 43188 43545 46731 47367 47757 51029 51673 52072 55444 56076 56470
[34] 59794 60445 60851 64267 64877 65276 68659 69200 69547 72747 73303
[45] 73657 76896 77648 78103 81541 82050 82391 85277 85848 86227 89128
[56] 89656 90010 92830 93329 93656
end
[1] 199 …Run Code Online (Sandbox Code Playgroud) 因此我昨天学习了如何使用绑定。
通过在终端中键入Ctrl+ v后跟键,我得到了代表该键的原始字符。例如:Ctrl+ v后跟Escreturn ^[。
我的问题是,如何绑定“输入密钥”。Enter键返回,^M但是当我键入命令时
bind '"\e^M":"foobar"'
Run Code Online (Sandbox Code Playgroud)
按Enter键不会导致在我的终端中键入foobar。
我想知道R是否有办法将此格式转换为任何日期对象.格式为'月[空间]日.例如:Jan 1或Jul 29或Jul 30.我只是希望将这些示例作为日期对象读取,以便我可以操作它们.
假设我有queries.txt.
queries.txt:
cat
dog
123
Run Code Online (Sandbox Code Playgroud)
现在我想使用它们是使用grep在myDocument.txt中查找行的查询.
cat queries.txt | xargs grep -f myDocument.txt
Run Code Online (Sandbox Code Playgroud)
myDocument有像这样的行
cat
i have a dog
123
mouse
Run Code Online (Sandbox Code Playgroud)
它应该返回前3行.但事实并非如此.相反,grep尝试将它们作为文件名找到.我究竟做错了什么?