The result of my command is :
> htnnpjhuhj.jbjbnljnkn:tcp:ssh xxx.12234.r444:44056
> ftpddddddd.jbfffdnkn:tcp:ssh xxx.122555.r674:44067
I am trying to get only the first element in each line before the delimiter ":" That is, I am trying to get htnnpjhuhj.jbjbnljnkn
and ftpddddddd.jbfffdnkn
. Please tell me what is wrong with my code below.
function myfunc
{
command=$(command)
IFS=":" read -rA RESULTS <<< $command
echo ${RESULTS[0]}
}
Assuming the desired output looks like:
htnnpjhuhj.jbjbnljnkn
ftpddddddd.jbfffdnkn
There are several ways to accomplish this ... cut
, sed
, awk
... the array-based solution mentioned in the question.
It's not clear (to me) how the function is going to be used going forward so I'm going to look at something a bit more direct.
Here's an awk
solution that parses the input based on one of two possible delimiters ( space
and :
):
$ cat rawdata
> htnnpjhuhj.jbjbnljnkn:tcp:ssh xxx.12234.r444:44056
> ftpddddddd.jbfffdnkn:tcp:ssh xxx.122555.r674:44067
$ awk -F"[ :]" '{print $2}' rawdata
htnnpjhuhj.jbjbnljnkn
ftpddddddd.jbfffdnkn
If the intention is to place all of the values on a single line then we can add a bit more to the awk
code like such:
$ awk -F"[ :]" '{printf $2" "} END {printf "\n"}' rawdata
htnnpjhuhj.jbjbnljnkn ftpddddddd.jbfffdnkn
NOTE : This does place an extra space on the end of the output.
Again, plenty of ways to slice-n-dice this but we'll need more details from the OP as to the desired output and if/how the output will be used later on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.