Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to apply sed command on a array variable

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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[@]}"

>Solution :

Like this:

$ array=(/home/peter/my_commands/func1.sh /home/peter/my_commands/main.sh /home/peter/my_commands/test.sh)
$ array2=( "${array[@]##*/}" )
$ printf '%s\n' "${array2[@]}"
func1.sh
main.sh
test.sh

Using bash parameter expansion

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading