简体   繁体   中英

remove last N lines from file bash

i am trying to remove some text from a file, i need to find the line number using a string and then remove from that line to the end of the file.

i tried using sed and outputting to a new file using a string as a search term but it does not keep the lines, it just prints the whole file on one line, which isnt helpful as the rest of the script relies on line numbers. my code so far is:

file=$(sed -ie '/<div class="col-wrapper">/,$d' < file1.txt)  
echo $file > file.txt

You can just use inline editing in sed :

sed -i '/<div class="col-wrapper">/,$d' file1.txt

to save changes to file1.txt

这可能适合你(GNU sed):

sed -i '/<div class="col-wrapper">/Q' file1.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM