Note that there are some explanatory texts on larger screens.

plurals
  1. POremove single elements in a text file in bash
    primarykey
    data
    text
    <p>Basically what I have is a text file (file.txt), which contains lines of numbers (lines aren't necessarily the same length) e.g.</p> <p><br>1 2 3 4 <br>5 6 7 8 <br>9 10 11 12 13</p> <p>What I need to do is write new files with each of these numbers deleted, one at a time, with replacement e.g. the first new file will contain</p> <p><br>2 3 4 &lt;--- 1st element removed <br>5 6 7 8 <br>9 10 11 12 13</p> <p>and the 7th file will contain</p> <p><br> 1 2 3 4 <br> 5 6 8 &lt;--- 7th element removed here <br> 9 10 11 12 13</p> <p>To generate these, I'm looping through each line, and then each element in each line. E.g. for the 7th file, where I remove the third element of the second line, I'm trying to do this by reading in the line, removing the appropriate element, then reinserting this new line</p> <p><br>$lineNo is 2 (second line) <br>$line is 5 6 7 8 <br>with cut, I remove the third number, making $newline 5 6 8</p> <p>Then I try to replace the line $lineNo in file.txt with $newline using sed: <br>sed -n '$lineNo s/.*/'$newline'/' > file.txt</p> <p>This is totally not working. I get an error <br>sed: can't read 25.780000: No such file or directory<br> <br>(where 25.780000 is a number in my text file. It looks like it's trying to use $newline to read files or something) <br>I have reason to suspect my way of stating which line to replace isn't working either :(</p> <p>My question is, a) is there a better way to do this rather than sed, and b) if sed is the way to go, what am I doing wrong?</p> <p>Thanks!! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload