将awk结果存储为bash $ {}数据格式

时间:2022-09-03 16:57:29

i got a question about bash and awk, so, i'm going to filter the processes list from ps aux then i did filtering by grep, then i filter it again using awk to display only the pid and the path of the process, then save it to the corresponding ${pid} and ${path} field. the question is, when i done filtering the results using awk, i'm going to save those results on ${pid} for pid numbers and ${path} for process's path, but i got no idea at all on doing that thing. if anyone here have a solution it would be very appreciated.

我有一个关于bash和awk的问题,所以,我要从ps aux过滤进程列表然后我用grep过滤,然后我再次使用awk过滤它来显示pid和进程的路径,然后将其保存到相应的$ {pid}和$ {path}字段。问题是,当我使用awk过滤结果时,我将把这些结果保存在$ {pid}的pid数字和$ {path}的进程路径上,但我完全不知道做那件事。如果有人在这里有一个解决方案,将非常感激。

Thanks

谢谢

EDIT: here is the code

编辑:这是代码

ps aux | grep 'firefox' | awk '{print $2 " " $11}'

then i don't know what to do to save the $2 content to ${pid} and $11 to ${path} and save those fields to the txt file again...

然后我不知道如何将$ 2内容保存到$ {pid}和$ 11到$ {path}并将这些字段再次保存到txt文件中...

4 个解决方案

#1


0  

If all else fail remember that you have the disk that you can use. Pipe the result to a file and then process the file using awk or cut two times to assign the fields to the corresponding variables.

如果所有其他方法都失败,请记住您拥有可以使用的磁盘。将结果传递给文件,然后使用awk处理文件或剪切两次以将字段分配给相应的变量。

For example:

例如:

$result > tmp
pid=`cat tmp | cut -f 1`
path=`cat tmp | cut -f 2`
rm tmp

#2


1  

This should do it:

这应该这样做:

read pid path <<< $(ps aux | grep 'firefox' | awk '{print $2 " " $11}')

#3


0  

use set :

使用集:

out=$(ps aux | grep 'nginx' | awk '{print $2 " " $11}' )
set $out
pid=$1
path=$2
echo $pid
echo $path

#4


0  

awk_output=($(ps aux | awk '/firefox/{print $2, $11}'))
pid="${awk_output[0]}"
path="${awk_output[1]}"

If your paths can contain spaces you'll need a different solution involving setting IFS.

如果您的路径可以包含空格,则需要一个涉及设置IFS的不同解决方案。

#1


0  

If all else fail remember that you have the disk that you can use. Pipe the result to a file and then process the file using awk or cut two times to assign the fields to the corresponding variables.

如果所有其他方法都失败,请记住您拥有可以使用的磁盘。将结果传递给文件,然后使用awk处理文件或剪切两次以将字段分配给相应的变量。

For example:

例如:

$result > tmp
pid=`cat tmp | cut -f 1`
path=`cat tmp | cut -f 2`
rm tmp

#2


1  

This should do it:

这应该这样做:

read pid path <<< $(ps aux | grep 'firefox' | awk '{print $2 " " $11}')

#3


0  

use set :

使用集:

out=$(ps aux | grep 'nginx' | awk '{print $2 " " $11}' )
set $out
pid=$1
path=$2
echo $pid
echo $path

#4


0  

awk_output=($(ps aux | awk '/firefox/{print $2, $11}'))
pid="${awk_output[0]}"
path="${awk_output[1]}"

If your paths can contain spaces you'll need a different solution involving setting IFS.

如果您的路径可以包含空格,则需要一个涉及设置IFS的不同解决方案。