假设在linux中有一个文件,它的行用空格分隔。
例如:
This is linux file
This is linux text
This is linux file 1
This is linux file 3
现在我只想打印那些在文件行中有第5列的行。在本例中,我的输出应该是第3行和第4行(其中1和3作为第5列)
做这件事最好的方法是什么?
我有下面的脚本,我想用它来比较一个float和另一个给定的float。
#!/bin/sh
echo 'Enter a real number'
read n
echo n=$n
if (( $(echo "$n > 0.0" |bc -l) ))
then
echo 'n is +ve'
elif (( $(echo "$n < 0.0" |bc -l) ))
then
echo 'n is -ve'
else
echo 'n is zero'
fi
这在我的
File_A
Name: John Smith
Grade: 8
Institute: Baldwin
Number of entries: 125
State: Texas
File_B
Name: David Buck
Grade: 9
Institute: High Prime
Number of entries: 123
State: California
在许多类似的文件中,条目的数量(在所有文件中都位于第4行)增加了一倍。
对于File_A,它应该是250,对于File_B 246。
如何对Linux中的所有文件执行此操作?(使用sed或awk或任何其他命令)
尝试命令:
sed
我正在尝试确定哪个应用程序正在使用某个端口并获取netstat -tlnp | grep <port> for Linux。
此命令返回以下输出:
(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:<port> 0.0.0.0:* LISTEN 3591/java
我只需要在结果中获得进程名和PID,即java3591。
做这件事
在Linux中,我有很多文件,我需要将所有文件第n行的第m个单词连同文件名一起复制粘贴到一个普通的.txt文件中。所以我的最终文本文件看起来有点像这样...
<FileName1> <mth word of nth line of FileName1>
<FileName2> <mth word of nth line of FileName2>
.
.
<FileNameN> <mth word of nth line of FileNameN>
有没有人可以告诉我Linux的命令是什么?谢谢你!!
我需要循环环境变量,并在POSIX sh (而不是bash)中获取它们的名称和值。这就是我到目前为止所拥有的。
#!/usr/bin/env sh
# Loop over each line from the env command
while read -r line; do
# Get the string before = (the var name)
name="${line%=*}"
eval value="\$$name"
echo "name: ${name}, value: ${value}"
done <
下面有一个名为emoji.txt的文件: ? Human 1 Disconnected
VPN1 Human 2 Disconnected
? Human 3 Is Connected
VPN2 Human 4 Connected
? Human 5 Disconnected
? Human 6 Connected
VPN4 Human 7 Disconnected
? Human 8 Connected 然后我希望得到这样的结果: ? Human 1 Disconnected
? Human 3 Is Connected
? Human 5 Disconnected
? Human
样本文本
I like Linux
I like Microsoft
I like Solaris
I hate Linux
I hate Microsoft
I hate Solaris
我试图找出一个与regex X匹配但没有regex的行。使用示例文本,我试图得到以下内容。
I like Linux
I like Microsoft and Linux
I like Solaris and Linux
I hate Linux
I hate Microsoft
I hate Solaris
请注意,它只添加到有like而没有Linux的行中。