LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2021, 03:07 AM   #1
babag
Member
 
Registered: Aug 2003
Posts: 392

Rep: Reputation: 31
linux - sending output to text file


hope this is an appropriate place to ask this. i have this:
Code:
for file in *.mp3; do sox $file -n stats > $file.txt 2>&1 | tail -1; done
which makes a text file for each file examined in looping through a directory. how would i change this to write all of it to a single file, with a couple of lines between each set of stats?

thanks,
babag
 
Old 04-19-2021, 03:11 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,611
Blog Entries: 3

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
You could use an append redirection in place of the overwrite which you currently have. Or you could move the redirection to after the 'done' statement.
 
1 members found this post helpful.
Old 04-19-2021, 03:36 AM   #3
babag
Member
 
Registered: Aug 2003
Posts: 392

Original Poster
Rep: Reputation: 31
thanks turbocapitalist.

not savvy enough to figure out the append idea. moving the redirect to the end did get it all into one file, though.
Code:
for file in *.mp3; do sox $file -n stats; done > stats.txt 2>&1 | tail -1
problem with that is it all runs together. would be nice to be able to put a couple of line breaks between each loop and maybe the name of the file being referenced. i'm guessing that might be more easily done through the append, which i don't yet get. also don't understand that i'm using an 'overwrite'?

thanks again,
babag

Last edited by babag; 04-19-2021 at 03:40 AM.
 
Old 04-19-2021, 03:42 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,611
Blog Entries: 3

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
In the example you have shown, you have redirection from the program into a pipe and then into a file. That last part used > for overwriting instead of >> for appending.

If you wish to add lines, one of the simple ways would be to introduce echo statements:

Code:
for file in *.mp3; 
do 
        sox $file -n stats 2>&1 | tail -1; 
        echo -e '.\n.\n.\n'
done > $file.txt
see 'man echo' for the details.
 
1 members found this post helpful.
Old 04-19-2021, 08:20 PM   #5
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 17,395
Blog Entries: 27

Rep: Reputation: 5405Reputation: 5405Reputation: 5405Reputation: 5405Reputation: 5405Reputation: 5405Reputation: 5405Reputation: 5405Reputation: 5405Reputation: 5405Reputation: 5405
Use ">>" to append the output, instead of ">" to redirect the output. See http://www.linfo.org/output_redirection_operator.html

From the link:

Quote:
There are other types of operators that are also used with redirection. Among them is the append operator, which is similar to the output redirection operator except that it is represented by two successive rightward pointing angle brackets (>>) and adds text from a file or other source to the end of file, if a file with the target name already exists, rather than overwriting the file.

Last edited by frankbell; 04-19-2021 at 08:21 PM. Reason: syntax
 
Old 04-20-2021, 12:09 AM   #6
babag
Member
 
Registered: Aug 2003
Posts: 392

Original Poster
Rep: Reputation: 31
thanks for the replies. i looked at the links and they cleared up a little bit of my confusion but i'm so bad at this that it didn't get me to where i could adapt any of this to work.

i've been continuing to try to get this to work. below is the experience so far. first, this is the output from a single mp3 file as an example of what the txt file i want to create should look like:
Code:
DC offset  -0.000287
Min level  -0.585483
Max level   0.572299
Pk lev dB      -4.65
RMS lev dB    -19.55
RMS Pk dB     -12.98
RMS Tr dB     -78.44
Crest factor    5.56
Flat factor     0.00
Pk count           2
Bit-depth      29/29
Num samples     628k
Length s      14.237
Scale max   1.000000
Window s       0.050
there should be fifteen lines of info like the above for each file looked at by the loop.

this command,
Code:
for file in *.mp3; do sox $file -n stats 2>&1 | tail -1; echo -e '.\n.\n.\n'; done > stats.txt
basically just the last suggestion from turbocapitalist with the output filename customized and a semicolon added after the line breaks (without the semicolon it just hangs), produces this:
Code:
Window s       0.050
.
.
.
the above repeats in the file once for each file looked at by the loop. rather than giving the 15 lines of data in my top example here, followed by the line breaks, only the last line is printed to the file followed by the line breaks. it does make an entry for each mp3 file but missing fourteen of the fifteen lines.

