Search code examples
bashgrepechomediainfo

How to print two lines from grep into one line with bash script


I'm trying to achieve the following:

I have a directory with multiple video files in it, I want to list all the files and in the same line add the duration and size of the file , using mediainfo (nothing else is available).

Mediainfo's output would be something like:

General
Format      : 
File Size   : 335 MiB
Duration    : 28mn 24s

I want to get the following data in a file:
filename : 335 MiB : 28mn 24s
So I can check if there are duplicates of the file...

Therefore I have the following script:

#!/bin/bash
         for i in $( ls /mnt/storage/kids/* ); do
                     echo -n item: $i ":"
                     mediainfo $i |grep -A 1 "File size"
                     done

with echo n I get the following line in the same line as item: $i and with grep -A 1 I get both file size and duration but duration goes into a second line isntead of the same as the file name and file size.
I would also like to get rid of file name and duration headers.

Any idea?


Solution

    • Don't use for i in $(ls), this is buggy. If you want to loop over files use for i in /mnt/storage/kids/* directly.
    • Avoid echo -n in favor of printf, which is portable.

    Try this:

    for i in /mnt/storage/kids/* ; do
        printf 'item: %s: ' "$i"
        mediainfo "$i" | sed -e '/File Size/,/Duration/{s/.*: //p;};d' | sed -e '$!N;s/\n/: /'
    done