Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

Help with pipe command

I can use this command to find a list of files (out of hundreds in this directory):
grep "Konto-Segment" /usr/tmp/*.tmp

That produces a list like this:
/usr/tmp/l1647444.tmp:Es konnte kein Konto-Segment mit Hilfe des angegebenen Qualifiers ermittelt werden.
/usr/tmp/l1647445.tmp:Es konnte kein Konto-Segment mit Hilfe des angegebenen Qualifiers ermittelt werden.
/usr/tmp/l1669057.tmp:Es konnte kein Konto-Segment mit Hilfe des angegebenen Qualifiers ermittelt werden.

I would like a list of filenames with timestamps like this instead:

Dec 16 10:50 l1647444.tmp
Dec 16 10:52 l1647445.tmp
Dec 16 11:40 l1649057.tmp

Can I pipe the output of the "cat" command or use "xargs"  to "ls -l" or something to get just the file names and the timestamp of each file?

I have Oracle Enterprise Linux 5 in case that makes a difference.
0
Mark Geerlings
Asked:
Mark Geerlings
  • 3
  • 2
1 Solution
 
ozoCommented:
grep -l "Konto-Segment" /usr/tmp/*.tmp | xargs stat -t"%b %d %T" -f"%Sm %N"
0
 
woolmilkporcCommented:
1) you can use "grep -l". This will display just filenames, not matching lines.

2) Use this output to run "stat" against these files. "ls -l" will not work in cases where the file is older than 6 months. Use "date" to convert the output of stat to the desired format:

grep -l "Konto-Segment" /usr/tmp/*.tmp | while read file
  do
    echo $(date -d "01/01/1970 + $(stat -c %Y $file) seconds" "+%b %d %H:%M") $file
  done

Too late, ozo was faster. But does it really work? My "stat" doesn't do it the suggested way.
0
 
Mark GeerlingsDatabase AdministratorAuthor Commented:
No, the suggestion from ozo doesn't work for me.  That returns:
stat: invalid option -- %

The suggestion from wollmilkporc gives me exactly what I asked for.  Here is a sample of the output:

Dec 16 15:50 /usr/tmp/l1647444.tmp
Dec 17 09:13 /usr/tmp/l1647445.tmp
Nov 16 21:05 /usr/tmp/l1669057.tmp

I don't understand shell scripting well enough to understand exactly how this command gets the job done, but I'll plan to accept that suggestion as the answer, since it gives me what I asked for.

If someone is willing to explain what the syntax on the "echo..." line does I would appreciate that, so I may then be able to tweak this a bit if I want something slightly different someday.  I understand the "| while read file" and the "do ... done" loop syntax.  I've used "%H" and "%M" with "date" before, but most of the rest of that line is unintelligible to me.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
woolmilkporcCommented:
Glad I could help.

The basic idea is using date's capability to not only display the current date but also to accept a different date specification by means of the "-d" option, even using kind of "natural speech" like "1 day ago", "yesterday + 1000 seconds" etc.

"stat", on the other hand, can display a file's modification date in seconds since epoch (01/01/1970 00:00:00).

Combining these two capabilities yields the desired result.
Using the command substitution "$(stat -c %Y $file)" ($file is the name read from grep's output) we get the file's modification date in seconds since epoch.
We now feed this value to "date -d" and instruct "date" to add it to 01/01/1970 (the start of epoch) and then reformat the result according to the desired appearance (%b %d %H:%M means "abbreviated month, space, decimal day, space, hour, colon, minute).

We put the whole construct into another command substitution "$(date -d ... ...) and echo out the result, followed by the filename read from grep's output.

Have fun with shell scripting und schöne Feiertage!

wmp
0
 
Mark GeerlingsDatabase AdministratorAuthor Commented:
Viele dank!  Mein Deutsch ist nicht so gut, aber ich kan "schöne Feiertage" verstehen.  (I think that's "Happy Holidays" for English speakers, or as we prefer to say here: "Merry Christmas".
0
 
woolmilkporcCommented:
Thanks for the points!

I saw that you posted German text samples so I assumed you were from Austria/Germany/Switzerland, whatever (I'm from Germany, by the way).
Sorry for the misinterpretation!


wmp
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now