如何删除X个字符后的所有单词 [英] How To Delete All Words After X Characters
问题描述
我阅读了这篇文章: sed删除行中剩余的字符除了前5个,它可以帮助我删除x之后的所有字符.但是,我很难找到如何删除x个字符后的所有单词.
I read this post: sed delete remaining characters in line except first 5 which helps me to delete all characters after x. However, I'm having a hard time finding how to delete all words after x characters.
我从以下代码开始:
echo "StackOverflow Users Are Brilliant And Hard Working" | sed 's/.//30g'
#character 30 ---------------------^
我的尝试:
echo "StackOverflow Users Are Brilliant And Hard Working" | sed 's/ .* //30g'
#character 30 ---------------------^
在这些输出中,我截断了最后一个单词或对单词进行计数.相反,我需要删除30个字符之后的单词.我在各种行/单词长度上运行此操作,因此这就是为什么我不能仅将其设置为单词的末尾.
In these outputs, I either cut off the last word or counting words. Instead, I need to remove the words after 30 characters. I am running this on various lines/word lengths so that's why I can't just set it to the end of the word.
所需的输出:
StackOverflow Users Are Brilliant
如果您知道如何将x个字符后的单词数加在一起,将非常感谢您的帮助.
If you know how to put together counting the words after x characters, your help would be greatly appreciated.
请注意:如前所述,请勿将代码更改为33或34个字符.问题的关键是要删除30个字符后的所有单词.
Please note: As stated earlier, do not change the code to 33 or 34 characters. The point of the question is to remove all WORDS after 30 characters.
推荐答案
此 awk 可以完成
$ awk 'BEGIN{FS=OFS="" } length>30{i=30; while($i~/\w/) i++; NF=i-1; }1' file
StackOverflow Users Are Brilliant
This line has 22 chars
设置FS=OFS=""
,以便将每个字符视为一个字段
Setting FS=OFS=""
so that each char is considered as a field
如果length>30
,则i=30; while($i~/\w/) i++;
,即继续递增i
,直到我们降到非数字字符为止;循环结束后,设置所需的NF
.
If length>30
then i=30; while($i~/\w/) i++;
i.e keep incrementing i
until we land at a non-alnum character; Once loop ends set the desired NF
.
带有length<=30
的行将原样打印.
使用 grep
$ grep -oE "^.{1,29}\w*" file
StackOverflow Users Are Brilliant
This line has 22 chars
^.{1,29}\w*
:1
到29
,因为如果30th
char是非数字的,则不应考虑它.
^.{1,29}\w*
: 1
to 29
because if 30th
char is non-alnum then it shouldn't be considered.
这篇关于如何删除X个字符后的所有单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!