Search code examples
linuxunixsedviin-place

Linux- Add/Insert in only the secondlast line with a new value keeping original content intact in same file


I need to add/insert a value "new content" on the secondlast line of the same file without deleting any of the lines or content of my file.

The condition is that the file content keeps changing and line numbers do not remain fixed.

Sample file:

library_path="usr/lib/1.0"
if test -n "/usr/local/lib64"; then
: ${PATH=${HOME}/lib:/usr/local/lib64}
else
: ${L_PATH=${HOME}/lib}
fi
if test -n "/usr/local/libs"; then
: ${PATH=${HOME}/lib:/usr/local/}
fi
##comment1
#comment2
##comment3
#comment4
if vals"${PATH}"; then
  PATH="${L_PATH}"
else
  PATH="${LD_PATH}:${Y_PATH}"
fi
export LD_PATH

I am trying the below command but its inserting the content on every line of the file:

sed -i 'x; s/.*/new content/;p;x; $d' myfile.txt

Any help appreciated !!


Solution

  • To insert "new content" line as the second-to-last line into the file, use the Perl one-liner below. Example of usage:

    echo "foo\nbar\nbaz" > in_file
    perl -i.bak -ne 'push @a, $_; if ( eof ) { print for ( @a[0 .. ($#a-1)], "new content\n", $a[-1] ); }' in_file
    

    Input:

    foo
    bar
    baz
    

    Output:

    foo
    bar
    new content
    baz
    

    The Perl one-liner uses these command line flags:
    -e : Tells Perl to look for code in-line, instead of in a file.
    -n : Loop over the input one line at a time, assigning it to $_ by default.
    -i.bak : Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak.

    push @a, $_; : Add the current line that was read from the input file into array @a. Thus, at the end of the file (eof), @a has the entire input file contents, 1 line per array element.
    @a[0 .. ($#a-1)] : Lines 1 through the next-to-last line (inclusive). Note that arrays in Perl are 0-indexed.
    $a[-1] : Last line.

    SEE ALSO:
    perldoc perlrun: how to execute the Perl interpreter: command line switches