Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Question easy way to separate different fields?

    say I input aaa.f and I need to separate it into aaa and f, and then attach .f90 to aaa to form aaa.f90. How can that be most easily done? Thanks,

  2. #2
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Western Kentucky
    I would write it in PERL, and I would use the split function, splitting at the dot. Then joining the two variables. It might look something like this.

    $input = "aaa.f";
    $ext= "f90"
    ($name, $discard) = split('.', $input);
    $desired = join('.',$name,$ext);
    print $desired;
    Of course, you'd have to change it up to match how your input will be added.
    linux user # 503963

  3. #3
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Yes, perl or any other language is a possibility, as it enables all kind of further processing.

    For a one shot solution, a sed oneliner might also be suitable:
    echo aaa.f | sed 's#^\(aaa\)\.f$#\1.f90#'
    You will need to adjust the regex to your actual usecase.
    You must always face the curtain with a bow.

  4. $spacer_open
  5. #4
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris

    It looks like you are renaming old Fortran filenames to new filenames.

    If you are using bash or ksh, you can use:
    $ v="faaf.f"
    $ echo ${v/.f/.f90}
    I used versions:
    GNU bash 3.2.39
    ksh 93s+
    This is all done within the shell as opposed to running external commands, but for a small amount of data, the difference is probably not significant. Examples of these kinds of internal manipulations can be seen at BashFAQ/100 - Greg's Wiki

    Using this construct in a mv command should solve your problem. If indeed you are renaming files, you might be able to use rename:
    $ touch faaf.f
    $ ls -lgG *.f
    -rw-r----- 1 0 Dec 15 04:21 faaf.f
    $ rename --no-act 's/\.f$/.f90/' *.f
    faaf.f renamed as faaf.f90
    The "no-act" shows what would happen, and, when you are satisfied that it will do what you desire, the "no-act" should be removed to actually perform the rename. As I have done here, you can try that in a separate directory on test filenames.

    See man pages for details.

    Best wishes ... cheers, drl
    Last edited by drl; 12-15-2011 at 09:32 AM.
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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