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.
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"
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.
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