I have a file that contains multiple instances of code that I need to delete. Here is an example:

mkdir /rootdir/pipeline_runs/oncology/analysis/sample_1_NA172_1
cd /rootdir/pipeline_runs/oncology/analysis/sample_1_NA172_1
ln -s ../oncology/importantFile1 importantFile1
ln -s ../oncology/importantFile2 importantFile2

mkdir /rootdir/pipeline_runs/oncology/analysis/sample_2_NA172_2
cd /rootdir/pipeline_runs/oncology/analysis/sample_2_NA172_2
ln -s ../oncology/importantFile1 importantFile1
ln -s ../oncology/importantFile2 importantFile2

mkdir /rootdir/pipeline_runs/oncology/analysis/sample_3_NA172_3
cd /rootdir/pipeline_runs/oncology/analysis/sample_3_NA172_3
ln -s ../oncology/importantFile1 importantFile1
ln -s ../oncology/importantFile2 importantFile2

In reality, there can be 16 to 30 of these in this script. I need to be able to sed into this file and search a given sample(e.g. sample_1_NA172_1) and delete the mkdir line and the 13 lines that follow it for all samples but this one. There are some cases where I will need to keep the code snippet for more than one sample, but to start with I'm trying to make just one work.


# This pulls out the first line of each mkdir snippet along with the 
# sample name.
mkdirList=$(grep -E mkdir $fileToEdit)

# Removes the mkdir from output and cuts 
# all the dir path leaving just the sample name
sample=$(echo $mkdirList | sed 's/mkdir //g' | tr ' ' '\n' | cut -d/ -f14-)

printf "\n"
echo "Which sample(s) would you like to keep?"
printf "\n"

# Dynamic Menu Function
createmenu () {
select selected_option; do # in "$@" is the default
    if [ 1 -le "$REPLY" ] && [ "$REPLY" -le $(($#)) ]; then
        echo "Please make a vaild selection (1-$#)."

declare -a tsample=();

# Load Menu by Line of Returned Command
mapfile -t tsample < <(echo $sample | tr ' ' '\n');

# Display Menu and Prompt for Input
echo "(Please select one):";
# This generates a dynamic numbered menu list of all the samples.
# Currently it allows the user to choose one sample to keep.
# Eventually I'd like to allow them to choose multiple samples. 
createmenu "${tsample[@]}"

# This is the sample that was chosen
tsample=($echo "${selected_option}");

# This greps all the samples BUT the chosen sample and makes it a variable.
rsample=$(echo $sample | tr ' ' '\n' | grep -v $tsample)

# This is my attempt to make an array out of the remaining samples
# that need to be deleted, and then sed search/delete them from the script.
declare -a array=( "echo $rsample" )
for i in "${!array[*]}"
            sed -i '/mkdir.*$i/,+13 d' $fileToEdit

I have verified that I can successfully use:

sed -i /mkdir.*sample_1_NA172_1/,+13 d'

I think my array is ok. I think my problem is using $i next to a "*" inside a sed search field.


  1. I'm trying to make sed work with a wildcard against an array.
  2. I'd like to get this where it can accept multiple samples to preserve.

1 Answer 1


Does changing the single quote to double quotes like below works?:

sed -i "/mkdir.*$i/,+13 d" $fileToEdit

What about :

 sed -i "/mkdir.*${i}/,+13 d" $fileToEdit
  • Yes, the double quotes made the difference! Thanks Demeter!
    – Don Story
    Nov 30, 2018 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.