Help with pipe command

Posted on 2013-12-17
Last Modified: 2013-12-17
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.
Question by:Mark Geerlings
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 84

Expert Comment

ID: 39724281
grep -l "Konto-Segment" /usr/tmp/*.tmp | xargs stat -t"%b %d %T" -f"%Sm %N"
LVL 68

Accepted Solution

woolmilkporc earned 500 total points
ID: 39724308
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
    echo $(date -d "01/01/1970 + $(stat -c %Y $file) seconds" "+%b %d %H:%M") $file

Too late, ozo was faster. But does it really work? My "stat" doesn't do it the suggested way.
LVL 35

Author Comment

by:Mark Geerlings
ID: 39724378
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.
Industry Leaders: 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!

LVL 68

Expert Comment

ID: 39724538
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!

LVL 35

Author Comment

by:Mark Geerlings
ID: 39724565
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".
LVL 68

Expert Comment

ID: 39724606
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!


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Shell script - run command using variable from a list 2 83
sqlldr issue 1 34
check the file dates in unix 14 76
change script to get csv file on the prompt 8 31
This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question