Sed delete from line number to end of file
Active 7 years, 8 months ago. Viewed 12k times. Improve this question. Tom Klino. Tom Klino Tom Klino 2 2 gold badges 10 10 silver badges 23 23 bronze badges. Add a comment. Active Oldest Votes. Improve this answer.
Gilles 'SO- stop being evil' Gilles 'SO- stop being evil' k gold badges silver badges bronze badges. Otherwise print the line unchanged. So if I understand correctly, next is used to have awk look at the next line, and '1' is used to print it as is? Gnouc thanks, I know, it was left over from my testing where I was print ing explicitly. By the way, you have more than enough rep to edit, please feel free to just delete it if I do something silly like that again.
If you want to delete lines from 5 through 10 and line 12th: sed -e '5,10d;12d' file This will print the results to the screen. If you want to save the results to the same file: sed -i. Note: Line numbers start at 1. The first line of the file is 1, not 0. Brian Campbell Brian Campbell k 56 56 gold badges silver badges bronze badges. Not all unixes have gnu sed with "-i". KanagaveluSugumar sed -e '5d' file. Note that the lines from the 5th to 10th are all inclusive. Show 6 more comments.
You can delete a particular single line with its line number by sed -i '33d' file This will delete the line on 33 line number and save the updated file. In my case "sed" removed a wrong line. Same here, I wrote a loop and strangely some files lost the correct line but some files lost one other line too, have no clue what went wrong. Be really careful to use sort -r if you are deleting from a list of lines, otherwise your first sed will change the line numbers of everything else!
To comments about wrong lines being deleted within a loop : be sure to start with the largest line number, otherwise each deleted line will offset the line numbering… — Skippy le Grand Gourou. Note that this doesn't delete anything in the file.
It just prints the file without these lines to stdout. So you also need to redirect the output to a temp file, and then move the temp file to replace the original. Matthew Slattery Matthew Slattery For example; grep -nh error logfile cut -d: -f1 deletelines logfile where deletelines is the utility you are imagining you need is the same as grep -v error logfile Having said that, if you are in a situation where you genuinely need to perform this task, you can generate a simple sed script from the file of line numbers.
I don't quite agree with "symptom of an antipattern". Markup-based file types e. In that case, it's often the most reasonable approach to remove those lines, put into the file what you want to be added and then re-add those lines, because putting the lines in between straight away can be much more effort, and goes against the potential desire to avoid extra tools like sed as much as you can.
I don't quite understand what sort of scenario you are imagining. There are scenarios where this is a legitimate approach but the vast majority of cases I have seen are newbies who do more or less exactly what my first example demonstrates. Perhaps they come from some really low-level language and are used to dividing their problem way past the molecular level, because you have to in asm or C. What I basically mean by that, is that as the creator of such a file, you know what has to be at the end of the document i.
Being aware of that, the most simple approach to extend such a document is 1 remove the last few lines, 2 add the new content, 3 re-add the last few lines.
This way, the document can be valid both before and after it has been extended, without needing to find a way of adding lines mid-document. The sed command removes the lines from m to n in the file. Delete lines other than the first line or header line Use the negation! The following sed command removes all the lines except the header line.
Delete first and last line You can specify the list of lines you want to remove in sed command with semicolon as a delimiter. However, this sed do not remove the lines that contain spaces. Sed Command to Delete Lines - Based on Pattern Match In the following examples, the sed command deletes the lines in file which match the given pattern.
Above sed command removes all the lines that start with character 'u'. The above command deletes all the lines that end with character 'x'. Note : In all the above examples, the sed command prints the contents of the file on the unix or linux terminal by removing the lines.
However the sed command does not remove the lines from the source file. To Remove the lines from the source file itself, use the -i option with sed command. Tags Unix. You might like Show more. Unknown 28 April, Vijay Bhaskar 28 April, Unknown 29 April, Unknown 26 October,
0コメント