简体   繁体   中英

Print last line of text file

I have a text file like this:

    1.2.3.t
    1.2.4.t

    complete

I need to print the last non blank line and two line to last as two variable. the output should be:

 a=1.2.4.t
 b=complete

I tried this for last line: b= awk '/./{line=$0} END{print line}' myfile but I have no idea for a.

grep . file | tail -n 2 | sed 's/^ *//;1s/^/a=/;2s/^/b=/'

Output:

a=1.2.4.t
b=complete

awk to the rescue!

$ awk 'NF{a=b;b=$0} END{print "a="a;print "b="b}' file
a=1.2.4.t
b=complete

Or, if you want to the real variable assignment

$ awk 'NF{a=b;b=$0} END{print a, b}' file 
  | read a b; echo "a="$a; echo "b="$b

a=1.2.4.t
b=complete

you may need -r option for read if you have backslashes in the values.

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