|
| |||||||||
| Tags: file, parse, shell script, unix shell script |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| ||||
| ||||
| 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
| ||||
| ||||
| 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
| ||||
| ||||
| 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
| ||||
| ||||
| 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 |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |