Help with Complex Bash Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Complex Bash Script
# 1  
Old 01-31-2017
Error Help with Complex Bash Script

I have an FTP server with thousands of Invoices. All Invoices are in a folder called /volume1/MBSInvoices/

Monthly invoices are added to that folder every month.

Here is a sample filename of the Invoices:
Code:
invoice_1_20170101_10010052_10020052_10030052_JOHNDOE.pdf

the Account ID is the 6th segment of the name (10030052)

I would like to create a bash script that does the following:

Check the 6th segment in the file name. If a folder with that name doesn't exist, create one. Otherwise don't create one.

Move those Invoices to their correspondent folders based on the 6th segement on the invoice name

Example:

Code:
invoice_1_20170101_10010052_10020052_10030052_JOHNDOE.pdf

The script will check the file name, specifically the 6th segment.
If the a folder named 10030052 doesn't exist, create a new one.
Then move the file invoice_1_20170101_10010052_10020052_10030052_JOHNDOE.pdf to folder 10030052

If the folder named 10030052 already exists,
move the file invoice_1_20170101_10010052_10020052_10030052_JOHNDOE.pdf to folder 10030052

The script needs to do this for over 600 files. The script will be run once a month as new monthly invoices become avaiable.

Thanks



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-31-2017 at 12:28 PM.. Reason: Added CODE tags.
# 2  
Old 01-31-2017
Welcome to the forum.

Any attempts / ideas / thoughts from your side?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-31-2017
Hi RudiC, I have no idea as I'm a newbie to bash scripting.


Thanks

Badr
# 4  
Old 01-31-2017
Well, try
Code:
for FN in *.pdf
   do   TMP=${FN#*_*_*_*_*_}
        TMP=${TMP%%_*}
        mkdir "$TMP"
        echo mv -v "$FN" "$TMP"
   done

The echo is for safety reasons; remove it if you're happy with the results it proposes. No error checking is done; add some decent check before using it in production. Esp., we make use of the behaviour of mkdir failing without further complications should the directory already exist, but it could fail for other reasons.
# 5  
Old 01-31-2017
use below code which wills serve your purpose.

Code:
#!/bin/bash
BASE_DIR=/Users/karan/Practice_Shell_Scripting/Move_Invoice
SOURCE_DIR=/Users/karan/Practice_Shell_Scripting/Move_Invoice/Source_DIR
TARGET_DIR=/Users/karan/Practice_Shell_Scripting/Move_Invoice/Target_DIR

rm ${BASE_DIR}/FIle_list.txt

find ${SOURCE_DIR}/ -name "*.pdf" >> ${BASE_DIR}/FIle_list.txt

for i in `cat ${BASE_DIR}/FIle_list.txt`
do
	FILE_NAME=`basename $i`
	FOLDER_NAME=`basename $i| awk -F "_" '{print $6}'`
	echo ${FILE_NAME}
	echo ${FOLDER_NAME}
	if [ -d "${TARGET_DIR}/${FOLDER_NAME}" ]
	then
		mv $i ${TARGET_DIR}/${FOLDER_NAME}/
	else
		mkdir -p ${TARGET_DIR}/${FOLDER_NAME}
		mv $i ${TARGET_DIR}/${FOLDER_NAME}/
	fi
done


Last edited by Corona688; 01-31-2017 at 02:07 PM..
# 6  
Old 01-31-2017
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 7  
Old 01-31-2017
kpkanani, Thanks a Million. The scripts works exactly the way we need it to.

Rudi, Thanks for your help too.

Badr
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Complex: bash, using ANSI-C quotes in skript

Hello, I hope someone can hep with this. I use a skript to send multiline Data to a Monitoring system. Bu I'm not able to use linebreaks or escape sequences. The skript is simple like that: #!/bin/bash var="Erste Zeile \n zweite Zeile \n Dritter Teil" zabbix_sender -c... (17 Replies)
Discussion started by: mpmichael
17 Replies

3. Shell Programming and Scripting

Complex calulation in shell script

Hi , I need one help to do some complex calculation in shell script. here is what i need to do:- a=2 b=2 c=2 d=2 result=a+(b/(20*c))+(c/(10*d)) is there any thing special there so that i can group intermdiate results. Please help me if you have any idea. (4 Replies)
Discussion started by: harpal singh
4 Replies

4. Shell Programming and Scripting

Complex bash/sed, variables and nested quotes

Ok, this one isn't for everybody, it's pretty tough and I've spent a good deal of time on it without figuring it out yet. Can anybody get this script to work: #!/bin/bash cq_fname="%let outputfile="/user/cq_"$1".csv";" sed "29s/.*/\"$cq_fname\"/" file1.sas >... (3 Replies)
Discussion started by: nocloud
3 Replies

5. Shell Programming and Scripting

awk script (complex)

picked this up from another thread. echo 1st_file.csv; nawk -F, 'NR==FNR{a++;next} a{b++} END{for(i in b){if(b-1&&a!=b){print i";\t\t"b}else{print "NEW:"i";\t\t"b} } }' OFS=, 1st_file.csv *.csv | sort -r i need to use the above but with a slight modification.. 1.compare against 3 month... (25 Replies)
Discussion started by: slashbash
25 Replies

6. Shell Programming and Scripting

Difficult problem: Complex text file manipulation in bash script.

I don't know if this is a big issue or not, but I'm having difficulties. I apoligize for the upcoming essay :o. I'm writing a script, similar to a paint program that edits images, but in the form of ANSI block characters. The program so far is working. I managed to save the image into a file,... (14 Replies)
Discussion started by: tinman47
14 Replies

7. Shell Programming and Scripting

Complex Script

hey... i had a big problem with my professor i have 3 simple archives in.txt -> had all timestamps of users logon (100lines) ex. 111111 222222 333333 out.txt -> had all timestamps of users logof (100lines) ex. 111113 222225 333332 commands.txt... (9 Replies)
Discussion started by: beandj
9 Replies

8. Shell Programming and Scripting

Complex coloring in script

My script prints lines in which the entire line may be colored, and portions may also be colored. e.g. Consider this to be one line: $red some text in red $yellow abcd $end_yellow red text 1234 $blue some text $end_blue more red text $end_red So using sed, I may based on condition 1,... (5 Replies)
Discussion started by: sentinel
5 Replies

9. Shell Programming and Scripting

complex find in script

How to I put my find command string into a script. It is currently to long to be entered manually at command line. for FNAME in `find /unixsxxx/interface/x.x/xxxxxx -type f \( -name '*.KSH' -o -name '*.sh' -o -name '*.sql' -o -name '*.ksh' \) -exec grep -il xxx.xxx.xxx.xxx {} \;`; do C=`grep -c... (5 Replies)
Discussion started by: TimHortons
5 Replies

10. Shell Programming and Scripting

Need complex script, anyone up for a challenge?

Default shell is /usr/bin/zsh Script will be running #!/bin/bash Need to pull information from database while using other scripts already made (not by me). Ok, so i need a script pulling certain information about a customer's router interfaces. I am using a ROUTER-DNS-NAME as variable $1 I... (3 Replies)
Discussion started by: ///NNM
3 Replies
Login or Register to Ask a Question