i tried lots of slight variations on this without ever being able to get the output as intended. it seemed like, when i could get the full output from sox into the txt file, i could not get the line breaks so that all of the sox output ran together. whenever i got the line breaks to show up in the text file, the sox output was always truncated to just the last line.

thanks again,
babag

Last edited by babag; 04-20-2021 at 12:15 AM.
 
Old 04-20-2021, 12:28 AM   #7
babag
Member
 
Registered: Aug 2003
Posts: 392

Original Poster
Rep: Reputation: 31
spoke too soon. this is starting to get somewhere:
Code:
for file in *.mp3; do sox $file -n stats; echo -e '\n--------------------\n'; done > stats.txt 2>&1 | tail -1
 
Old 04-20-2021, 12:59 AM   #8
babag
Member
 
Registered: Aug 2003
Posts: 392

Original Poster
Rep: Reputation: 31
think i got it:
Code:
for file in *.mp3; do echo -e '\n--------------------\n' $file '\n'; sox $file -n stats; done > stats.txt 2>&1 | tail -1
produces:
Code:
--------------------
 00-somesoundfile.mp3 

DC offset  -0.000287
Min level  -0.585483
Max level   0.572299
Pk lev dB      -4.65
RMS lev dB    -19.55
RMS Pk dB     -12.98
RMS Tr dB     -78.44
Crest factor    5.56
Flat factor     0.00
Pk count           2
Bit-depth      29/29
Num samples     628k
Length s      14.237
Scale max   1.000000
Window s       0.050

--------------------
 01-somesoundfile.mp3 

DC offset   0.000000
Min level  -0.637310
Max level   0.632990
Pk lev dB      -3.91
RMS lev dB    -20.54
RMS Pk dB     -12.99
RMS Tr dB     -79.91
Crest factor    6.78
Flat factor     0.00
Pk count           2
Bit-depth      29/29
Num samples    9.24M
Length s     209.528
Scale max   1.000000
Window s       0.050

--------------------
 02-somesoundfile.mp3 

DC offset   0.000070
Min level  -0.685630
Max level   0.674988
Pk lev dB      -3.28
RMS lev dB    -20.13
RMS Pk dB      -7.84
RMS Tr dB     -65.74
Crest factor    6.96
Flat factor     0.00
Pk count           2
Bit-depth      29/29
Num samples    94.2M
Length s    2136.451
Scale max   1.000000
Window s       0.050
thanks for all the info and help,
babag
 
Old 04-20-2021, 04:48 AM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 2,321

Rep: Reputation: Disabled
What is the purpose of tail -1 as the last command? With >stats.txt 2>&1 you're redirecting both stdout and stderr to stats.txt, so tail will never get anything to consume. If you intended to reduce the stderr output to one line then the order of redirections should have been 2>&1 >stats.txt
 
1 members found this post helpful.
Old 04-20-2021, 12:24 PM   #10
babag
Member
 
Registered: Aug 2003
Posts: 392

Original Poster
Rep: Reputation: 31
thanks shruggy.

i figured there was probably something extraneous in there and tail seemed a likely place to look but was so relieved to get something to work that i just stopped working on it when i got a good result. just tested without the pipe to tail and it does, indeed, produce the same result.

thanks again,
babag
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrote Shell in C - piping output from first command to second command, sending to output file issue KarenWest Programming 2 03-31-2016 05:27 PM
[SOLVED] Redirecting terminal output to text file leads to corrupted text file mariovrn Linux - Newbie 5 11-30-2011 07:21 AM
[SOLVED] Bash command to 'cut' text into another text file & modifying text. velgasius Programming 4 10-17-2011 04:55 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
in Pascal: how to exec a program, discard text output or send to text file Valkyrie_of_valhalla Programming 6 05-02-2007 09:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:09 AM.

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