Find the answer to your Linux question:
Results 1 to 7 of 7
Hey! I am new to this forum but confused in programming to Bash scripting. How do change the file extension using awk and expr (or egrep). Ex: me.mp3 to me.wav ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    4

    Unhappy Question about text manipulation (substitution) using expr/egrep/awk in Bash

    Hey!

    I am new to this forum but confused in programming to Bash scripting. How do change the file extension using awk and expr (or egrep).

    Ex: me.mp3 to me.wav

    I have tried:

    files=$(ls -1 | grep *.mp3)

    for x in $files
    do
    mv $x band_song$x
    done

    This example only changes the file name and not the extention.



  2. #2
    Just Joined!
    Join Date
    Oct 2007
    Posts
    37
    You could do something like:

    for i in `ls -1 *.mp3| awk -F. '{print $1}'`; do mv $i.mp3 $i.wav; done

    Of course there are a million other ways and probably better ones too, but this way works also.

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Posts
    4

    Unhappy Text manipulation (substitution) using expr/egrep/awk in Bash



    I want to use expr in order to change a the file extension from .mp3 to .wav. I wasn't able to find any examples in Google.

    I know the more simple codes are a lot better than complex codes.

  4. #4
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Quote Originally Posted by acwbrat View Post
    I want to use expr in order to change a the file extension from .mp3 to .wav. I wasn't able to find any examples in Google.
    [...]
    Why expr?

    A few ways (without expr, awk etc.):

    Code:
    $ touch {1..5}.mp3
    $ ls
    1.mp3  2.mp3  3.mp3  4.mp3  5.mp3
    $ rename s/.mp3/.wav/ *mp3
    $ ls
    1.wav  2.wav  3.wav  4.wav  5.wav
    Code:
    $ ls
    1.mp3  2.mp3  3.mp3  4.mp3  5.mp3
    $ for f in *.mp3;do mv "$f" "${f%.*}".wav;done
    $ ls
    1.wav  2.wav  3.wav  4.wav  5.wav
    With zsh:

    Code:
    % ls
    1.mp3  2.mp3  3.mp3  4.mp3  5.mp3
    % autoload -U zmv
    % zmv -W '*.mp3' '*.wav'
    % ls
    1.wav  2.wav  3.wav  4.wav  5.wav

  5. #5
    Just Joined!
    Join Date
    Oct 2007
    Posts
    4
    I have this code:

    #!/bin/bash
    #
    # Replaces blanks with underscores in all the filenames
    # of the current directory.

    i=0

    for filename in *
    do
    echo "$filename" | grep -q " "
    if [ $? -eq 0 ]
    then
    fname=$filename
    n=`echo $fname | sed -e "s/ /_/g"`
    mv "$fname" "$n"
    ((i++1))
    fi
    done

    echo "$i file(s), done."
    exit 0

    When I tried to substitute my information, it didn't compute.

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by acwbrat View Post
    I have this code:

    #!/bin/bash
    #
    # Replaces blanks with underscores in all the filenames
    # of the current directory.

    i=0

    for filename in *
    do
    echo "$filename" | grep -q " "
    if [ $? -eq 0 ]
    then
    fname=$filename
    n=`echo $fname | sed -e "s/ /_/g"`
    mv "$fname" "$n"
    ((i++1))
    fi
    done

    echo "$i file(s), done."
    exit 0

    When I tried to substitute my information, it didn't compute.
    you are trying to increment i, it should be like this
    Code:
    i=$(($i+1))

  7. #7
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Quote Originally Posted by ghostdog74 View Post
    you are trying to increment i, it should be like this
    Code:
    i=$(($i+1))
    With recent versions of bash(and, of course, zsh):
    Code:
    $((++i))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...