Help with pipe command

Posted on 2013-12-17
Medium Priority
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 2000 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.
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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 …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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 (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

762 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