文件中带有注释的所有行都以#
.如何删除以#
?开头的所有行(以及只有那些行)?#
应忽略包含但不在行开头的其他行.
有时在制作条件时,我需要代码什么都不做,例如,在这里,我希望Bash在$a
大于"10" 时什么也不做,如果$a
小于"5" 则打印"1 ",否则,打印"2":
if [ "$a" -ge 10 ]
then
elif [ "$a" -le 5 ]
then
echo "1"
else
echo "2"
fi
Run Code Online (Sandbox Code Playgroud)
这会产生错误.是否有一个命令什么都不做,也不会减慢我的脚本?
我rm
在BASH脚本中使用删除许多文件.有时文件不存在,因此会报告许多错误.我不需要这条消息.我在man页面搜索了一个让rm
安静的命令,但我找到的唯一选项是-f
,从描述中"忽略不存在的文件,从不提示",似乎是正确的选择,但名称似乎不合适所以我担心它可能会产生意想不到的后果.
-f
选择沉默的正确方法rm
?为什么不叫它-q
?我有一个Bash变量,$word
有时候是一个单词或句子,例如:
word="tiger"
Run Code Online (Sandbox Code Playgroud)
要么:
word="This is a sentence."
Run Code Online (Sandbox Code Playgroud)
如何创建一个新的Bash变量,该变量仅等于变量中的第一个字母?例如,以上将是:
echo $firstletter
t
Run Code Online (Sandbox Code Playgroud)
要么:
echo $firstletter
T
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的变量:
words="??????"
Run Code Online (Sandbox Code Playgroud)
我想打一个for循环的每个字符,一次一个,例如第一的character="?"
,那么character="?"
,character="?"
等
我知道的唯一方法是将每个字符输出到文件中的单独行,然后使用while read line
,但这似乎非常低效.
我经常在Bash中使用管道,例如:
dmesg | less
Run Code Online (Sandbox Code Playgroud)
虽然我知道这是什么输出,它需要dmesg
并让我滚动它less
,我不明白|
它在做什么.它恰恰相反>
吗?
|
?如何在文件的每一行的开头添加数字?
例如:
This is the text from the file.
变为:
000000001 This is 000000002 the text 000000003 from the file.
我有一个包含这样的表的文档:
<table>
<tr>
<td>
Word
</td>
<td>
Definition
</td>
</tr>
<tr>
<td>
Word
</td>
<td>
Definition
</td>
</tr>
<tr>
<td>
Word
</td>
<td>
Definition
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
使用CSS,我需要将所有单元格设置为50%宽度,左侧"列"中的文本右对齐,左列中的文本左对齐.我尝试了很多不同的选择,例如width: 50%; text-align: left
,结果总是不寻常的.结果应如下所示:
_________________________
| word | definition |
|____________|____________|
| word | definition |
|____________|____________|
Run Code Online (Sandbox Code Playgroud)
如何在CSS中的两列表中设置等宽单元格,并将所有内容对齐到中间?
如果在一串文本中至少找到一次特定的匹配文本,我需要创建一个条件,例如:
str = "This is some text containing the word tiger."
if string.match(str, "tiger") then
print ("The word tiger was found.")
else
print ("The word tiger was not found.")
Run Code Online (Sandbox Code Playgroud)
如何检查文本是否在字符串中的某处找到?
我有这样一个文件:
This is a file with many words.
Some of the words appear more than once.
Some of the words only appear one time.
Run Code Online (Sandbox Code Playgroud)
我想生成一个两列列表.第一列显示出现的单词,第二列显示出现的频率,例如:
this@1
is@1
a@1
file@1
with@1
many@1
words3
some@2
of@2
the@2
only@1
appear@2
more@1
than@1
one@1
once@1
time@1
Run Code Online (Sandbox Code Playgroud)
words
并且word
可以算作两个单独的单词.到目前为止,我有这个:
sed -i "s/ /\n/g" ./file1.txt # put all words on a new line
while read line
do
count="$(grep -c $line file1.txt)"
echo $line"@"$count >> file2.txt # add word and frequency to …
Run Code Online (Sandbox Code Playgroud) bash ×8
conditional ×2
sed ×2
css ×1
file-io ×1
for-loop ×1
grep ×1
html ×1
html-table ×1
lua ×1
no-op ×1
pipe ×1
rm ×1
row-number ×1
string ×1