|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Substring from the file name
Hi
I need substring from the file name of list of files. I mean i will have to load all the files into database which are there in the Directory. I need to make the directory with the date(which is substring of the filename Eg: Filename_Name_2012013001010101.txt, So the directory should be 20120130). I need to load this file into oracle and need to move the file into above created directory. It should happend till all files are loaded into database. Could someone give the ideas? Thanks |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Code:
$ echo Filename_Name_2012013001010101.txt | awk '/^[0-9]/{print substr($1,1,8)}' RS='[_.]'
20120130 |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
Create a file with list of file names to be loaded, use
SQL loader to load them to Oracle DB. Verify the result of load operation, if successful then run below bash script to perform the move: Code:
#!/bin/bash
while read filename
do
fname=${filename##*_}
dir=${fname:0:8}
echo mkdir -p "$dir"
echo mv "$filename" "${dir}/"
done < file.listNote: Remove the highlighted echo if the output looks good. |
|
#4
|
|||
|
|||
|
Thanks for you reply,
But forgot to tell you that Filename may have numbers as well apart from Date and Full file name will have n. number of underscores. What i need is we should pick up the date from end of the file name as it has date and time before the extension of file. |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
If you want to extract the date from end of the file name, then try this: Code:
filename="Filename_Name_2012013001010101.txt"
fname=${filename%%.*}
fname=${fname##*_}
dir=${fname:0:8} |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Thanks Bipin,
I tried running the script using your code but it gives an error saying bad substitution at "dir=${fname:0:8}" |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
Which shell are you using?
bakunin |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Get value of substring from a file | mjwoodford | Shell Programming and Scripting | 4 | 09-25-2009 01:01 AM |
| How do I pull a substring out of a file? | enator45 | Shell Programming and Scripting | 4 | 07-07-2009 12:25 PM |
| Help on substring of file list | victorcheung | Shell Programming and Scripting | 1 | 04-03-2009 01:03 AM |
| extracting substring from a file name | adityamahi | Shell Programming and Scripting | 4 | 12-30-2008 10:07 AM |
| Calling a substring from another file | dcfargo | Shell Programming and Scripting | 4 | 06-10-2008 08:03 PM |
|
|