【问题标题】:how to apply sed command on a array variablehow to apply sed command on a array variable
【发布时间】:2022-12-02 03:13:17
【问题描述】:

The primary goal is to know how to use the sed command on a array-variable rather then a file.

Secondary is to remove the path out of the array, the final output should contain only the files names

sample data:

path=$(pwd) 
echo $path
>> /home/peter/my_commands

array=($path/*.sh)
echo "${array[@]}"
>> /home/peter/my_commands/func1.sh /home/peter/my_commands/main.sh /home/peter/my_commands/test.sh

desired output:

func1.sh  main.sh  test.sh

this values inside of array-variable called array2

summarizing:

#input data
array=(/home/peter/my_commands/func1.sh /home/peter/my_commands/main.sh /home/peter/my_commands/test.sh)

#sed command to make the transformation
#HERE..

#output will be
array2=(func1.sh  main.sh  test.sh)

failed attempts:

# sed -i -e 's.$path..'
# array2=$(sed -n '2p' $path)
# array2=(($path/*.sh) | sed -i "s.$path..")
# array2="$(echo $array | sed -i -e "s.$path..g")"
# array2=$(sed -i -e "s./.." <<< "$array")

echo "${array2[@]}"

【问题讨论】:

  • Not very clear question
  • let me edit the question @GillesQuenot

标签: bash sed sh


【解决方案1】:

Like this:

$ printf '%s
' "${array[@]##*/}"
func1.sh
main.sh
test.sh

Using bashparameter expansion

【讨论】:

  • question is updated
猜你喜欢
  • 2022-12-27
  • 2022-12-02
  • 2022-12-26
  • 2022-12-02
  • 1970-01-01
  • 2022-12-01
  • 2011-09-22
  • 2022-12-27
  • 2022-12-02
相关资源
最近更新 更多