I have one source folder "Source" and 2 target folders "Target_123456" & "Target_789101". I have 2 series of files. 123456 series and 789101 series. Each series has got 3 types of fiels "Debit", "Refund", "Claims".
All files are getting created in Source folder. I request for a unix script to open the file and check for its series (wheter its 123456 or 789101 series) number in its header and move the files to its corresponding Target folders "Target_123456" OR "Target_789101".
Please Note:
for DEBIT.DAT file the series number is available in line 1 i.e �VOL1000001 123456 �
for Claim & Refund files the series number is available in line 1 i.e �PWRD002123456 123456 00�
Upper case, lower case, or mixed case file names? However, try
for FN in *.dat; do read X SERIES X <"$FN"; echo mv "$FN" "Target_$SERIES/$FN"; done
mv Claim25112016222930.dat Target_789101/Claim25112016222930.dat
mv Debit25112016222930.dat Target_789101/Debit25112016222930.dat
mv Refund25112016222930.dat Target_789101/Refund25112016222930.dat
I am new to unix and not sure how to execute this. My requirement is to
Script should check for .dat files in Source folder.
Check for specific text (645559 or 508659) in first line of each file.
If 645559 exists, move the files to folder SUN_ID_645559
If 508659 exists, move the files to folder SUN_ID_508659
I am able to achieve this using java for windows platform. In jave we can hard code source and target directories. Write if conditons to search for the number in the first line of a file and if exists we can call a method to move this file to its corresponding folder.
I am trying to achive the same using unix as the requirement is to move the files in Unix and prefered solution is to use unix script. Not sure how to set variables for source and target folders? If condition for check for specific text in the first line and move the file to specific folders accordingly.
package com.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
public class MoveSunIDFiles {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File src = new File("C:\\Users\\akellap\\RND\\Test\\Source");
// create new filename filter for .dat files
File[] result = src.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name.lastIndexOf('.') > 0) {
// get last index for '.' char
int lastIndex = name.lastIndexOf('.');
// get extension
String str = name.substring(lastIndex);
// match path name extension
if (str.equals(".dat")) {
return true;
}
}
return false;
}
});
if (result != null && result.length > 0) {
for (int i = 0; i < result.length; i++) {
BufferedReader in = null;
try {
in = new BufferedReader(
new FileReader(result));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String line = "";
try {
while ((line = in.readLine()) != null) {
if (line.contains("645559")) {
move645559(result);
break;
}
if (line.contains("508659")) {
move508659(result);
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else if (result == null) {
System.out.println("Invalid filename or folder");
} else {
System.out.println("No files matching the criteria");
}
}
private static void move508659(File result) {
// TODO Auto-generated method stub
if (result.renameTo(new File(
"C:\\Users\\akellap\\RND\\Test\\Target\\SUN_ID_508659\\"
+ result.getName()))) {
System.out.println("File:" + result + " moved successfully");
} else {
System.out.println("File " + result.getName() + " not moved");
}
}
private static void move645559(File result) {
// TODO Auto-generated method stub
if (result.renameTo(new File(
"C:\\Users\\akellap\\RND\\Test\\Target\\SUN_ID_645559\\"
+ result.getName()))) {
System.out.println("File:" + result + " moved successfully");
} else {
System.out.println("File " + result.getName() + " not moved");
}
}
}
I have copied below lines of code into a notepad and saved as Script.sh. Placed the file in /Source folder.
for FN in *.dat; do read X SERIES X <"$FN"; echo mv "$FN" "SUN_ID_$SERIES/$FN"; done
mv Claim25112016222930.dat SUN_ID_789101/Claim25112016222930.dat
mv Debit25112016222930.dat SUN_ID_789101/Debit25112016222930.dat
mv Refund25112016222930.dat SUN_ID_789101/Refund25112016222930.dat
Placed around 100 .dat files in Source folder. Executed the script. I got below error.
-bash: Script.sh: command not found
Does below code means this script will handle only these 3 files?
for FN in *.dat; do read X SERIES X <"$FN";
mv "$FN" "SUN_ID_$SERIES/$FN";
done
Will check for .dat files in Source folder.
Check for specific text (645559 or 508659) in first line of each file.
If 645559 exists, move the files to folder SUN_ID_645559
If 508659 exists, move the files to folder SUN_ID_508659
You can look into mv 's -v (verbose) option to log what is actually done.
While I could post some error handling for this very basic snippet, I'd suggest you play around with e.g. if [ "$SERIES" -ne xxxxxx ] or if [ $? -ne 0 ] as an exercise to improve your shell scripting.
I am receiving files in below format(.DAT followed by 4 numbers). I the filter to *.DAT* or *.DAT???? . files are not getting picked tried searching for this in forums. No luck.
Sample1.DA9847
Sample2.DAT3498
Sample3.DAT9867
Observed one thing. This script is also picking the files which are getting copied along with existing files.
e.g. File A is existing and File B is getting copied (30% copied). When executed this script is moving File A (100%) and file B (30% copied) to target location. Is there anything to restrict in script to move only fully copied files?