script help

HI All,

I am having a file with content

-rw-r----- 1 root system 0 Jun 03 01:50 /etc/name_v1
-rw-r----- 1 root system 0 Jun 03 01:50 /etc/name_v2
-rw-r----- 1 root system 0 Jun 03 01:50 /etc/name_v3

From this, i need find out only '/etc/name_v3' of the last line(last part of the last line). And need to store that value to a variable.

Thanks in advance
Renjesh Raju

I don't understand your question very well but

grep "/etc/name_v3$" IN_FILE

shows only the line you want...

VAR1=`tail -1 filename | awk '{print $NF}'`
echo $VAR1

I gave just an eg.
I am not sure about the file content, all i need to do is to find the last part of the last line. I want a general way to find the particular content from the file.

Hi.

$ sed -n "$ s|[^/]*||p" file1
/etc/name_v3
sed -n "$ s/.* //p" file1
/etc/name_v3

I want to grep "name_v3" part only.

The las solution will not work if the last is: /etc/var file name with spaces

Try with this:

tail -1 fichero1 | awk -F" " '{$1=$2=$3=$4=$5=$6=$7=$8="";OFS=" "}1' | sed "s/^ *\/etc\///"

That's slightly different from what you asked originally.

$ cat file1
-rw-r----- 1 root system 0 Jun 03 01:50 /etc/name_v1
-rw-r----- 1 root system 0 Jun 03 01:50 /etc/name_v2
-rw-r----- 1 root system 0 Jun 03 01:50 /etc/name_v3
-rw-r----- 1 root system 0 Jun 03 01:50 /etc/name v4

$ sed -n "$ s|.*/||p" file1
name v4

Hello!...

I'm lookin in the sed help but I don't find |...
�is it a reverse search?

from final ($) search 'anything' until "/" is found??

thanks

tail -1 inputfile | awk -F"/" '{print $NF}'

Everything up to the final /

| is just a separator, for convenience because your string has a / in it.

You could just as easily do

$ sed -n "$ s/.*\///p" file1
name v4

or

$ sed -n "$ s@.*/@@p" file1
name v4

or

$ sed -n "$ s�.*/��p" file1
name v4

or almost anything you like.

1 Like