LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Accessing file, and manipulating with with folders (https://www.linuxquestions.org/questions/programming-9/accessing-file-and-manipulating-with-with-folders-795071/)

adamelody 03-12-2010 09:21 PM

Accessing file, and manipulating with with folders
 
Hi!

I need help.

I have a .csv file with a list of Drugs Name that i need to remove from the folder. The folder consist of files that stored as drugname.mol format.

What i need to do is to sieve out those mol files from the folder?

How do i
(1) access the drugs names from the .csv file line by line (variable x)
(2) how do i access the files in the folder one by one (variable y)
(3) how can i do a comparison whether $x.mol == $y
(4) and shift them to another folder if $x.mol == $y


Hope u understand my questions, and thanks very much for the help.

worm5252 03-12-2010 09:58 PM

This may be a better question for the Programming forums as it sounds like you want to write a shell script. It does sound like it may be a simple shell script, something similar to this:
Code:

#!/bin/bash

cat file.csv | while read line; do  ##Read the csv file line by line
    SEARCH=`ls /path/with/mol/files | grep $line`  ##Search directory listing for the line read in the csv file
    if [ -n "$SEARCH" ] then 
          mv $y /path/of/new/folder ##If the SEARCH Variable is not null do this
    else
          do nothing ## If the SEARCH variable is null then do this
    fi
done


adamelody 03-12-2010 10:29 PM

Accessing file, and manipulating with with folders
 
Hi!

I need help.

I have a .csv file with a list of Drugs Name that i need to remove from the folder. The folder consist of files that stored as drugname.mol format.

What i need to do is to sieve out those mol files from the folder?

How do i
(1) access the drugs names from the .csv file line by line (variable x)
(2) how do i access the files in the folder one by one (variable y)
(3) how can i do a comparison whether $x.mol == $y
(4) and shift them to another folder if $x.mol == $y


is there any simple unix scripting that can be used? or maybe C, C++ or perl?


Hope u understand my questions, and thanks very much for the help.

worm5252 03-12-2010 10:33 PM

Sorry I just reread this code and it does a call to $y but $y has not been defined. Here is an update

Code:

#!/bin/bash

cat file.csv | while read line; do  ##Read the csv file line by line
    y=$line
    SEARCH=`ls /path/with/mol/files | grep $line`  ##Search directory listing for the line read in the csv file
    if [ -n "$SEARCH" ] then 
          mv $y /path/of/new/folder ##If the SEARCH Variable is not null do this
    else
          do nothing ## If the SEARCH variable is null then do this
    fi
done


H_TeXMeX_H 03-13-2010 03:16 AM

What is in the csv file ? Is it just one column ? If so, you can just use a while read line loop and remove those that are not in the folder, or rather move the ones that you want into a different folder.

bash should work, unless you want something else.

Kenny_Strawn 03-13-2010 03:44 AM

What about:

Code:

$ sudo rm -irf *.mol
in the directory that they (the .mol files) are in?

grail 03-13-2010 08:55 AM

Hi adamelody

My first question is what have you tried?
If you need a direction I would suggest looking into tutorial pages on bash and / or awk.

If you have tried something and are stuck, then let us know where?

Otherwise it might be miscontrued that this is homework or an assignment you would like us to do for you :(

adamelody 03-13-2010 09:20 AM

thanks!!!! its solved!

worm5252 03-13-2010 10:20 AM

No problem. Glad I could help you

Mara 03-13-2010 11:46 AM

Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.

pixellany 03-13-2010 01:08 PM

I've merged your two duplicate threads---One thread per topic, pleas

adamelody 03-14-2010 10:32 AM

Thanks. I have managed to solve it. manipulating files was alright, but i have no idea how to "move" the files.


Thanks for your help anyway!



Quote:

Originally Posted by grail (Post 3896767)
Hi adamelody

My first question is what have you tried?
If you need a direction I would suggest looking into tutorial pages on bash and / or awk.

If you have tried something and are stuck, then let us know where?

Otherwise it might be miscontrued that this is homework or an assignment you would like us to do for you :(



All times are GMT -5. The time now is 11:30 PM.