Need help in sorting the file and putting to a different folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in sorting the file and putting to a different folder
# 1  
Old 10-26-2018
Need help in sorting the file and putting to a different folder

Hi,


I am new to unix. Kindly help me on this.


My requirement is as below:


I have a path temp/input , temp/CL and temp/CM


I have files in temp/input as below (dates in YYYYMMDDHHMISS)


Code:
  NMP1515O.CL.20181026111213
  NMP1515O.CM.20181025111213
  NMP1515O.CM.20181024111213
  NMP1515O.CL.20181023111213

****Basically I need to sort on basis of CL and timestamp. Similarly for CM and timestamp. Move only one file from CL and CM to relevant folders i.e temp/CL and temp/CM *******


Before moving the file from temp/input to temp/CL, we need to check if there is any file in the folder temp/CL. If there is file already in temp/CL, then we should not move from temp/input to temp/CL. File should be moved only when temp/CL is empty.


Same way we need to follow for CM.


Could you kindly help.


Thank you,
Priya

Last edited by Corona688; 10-26-2018 at 12:36 PM.. Reason: Code tags for code please
# 2  
Old 10-26-2018
Let me see if I have this straight. You have file names in temp/input like temp/input/NMP1515O.CL.20181026111213, temp/input/NMP1515O.CM.20181025111213, etc, which you need to process in order of date by sorting into temp/CM and temp/CL. Only move a file when these folders are empty (implying the files we move in there will also disappear automatically).

Have I got that right?
# 3  
Old 10-26-2018
If I do understand it, then maybe:

Code:
ls temp/input | sort -t. -k 3,3 | while IFS="." read NMP TYPE DATE
do
        case "$TYPE" in
        CL) ;; CM) ;; *) echo "Wrong type $TYPE" >&2 ; exit 1 ;;
        esac

        while [ "$(ls "temp/$TYPE" | wc -l)" -ne 0 ]
        do
                sleep 0.1 # Some systems can't sleep fractional seconds
        done

        echo "Moving $NMP.$TYPE.$DATE into temp/$TYPE" >&2
        echo mv "temp/input/$NMP.$TYPE.$DATE" "temp/$TYPE"
done


Last edited by Corona688; 10-26-2018 at 04:44 PM.. Reason: typo
# 4  
Old 10-26-2018
Let me paraphrase your request:
Move exactly one "CM" and one "CL" file to the respective directory (as, once a file has been moved, the directory is not empty any more). You did not specify anything about a sort order. How about
Code:
if ! ls temp/CM/* &>/dev/null ; then  echo mv -v $(ls -rt temp/input/*CM* | head -1) temp/CM; fi 
if ! ls temp/CL/* &>/dev/null ; then  echo mv -v $(ls -rt temp/input/*CL* | head -1) temp/CL; fi

You might want to loop through all file types in a for loop.
# 5  
Old 10-29-2018
Thank you for your reply. I am writing the script. You have been very helpful :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Putting a separator in file using awk/bash

I have a file with the following content: a-123-345-232 a-23343-4545-545 a-67676-45454-8787 a-129-8912-9824 b-564-78678-2322 b-5454-76767-8899 b-85554-124-152-29 c-34534-654543-323 (... and so on, actually these are pretty huge records) Now, I want that the file should not be broken in to... (8 Replies)
Discussion started by: askerbis
8 Replies

2. UNIX for Dummies Questions & Answers

Putting the Current -date in the Output File

Hi guys, Just want to ask how can I make a script that will perform like this. 1. Execute the command 2. Then the output of the command will be redirected to a file 2. The file that has been created will have a date on it equivalent to the date and time it was created (or maybe after the... (5 Replies)
Discussion started by: rymnd_12345
5 Replies

3. Red Hat

Sorting folder size not working

I am using du -h --max-depth=2 to get list of folders by size upto 2 levels down. Problem is I am not able to sort them in max folder size. Normally this can be achieved by using du -k | sort -nr * but I can't use it here since it conflicts (the -s argument) with the --max-depth=2 argument. ... (1 Reply)
Discussion started by: rockf1bull
1 Replies

4. Shell Programming and Scripting

Putting together all values from different files in one file

Hi All, This is what I am trying to achieve but to no avail. I have three sets of files which are: 1. One big dictionary file which looks like this: apple orange computer pear country 2. Some thousands of text files which are named as 1.dat, 2.dat, 3.dat etc The text files... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

5. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

6. UNIX for Dummies Questions & Answers

Putting echoed text into a new file

Hi, I've set up a script so that a user answers questions, and then these answers come back onto the screen accompanied by text that I've echoed. Is there a way of putting this into a new file? Thanks (7 Replies)
Discussion started by: likelylad
7 Replies

7. Shell Programming and Scripting

Putting screen output in a log file

I want to output screen messages to a logfile when executing an automated script. I have tried the script and command to do this but with no luck. Thanks, Nicole (5 Replies)
Discussion started by: nsutti
5 Replies

8. Shell Programming and Scripting

Parse the .txt file for folder name and FTP to the corrsponding folder.

Oracle procedure create files on UNIX folder on a regular basis. I need to FTP files onto windows server and place the files, based on their name, in the corresponding folders. File name is as follows: ccyymmddfoldernamefile.txt; Folder Name length could be of any size; however, the prefix and... (3 Replies)
Discussion started by: MeganP
3 Replies

9. UNIX for Dummies Questions & Answers

putting a timestamp in a file

I was sure there was a way to put a timestamp ina logfile but I can't seem to figure out how. What I would like to do is after the last messages in the rptmgr.err log is put a timestamp so I know the next time I look whats new. I am using AIX 5.1 any help will great Thanks (2 Replies)
Discussion started by: rocker40
2 Replies

10. UNIX for Dummies Questions & Answers

putting restrictions when copying file

I would like to create a command to copy a file with a restriction- if the file exists at the copy destination, the copy does not occur and message is provided that file already exists. (3 Replies)
Discussion started by: thoffpauir
3 Replies
Login or Register to Ask a Question