LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-23-2016, 10:16 PM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
Question grep doesn't extract string!?


grep doesn't work for extracting string from avprobe

Why does the syntax
Code:
lsb_release -a | grep 'Release'
just displays the desired line beginning with 'Release", but the command
Code:
avprobe ObamaFinalDebate.mp3 | grep 'Duration:'
displays a most undesired entire paragraph of stats?

Specific results:

Code:
a@a-NC210-NC110:~/Videos/Obama$ lsb_release -a | grep 'Release'
No LSB modules are available.
Release:	14.04

a@a-NC210-NC110:~/Videos/Obama$ avprobe ObamaFinalDebate.mp3 | grep 'Duration:'
avprobe version 9.18-6:9.18-0ubuntu0.14.04.1+fdkaac, Copyright (c) 2007-2014 the Libav developers
  built on Apr 10 2015 23:22:24 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
[mp3 @ 0xa081fe0] max_analyze_duration reached
Input #0, mp3, from 'ObamaFinalDebate.mp3':
  Metadata:
    major_brand     : dash
    minor_version   : 0
    compatible_brands: iso6mp41
    creation_time   : 2015-02-22 17:24:22
    encoder         : Lavf54.20.4
  Duration: 00:05:11.69, start: 0.000000, bitrate: 77 kb/s
    Stream #0.0: Audio: mp3, 44100 Hz, 2 channels, s16p, 77 kb/s
Can just the line beginning with "Duration" be extracted?

{Once I do this I think I could use awk to extract a certain word in that line.}
 
Old 04-23-2016, 10:39 PM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Why does the output of your first command also include 'No LSB modules are available.' when only the line containing 'Release' should be shown?
Code:
a@a-NC210-NC110:~/Videos/Obama$ lsb_release -a | grep 'Release'
No LSB modules are available.
Release:	14.04
Do you have 'grep' aliased to something?
Perhaps the output from 'alias' will help.
 
1 members found this post helpful.
Old 04-23-2016, 11:28 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
What happens if you pipe the output to a file then search it ?. Else dump it out in hex/octal and see what is being produced.
 
1 members found this post helpful.
Old 04-24-2016, 01:32 AM   #4
cnamejj
Member
 
Registered: Mar 2015
Distribution: Ubuntu
Posts: 37

Rep: Reputation: Disabled
Looks like "avprobe" writes output to STDERR, which is why your grep isn't doing anything. And "lsb_release" also writes part of it's output, specifically the "No LSB modules are available." line to STDERR.

So you can do something like,

Code:
avprobe 2>&1 | grep 'Duration:'
to get the behavior you want.
 
1 members found this post helpful.
Old 04-25-2016, 07:49 PM   #5
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Smile alias output

Quote:
Originally Posted by allend View Post
Why does the output of your first command also include 'No LSB modules are available.' when only the line containing 'Release' should be shown?
Do you have 'grep' aliased to something?
Perhaps the output from 'alias' will help.
My alias output:
Code:
...
alias ftp='ncftp -d15'
alias grep='grep --color=auto'
alias h='history 10'
...
 
Old 04-25-2016, 08:03 PM   #6
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Smile pipe output to file: Results

Quote:
Originally Posted by syg00 View Post
What happens if you pipe the output to a file then search it ?
Attempt01:
Code:
$ avprobe ObamaFinalDebate.mp3 | grep 'Duration'>/home/a/Desktop/b.txt
b.txt output:
(blank)

Attempt02:
Code:
$avprobe ObamaFinalDebate.mp3>/home/a/Desktop/a.txt
Terminal output
Code:
avprobe version 9.18-6:9.18-0ubuntu0.14.04.1+fdkaac, Copyright (c) 2007-2014 the Libav developers
  built on Apr 10 2015 23:22:24 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
[mp3 @ 0x9b37fe0] max_analyze_duration reached
Input #0, mp3, from 'ObamaFinalDebate.mp3':
  Metadata:
    major_brand     : dash
    minor_version   : 0
    compatible_brands: iso6mp41
    creation_time   : 2015-02-22 17:24:22
    encoder         : Lavf54.20.4
  Duration: 00:05:11.69, start: 0.000000, bitrate: 77 kb/s
    Stream #0.0: Audio: mp3, 44100 Hz, 2 channels, s16p, 77 kb/s
a.txt:
Code:
# avprobe output
 
Old 04-25-2016, 08:13 PM   #7
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Question Does "2" = media file name?

