General
cancel
Showing results for 
Search instead for 
Did you mean: 

Query on Small sed output + ls-ltr

 
Highlighted
Regular Advisor

Query on Small sed output + ls-ltr

Hello,
I have 2 questions-
1) What is the signifiance of sed part in below command:
find $glob_path/*.txt -type f -print 2>/dev/null | sed -n '/'$CTY'invdet[0-9][0-9]*.txt$/p'

2) IF I want to have name of just the latest file generated using ls -ltr, How do i get that?

Thank you!
3 REPLIES 3
Highlighted
Honored Contributor

Re: Query on Small sed output + ls-ltr

> 1) What is the signifiance of sed part in
> below command:

It passes only the lines which match that
pattern. "man sed".

> [...] How do i get that?

One way:

ls -ltr | tail -1

> find $glob_path/*.txt [...]

Do you really want to let the shell expand
that wildcard "*.txt"?

> sed -n '/'$CTY'invdet[0-9][0-9]*.txt$/p'

If you really want names like "xxx.txt", and
not names like "xxxxtxt", then you might wish
to escape that last "." in the "sed" pattern:

sed -n '/'$CTY'invdet[0-9][0-9]*\.txt$/p'

For example:

td176> ls -lrt | sed -n '/tr.*2.cpp/p'
-rw-rw-rw- 1 antinode 513 207 May 6 09:25 trail2.cpp
-rw-rw-rw- 1 antinode 513 5 May 8 00:05 trail2xcpp

td176> ls -lrt | sed -n '/tr.*2\.cpp/p'
-rw-rw-rw- 1 antinode 513 207 May 6 09:25 trail2.cpp

In a "sed" expression, an unescaped "."
matches _any_ character, not only ".".
Highlighted
Acclaimed Contributor

Re: Query on Small sed output + ls-ltr

>1) What is the significance of sed part in below command:
find $glob_path/*.txt -type f -print 2>/dev/null | sed -n '/'$CTY'invdet[0-9][0-9]*.txt$/p'

If you want to make it clearer, you could have used grep instead:
... | grep $CTY'invdet[0-9][0-9]*.txt$'

And if you want a literal ".", you escape it the same way Steven said.
Highlighted
Honored Contributor

Re: Query on Small sed output + ls-ltr

>2) IF I want to have name of just the latest file generated using ls -ltr, How do i get that

If you want ONLY the file name using ls -ltr:
ls -ltr|tail -1 |awk '{print $9}'

or

if you want to use ls:
ls -rt1|tail -1

Rgds.