LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   linux - sending output to text file (https://www.linuxquestions.org/questions/programming-9/linux-sending-output-to-text-file-4175693924/)

babag 04-19-2021 03:07 AM

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

Turbocapitalist 04-19-2021 03:11 AM

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.

babag 04-19-2021 03:36 AM

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

Turbocapitalist 04-19-2021 03:42 AM

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.

frankbell 04-19-2021 08:20 PM

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.

babag 04-20-2021 12:09 AM

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

babag 04-20-2021 12:28 AM

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

babag 04-20-2021 12:59 AM

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

shruggy 04-20-2021 04:48 AM

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

babag 04-20-2021 12:24 PM

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


All times are GMT -5. The time now is 07:28 PM.