bashline

Bash: pop lines in a file


I need to read a file with bash and remove the request line? Some similar to pop/push functions.

How can i do it?


Solution

  • You can use sed to delete specific line numbers or lines that match a given pattern:

    $ cat file
    line 1
    line 2 
    line 3 
    line 4
    line 5
    
    $ sed -i '3d' file             # Delete the 3rd line
    line 1
    line 2 
    line 4
    line 5    
    
    $ sed -i '/^line 4$/d' file    # Delete the line that matches the pattern
    line 1
    line 2 
    line 3 
    line 5
    
    
    $ sed '$d' file                # Delete the last line in the file
    line 1
    line 2 
    line 3 
    line 4
    

    You will not be able to push these lines back however..