Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 23-11-2009
Member
 
Join Date: May 2008
Posts: 4,829
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.
Reply With Quote
  #2  
Old 23-11-2009
Member
 
Join Date: May 2008
Posts: 3,313
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.
Reply With Quote
  #3  
Old 23-11-2009
Member
 
Join Date: Jan 2008
Posts: 3,751
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.
Reply With Quote
  #4  
Old 23-11-2009
Member
 
Join Date: Apr 2008
Posts: 4,086
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
__________________
Ram requirement for various OS
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 01:11 AM.