2

I'm trying to remove some specific lines from a file. I've an index array that keeps which lines will be removed. I've used the following command for this issue. But, it removes only the one line which is pointed to by the first member of the index array. I can't delete the rest of the lines. Is there any problem with the command?

**sed -i ${index_array}'d' $file_name**
phs
  • 10,687
  • 4
  • 58
  • 84
metlira
  • 873
  • 1
  • 8
  • 12

4 Answers4

1

This might work for you (GNU sed & BASH):

sed -i ''"${index_array[*]/%/d;}"'' file

or:

sed -i -f - <<<"${index_array[@]/%/d;}" file

or:

echo "${index_array[@]/%/d;}" | sed -i -f - file
potong
  • 55,640
  • 6
  • 51
  • 83
0

It should be:

for i in "${index_array[@]}"
do
    sed -i "${i}d" "$filename"
done
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
0
for line in "${index_array[@]}"; do sed_script+=" -e '/^$line\$/d'"; done
sed -i "$sed_script" file
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0
nums=$(echo ${index_array[@]} | sed 's/ /\\|/')

sed = input_file | sed '
{
N
s/\n/ /
}
' | sed "/^$nums /d" | sed 's/^[0-9]* //'
perreal
  • 94,503
  • 21
  • 155
  • 181