Quote:
Originally Posted by cnamejj View Post
Looks like "avprobe" writes output to STDERR,
So you can do something like,
Code:
avprobe 2>&1 | grep 'Duration:'
I tried this, results are below.
Code:
a@a-NC210-NC110:~/Videos/Obama$ avprobe 2>&1 | grep 'Duration:'
a@a-NC210-NC110:~/Videos/Obama$ avprobe ObamaFinalDebate.mp3>&1 | grep 'Duration:'
avprobe version 9.18-6:9.18-0ubuntu0.14.04.1+fdkaac, Copyright (c) 2007-2014 the Libav developers
  built on Apr 10 2015 23:22:24 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
[mp3 @ 0x9ab8fe0] max_analyze_duration reached
Input #0, mp3, from 'ObamaFinalDebate.mp3':
  Metadata:
    major_brand     : dash
    minor_version   : 0
    compatible_brands: iso6mp41
    creation_time   : 2015-02-22 17:24:22
    encoder         : Lavf54.20.4
  Duration: 00:05:11.69, start: 0.000000, bitrate: 77 kb/s
    Stream #0.0: Audio: mp3, 44100 Hz, 2 channels, s16p, 77 kb/s
Looks like I'll use the topics you mentioned to try to find something related in the ebook: Advanced Bash Scripting by Mendell Cooper.

Thanks anyway.
 
Old 04-25-2016, 09:48 PM   #8
cnamejj
Member
 
Registered: Mar 2015
Distribution: Ubuntu
Posts: 37

Rep: Reputation: Disabled
Code:
avprobe ObamaFinalDebate.mp3 2>&1 | grep 'Duration:
 
1 members found this post helpful.
Old 04-26-2016, 09:36 PM   #9
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Smile [SOLVED] Thanks Everyone!

Quote:
Originally Posted by cnamejj View Post
Code:
avprobe ObamaFinalDebate.mp3 2>&1 | grep 'Duration:
It works!

Code:
$ avprobe ObamaFinalDebate.mp3 2>&1 | grep 'Duration:'
  Duration: 00:05:11.69, start: 0.000000, bitrate: 77 kb/s
And with a simple awk pipe I can extract just the bitrate:
Code:
$ avprobe ObamaFinalDebate.mp3 2>&1 | grep 'bitrate:' | awk '{ print $6 }'
77
Thanks a lot cnamejj and everyone else!
 
Old 04-27-2016, 01:03 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
do not need both awk and grep, use:
Code:
avprobe ... 2>&1 | awk '/bitrate:/ { print $6 }'
 
1 members found this post helpful.
Old 04-27-2016, 02:45 AM   #11
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Cool Advanced Bash Scripting, Mendel Cooper

I extracted the following from my current Bash textbook, this will convenience future learners who encounter the same problem.

Chapter 20. I/O Redirection
p 371:
There are always three default files [104] open, stdin (the keyboard), stdout (the screen), and stderr
(error messages output to the screen). These, and any other open files, can be redirected.
...

Each open file gets assigned a file descriptor. [105] The file descriptors for stdin, stdout, and stderr
are 0, 1, and 2, respectively.
...


p372:
2>&1
# Redirects stderr to stdout.
# Error messages get sent to same place as standard output.
>>filename 2>&1
bad_command >>filename 2>&1
# Appends both stdout and stderr to the file "filename" ...
2>&1 | [command(s)]
bad_command 2>&1 | awk '{print $5}'
# found
# Sends stderr through a pipe.
# |& was added to Bash 4 as an abbreviation for 2>&1 |.
i>&j
# Redirects file descriptor i to j.
# All output of file pointed to by i gets sent to file pointed to by j.
>&j
# Redirects, by default, file descriptor 1 (stdout) to j.
# All stdout gets sent to file pointed to by j.


Footnotes:
[104] By convention in UNIX and Linux, data streams and peripherals (device files) are treated as files, in a
fashion analogous to ordinary files.
[105] A file descriptor is simply a number that the operating system assigns to an open file to keep track of it. Consider it a simplified type of file pointer. It is analogous to a file handle in C.

Last edited by andrew.comly; 04-27-2016 at 02:51 AM. Reason: clarity
 
  


Reply

Tags
awk, extract, grep, pipe, strings



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to show selected string using grep from file and replace it with new input string prasad1990 Linux - Software 2 03-19-2015 08:02 AM
[SOLVED] grep for a string in nested string threezerous Linux - Newbie 3 09-12-2014 10:04 AM
[SOLVED] Issue trying to grep a string, but keep a similar string Supp0rtLinux Linux - Software 7 10-04-2011 06:35 PM
extract a string within a string using a pattern adshocker Linux - Newbie 1 11-04-2010 10:44 PM
grep string with space (2 word string) casperdaghost Linux - Newbie 7 08-24-2009 02:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration