Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with pipe command

Posted on 2013-12-17
6
Medium Priority
?
288 Views
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.
0
Comment
Question by:Mark Geerlings
[X]
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
6 Comments
 
LVL 84

Expert Comment

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

Accepted Solution

by:
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
  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
 
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.
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!

 
LVL 68

Expert Comment

by:woolmilkporc
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!

wmp
0
 
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".
0
 
LVL 68

Expert Comment

by:woolmilkporc
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!


wmp
0

Featured Post

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!

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…

618 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