Delete individual elements in a text file in bash

Basically, I have a text file (file.txt) that contains strings of numbers (strings don't necessarily have the same length), for example.


1 2 3 4
5 6 7 8
9 10 11 12 13

What I need to do is write new files with each of these numbers deleted one at a time, with a replacement, for example. the first new file will contain


2 3 4 <--- 1st element deleted
5 6 7 8
9 10 11 12 13

and the 7th file will contain


1 2 3 4
5 6 8 <--- 7th element removed here
9 10 11 12 13

, , . . 7- , , , , ,


$lineNo - 2 ( )
$$ 5 6 7 8
cut, , $newline 5 6 8

$lineNo .txt $newline sed:
sed -n '$ lineNo s/.*/'$ newline'/' > file.txt

.
sed: 25.780000:

( 25.780000 - . , $newline - )
, , : (

: ) , sed, ) sed - , ?

!!

+3
4
filename=file.txt
i=1
while [[ -s $filename ]]; do
    new=file_$i.txt
    awk 'NR==1 {if (NF==1) next; else sub(/^[^ ]+ /, "")} 1' $filename > $new
    ((i++))
    filename=$new
done

, , . , .


- :

words=$(wc -w < file.txt)
for ((i=1; i<=words; i++)); do 
    awk -v n=$i '
        words < n && n <= words+NF {$(n-words) = "" }
        {words += NF; print}
    ' file.txt > file_$i.txt
done
+3

, , , :

#! /bin/bash

remove_by_value()
{
  local TO_REMOVE=$1

  while read line; do 
    out=
    for word in $line; do [ "$word" = "$TO_REMOVE" ] || out="$out $word"; done
    echo "${out/ }"
  done < $2
}

remove_by_position()
{
  local NTH=$1

  while read line; do
    out=
    for word in $line; do
      ((--NTH == 0)) || out="$out $word"
    done
    echo "${out/ }"
  done < $2
}

FILE=$1
shift  
for number; do
  echo "Removing $number"
  remove_by_position $number "$FILE"
done

, , (, remove_by_position $number $FILE > $FILE.$$ && mv $FILE.$$ $FILE.$number ). , ,

$ bash script.sh file.txt $(seq 11)
+3

, , .

#!/bin/bash
#
file=$1
lines=$(cat $file | wc -l) 
out=0

dropFromLine () {
    file=$1
    row=$2
    to=$((row-1))
    from=$((row+1))
    linecontent=($(sed -n "${row}p" $file))
    # echo "    linecontent: " ${linecontent[@]}
    linelen=${#linecontent[@]}
    # echo "    linelength: " $linelen
    for n in $(seq 0 $linelen) 
    do
        ( 
        if [[ $row > 1 ]] ; then sed -n "1,${to}p" $file ;fi
        for i in $(seq 0 $linelen) 
        do
            if [[ $n != $i ]]
            then
                echo -n ${linecontent[$i]}" " 
            fi
        done
        echo 
        # echo "mod - drop " ${linecontent[$n]}
        sed -n "$from,${lines}p" $file 
        ) > outfile-${out}.txt
        out=$((out+1))
    done 
}

for row in $(seq 1 $lines)
do 
    dropFromLine $file $row 
done

:

./dropFromRow.sh num.dat

num.dat:

1 2 3 4
5 6 7 8
9 10 11

:

outfile-0  outfile-10  outfile-12  outfile-2  outfile-4  outfile-6  outfile-8
outfile-1  outfile-11  outfile-13  outfile-3  outfile-5  outfile-7  outfile-9

:

asux:~/proj/mini/forum > cat outfile-0
2 3 4  
5 6 7 8
9 10 11
asux:~/proj/mini/forum > cat outfile-1
1 3 4  
5 6 7 8
9 10 11
+1

: perl:

file.txt:

1 2 3 4
5 6 7 8
9 10 11 12 13

script.pl:

use warnings;
use strict;

## Read all input to a scalar variable as a single string.
my $str;
{
        local $/ = undef;
        $str = <>;
}

## Loop for each number found.
while ( $str =~ m/(\d+)(?:\h*)?/g ) {

        ## Open file for writing. The name of the file will be
        ## the number matched in previous regexp.
        open my $fh, q[>], ($1 . q[.txt]) or
                die qq[Couldn't create file $1.txt\n];

        ## Print everything prior to matched string plus everything
        ## after matched string.
        printf $fh qq[%s%s], $`, $';

        ## Close file.
        close $fh;
}

:

perl script.pl file.txt

:

ls [0-9]*.txt

:

10.txt  11.txt  12.txt  13.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt

:

cat 9.txt

:

1 2 3 4
5 6 7 8
10 11 12 13
0

All Articles