Substring from the file name | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Substring from the file name

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-30-2013
cnrj cnrj is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 2 October 2013, 5:57 AM EDT
Posts: 38
Thanks: 5
Thanked 0 Times in 0 Posts
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  
Old 01-30-2013
cabrao cabrao is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 15 April 2013, 7:39 AM EDT
Posts: 201
Thanks: 37
Thanked 24 Times in 24 Posts

Code:
$ echo Filename_Name_2012013001010101.txt | awk '/^[0-9]/{print substr($1,1,8)}' RS='[_.]'
20120130

Sponsored Links
    #3  
Old 01-30-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 25 November 2014, 4:24 PM EST
Location: Galactic Empire
Posts: 3,389
Thanks: 236
Thanked 1,210 Times in 1,136 Posts
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.list

Note: Remove the highlighted echo if the output looks good.
    #4  
Old 01-30-2013
cnrj cnrj is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 2 October 2013, 5:57 AM EDT
Posts: 38
Thanks: 5
Thanked 0 Times in 0 Posts
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  
Old 01-30-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 25 November 2014, 4:24 PM EST
Location: Galactic Empire
Posts: 3,389
Thanks: 236
Thanked 1,210 Times in 1,136 Posts
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  
Old 01-31-2013
cnrj cnrj is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 2 October 2013, 5:57 AM EDT
Posts: 38
Thanks: 5
Thanked 0 Times in 0 Posts
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  
Old 01-31-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 25 November 2014, 7:22 PM EST
Location: In the leftmost byte of /dev/kmem
Posts: 4,337
Thanks: 45
Thanked 839 Times in 662 Posts
Quote:
Originally Posted by cnrj View Post
but it gives an error saying bad substitution
Which shell are you using?

bakunin
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 02:01 AM
How do I pull a substring out of a file? enator45 Shell Programming and Scripting 4 07-07-2009 01:25 PM
Help on substring of file list victorcheung Shell Programming and Scripting 1 04-03-2009 02:03 AM
extracting substring from a file name adityamahi Shell Programming and Scripting 4 12-30-2008 11:07 AM
Calling a substring from another file dcfargo Shell Programming and Scripting 4 06-10-2008 09:03 PM



All times are GMT -4. The time now is 05:55 AM.