Sed - Piping a String Before the Last Line in a File

sed - Piping a string before the last line in a file

this should do the trick:

 sed -i "\$i $(cmd)" file

test:

kent$  cat f
1
2
3
4
5

kent$ sed -i "\$i $(date)" f

kent$ cat f
1
2
3
4
Tue Sep 30 14:10:02 CEST 2014
5

Sed syntax to extract all of text BEFORE last delimiter?

You can use a negated character class in your regex:

sed 's/-[^-]*$//' <<< 'scanning-client-container-0.2.tar'

scanning-client-container

RegEx Details:

  • -: Match a -
  • [^-]*: Match 0 or more characters that are not -
  • $: Match end

How to insert a string into second to last line of a file

You can use sed to insert a line before a specific line, given it's position :

sed '4i\My New Line\' my_file.txt

Will insert "My New Line" on the fourth line

You can use wc -l to get the number of line in the file :

$ wc -l < my_file.txt
5

Complete example to directly answer your question :

$ cat my_file.txt
Hello
World
There is another line

$ sed -i "`wc -l < my_file.txt`i\\My New Line\\" my_file.txt

$ cat my_file.txt
Hello
World
My New Line
There is another line
  • Adding -i so sed actually edit the file
  • Using double quotes, otherwise substitution and expansion is not performed within single quotes.
  • Escaping \

sed line range, all but the last line

sed -e "$ ! s/a/b/"

This will match every line but the last. Confirmed with a quick test!

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

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

bash, text file remove all text in each line before the last space

awk

Print the last field of each line using awk.

The last field is indexed using the NF variable which contains the number of fields for each line. We index it using a dollar sign, the resulting one-liner is easy.

awk '{ print $NF }' file

rs, cat & tail

Another way is to transpose the content of the file, then grab the last line and transpose again (this is fairly easy to see).

The resulting pipe is:

cat file | rs -T | tail -n1 | rs -T

cut & rev

Using cut and rev we could also achieve this goal by reversing the lines, cutting the first field and then reverse it again.

rev file | cut -d ' ' -f1 | rev

sed

Using sed we simply remove all chars until a space is found with the regex ^.* [^ ]*$. This regex means match the beginning of the line ^, followed by any sequence of chars .* and a space . The rest is a sequence of non spaces [^ ]* until the end of the line $. The sed one-liner is:

sed 's/^.* \([^ ]*\)$/\1/' file

Where we capture the last part (in between \( and \)) and sub it back in for the entire line. \1 means the first group caught, which is the last field.

Notes

  1. As Ed Norton cleverly pointed out we could simply not catch the group and remove the former part of the regex. This can be as easily achieved as

    sed 's/.* //' file


    Which is remarkably less complicated and more elegant.

  2. For more information see man sed and man awk.



Related Topics



Leave a reply



Submit