我正在尝试通过删除重复的空行来压缩文本文档,并将sed. 这就是我正在做的事情(无济于事):
sed -i -E 's/\n{3,}/\n/g' file.txt
Run Code Online (Sandbox Code Playgroud)
我知道根据本手册,这是不正确的,但我不知道如何正确执行。谢谢。
我试图删除文本字符串中除剩余 11 个字符之外的每个字符。字符串是Sample Text_that-would$normally~be,here--pe_-l4_mBY,我想要结束的只是-pe_-l4_mBY.
这是我尝试过的:
$ cat food
Sample Text_that-would$normally~be,here--pe_-l4_mBY
$ cat food | sed 's/^.*(.{3})$/\1/'
sed: 1: "s/^.*(.{3})$/\1/": \1 not defined in the RE
Run Code Online (Sandbox Code Playgroud)
请注意,文本字符串并没有真正存储在文件中,我只是用作cat food示例。
操作系统为 macOS High Sierra 10.13.6,bash版本为 3.2.57(1)-release
我有一个包含以下内容的文件:
set x 0.00456 y 0.05896.
Run Code Online (Sandbox Code Playgroud)
我想将数字乘以固定数量(比方说 1000)。这些数字并不总是存在于同一列中,因此任何带有 awk 的内容都不会出现。我一直在尝试这个,但不确定我使用子匹配的方式是否正确。
%s/ \d*\.\d*/\=submatch(2)*100
Run Code Online (Sandbox Code Playgroud) 将变量的范围限制$x 为特定的代码块或子例程,通过my $x将编码员从“全局变量”引起的混乱世界中解救出来。
但是当涉及到输入记录分隔符时$/,显然它的范围是不能被限制的。我在这方面正确吗?
因此,如果我忘记在循环结束时或在子例程内重置输入记录分隔符,则调用子例程下方的代码可能会产生意想不到的结果。以下示例演示了这一点。
#!/usr/bin/perl
use strict; use warnings;
my $count_records; my $infile = $ARGV[0]; my $HANDLEinfile;
open $HANDLEinfile, '<', $infile or die "cannot open $infile for reading";
$count_records = 0;
while(<$HANDLEinfile>)
{
$count_records++;
print "$count_records:\n";
print;
}
close $HANDLEinfile;
look_through_other_file();
print "\nNOW, after invoking look_through_other_file:\n";
open $HANDLEinfile, '<', $infile or die "cannot open $infile for reading";
$count_records = 0;
while(<$HANDLEinfile>)
{
$count_records++;
print "$count_records:\n";
print;
}
close $HANDLEinfile;
sub look_through_other_file
{ …Run Code Online (Sandbox Code Playgroud) 我的文件中没有任何数据 需要检查以下场景并返回文件为空,否则不为空
if file contains no data but as only spaces return it as FILE is EMPTY
if file contains no data but as only tabs return it as FILE is EMPTY
if file contains no data but as only empty new line return it as FILE is EMPTY
Run Code Online (Sandbox Code Playgroud)
下面的代码是否能满足我上述所有情况?或一次性使用任何最佳方法
if [ -s /d/dem.txt ]
then
echo "FILE IS NOT EMPTY AS SOME DATA"
else
echo "FILE IS EMPTY NOT DATA AVAILABLE"
fi
Run Code Online (Sandbox Code Playgroud) 我正在写一段代码,它将从正在运行的文本中提取单词.此文本可以包含\ r,\n等分隔符,这些分隔符可能位于文本中.
我想丢弃所有这些分隔符,只提取完整的单词.我怎么能用Python做到这一点?任何可用于在python中处理文本的库?
我正在使用开源perl脚本来创建基于英语维基百科转储的文本语料库.已经提取了纯文本,但是仍然需要删除各种标点符号等.但是,此脚本的输出实际上创建了一个包含单行的7.2GiB文本文件.由于我的需要,我想改变脚本,使其每20个字插入一个新的行字符.
到目前为止,我试过这个:
$wordCount=0;
while (<STDIN>) {
$wordCount++;
//text processing regex commands here
# Remove ellipses
s/\.\.\./ /g;
# Remove dashes surrounded by spaces (e.g. phrase - phrase)
s/\s-+\s/ /g;
# Remove dashes between words with no spaces (e.g. word--word)
s/([A-Za-z0-9])\-\-([A-Za-z0-9])/$1 $2/g;
# Remove dash at a word end (e.g. three- to five-year)
s/(\w)-\s/$1 /g;
# Remove some punctuation
s/([\"\?,;:%???!()\[\]{}<>_\.])/ /g;
# Remove trailing space
s/ $//;
# Remove double single-quotes
s/'' / /g;
s/ ''/ /g;
# Replace accented e …Run Code Online (Sandbox Code Playgroud) 我有以下字符串:
"Gender and Women's Studies"
Run Code Online (Sandbox Code Playgroud)
我想将其转换为:
"Gender\ and\ Women\'s\ Studies"
Run Code Online (Sandbox Code Playgroud)
这样我就可以使用该字符串创建一个有效的Unix文件夹.
我将如何在Ruby中解决这个问题?
我有一个包含数千个条目的xml文件,例如:
<gml:featureMember>
<Feature>
<featureType>JCSOutput</featureType>
<property name="gml2_coordsys"></property>
<gml:PointProperty>
<gml:Point>
<gml:coordinates>4048313.294966287,5374397.792158723 </gml:coordinates>
</gml:Point>
</gml:PointProperty>
<property name="BEZEICHNUN">Anton-Bosch-Gasse</property>
<property name="WL_NUMMER">68</property>
</Feature>
</gml:featureMember>
<gml:featureMember>
<Feature>
<featureType>JCSOutput</featureType>
<property name="gml2_coordsys"></property>
<gml:PointProperty>
<gml:Point>
<gml:coordinates>4044355.0231338665,5365146.95116724 </gml:coordinates>
</gml:Point>
</gml:PointProperty>
<property name="BEZEICHNUN">Anschützgasse</property>
<property name="WL_NUMMER">67</property>
</Feature>
</gml:featureMember>
Run Code Online (Sandbox Code Playgroud)
脚本应该搜索列表中给出的名称(例如Anton-Bosch-Gasse)并将整个段落复制<gml:featureMember>到一个新文件
你会为此目的使用什么 - awk,sed,perl?
我正在尝试找到一种使用nltk模块在Python中拆分单词的方法。考虑到我拥有的原始数据(例如带符号词的列表),我不确定如何达到我的目标
['usingvariousmolecularbiology', 'techniques', 'toproduce', 'genotypes', 'following', 'standardoperatingprocedures', '.', 'Operateandmaintainautomatedequipment', '.', 'Updatesampletrackingsystemsandprocess', 'documentation', 'toallowaccurate', 'monitoring', 'andrapid', 'progression', 'ofcasework']
Run Code Online (Sandbox Code Playgroud)
如您所见,许多单词被粘在一起(即“ to”和“ produce”被粘在一个字符串“ toproduce”中)。这是从PDF文件中抓取数据的一种人工产物,我想找到一种使用python中的nltk模块将卡住的单词拆分(即,将“ toproduce”拆分为两个单词:“ to”和“ produce”的方法);将“ standardoperatingprocedures”分为三个词:“ standard”,“ operating”,“ procedures”)。
感谢您的帮助!
python text-processing text-analysis nltk python-textprocessing