Posted: . At: 9:12 AM. This was 5 years ago. Post ID: 13699
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



Interesting bash scripting trick. Delete lines from a file.


This is an interesting bash scripting trick. This allows deleting the first 10 lines from a text file, but keeping the rest of the file instead of deleting it.

X=10
file="file.txt"
# read the file, line by line
while read -r line; do
    # add current line to array
    # (you can modify this to do whatever you want)
    links+=($line)
    # delete first line in file
    sed -i "1,1 d" $file
    # decrement X
    : $((X--))
    # if it reached 0, break out of the loop
    ((X<1)) && break
done < $file

Use it on a text file and then it will work on the file, but not delete it. I guess this could be useful, iterating through files and deleting the contents. So this is a good example of bash scripting.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.