Results 1 to 4 of 4

Thread: Parsing the file in Unix shell script

  1. #1
    Join Date
    May 2008
    Posts
    4,831

    Parsing the file in Unix shell script

    Hi friends, I need a script which will help me to strip part of the filenames. Unix shell script is new to me. I can do it with many lines of code but with shell scripts, I believe it could be shorter.

    file_1 = first111124.txt
    file_2 = second111251.pdf

    new file1 = first.txt
    new file2 = second.pdf

    Can anyone help me to do this?
    I am waiting for your reply.

  2. #2
    Join Date
    May 2008
    Posts
    3,316

    Re: Parsing the file in Unix shell script

    Hi, you can achieve parsing the file in unix shell script by doing the following thing. You can use the set command to parse the string.

    1) Set the field seperator, IFS, to a period.
    2) Execute the set command.
    3) Save the 2nd field as the extension
    4) $1 contains the base filename so execute another set command sets $1 which satisfies your requirement.
    5) Build your new file name.

    Code:
    file_1="first111124.txt"
    set - $(IFS="."; echo "$file_1")
    ext="$2"
    set - $(echo "$1")
    newfilename="$1.$ext"
    echo "$newfilename"
    Last edited by Raine; 23-11-2009 at 11:33 AM.

  3. #3
    Join Date
    Jan 2008
    Posts
    3,755

    Re: Parsing the file in Unix shell script

    Hi, I am also new the Unix shell script. You want to parse the file in Unix shell script. I don't know how to help you, But if you find solution regarding this; then please forward me also. I have searched this on internet but not find anything about this. If you got solution just post on this forum. Actually I am unable to get what you want to do, but if you post the solution I will able to get it.

  4. #4
    Join Date
    Apr 2008
    Posts
    4,088

    Re: Parsing the file in Unix shell script

    Hi, you can use following for parsing the file in unix shell script.

    Code:
    $ file="first111124.txt"
    $ extension=${file##*.}
    $ filename=${file%%.???}
    $ set -- $filename
    $ newfile="$1.$extension"
    $ echo $newfile
    first.txt

Similar Threads

  1. Shell script with sed
    By Ameeryan in forum Software Development
    Replies: 5
    Last Post: 23-12-2009, 02:29 PM
  2. Cutting a file into several files using shell script
    By Shaan12 in forum Software Development
    Replies: 3
    Last Post: 22-10-2009, 11:43 PM
  3. Need suggestion on book for Unix & shell scripting!
    By SamsherR in forum Software Development
    Replies: 2
    Last Post: 11-02-2009, 05:22 PM
  4. How to develop an interactive script in Unix systems
    By Allanoo in forum Software Development
    Replies: 4
    Last Post: 16-10-2008, 04:29 PM
  5. Unix Shell Script
    By BlackSunReyes in forum Software Development
    Replies: 3
    Last Post: 12-08-2008, 01:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,021,036.81184 seconds with 16 queries