Script for linking files with paths in 2 text files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for linking files with paths in 2 text files
# 1  
Old 03-06-2014
Script for linking files with paths in 2 text files

I have 2 txt files, 1.txt and 2.txt which contain the paths to files that need to be linked.

Example 1.txt:
Code:
/root/001/folder2/image4.nii.gz
/root/002/folder2/image4.nii.gz

Example 2.txt:
Code:
/root/001/folder2/image5.nii.gz
/root/002/folder2/image5.nii.gz

Each line represents images from one subject, so i need to make a link to image4.nii.gz called image5_done.nii.gz (same name as image5, but with _done at the end). Links should be made in the same directory for each subject. I'm very new to scripting and this is something I've been unsuccessfully struggling with for a while. Help is much appreciated.

Last edited by Scrutinizer; 03-06-2014 at 08:42 AM.. Reason: code tags
# 2  
Old 03-06-2014
Can you show us what you have tried?
# 3  
Old 03-06-2014
Code:
for file in `cat 1.txt`; do
	ln -si $file ./root/00*/folder2/*5*_done.nii.gz
done

# 4  
Old 03-06-2014
The way I use the variable ${COUNT} in the awk command isn't proper (and that will be pointed out by others since I can never remember how it goes) but this works:
Code:
#!/bin/ksh
COUNT=1
while read LNKFILE1
do
    LNKFILE2=$(awk 'NR == "'${COUNT}'"' 2.txt)
    ln -si ${LNKFILE1} ${LNKFILE2}_done
    COUNT=$(($COUNT + 1))
done < 1.txt


Last edited by port43; 03-06-2014 at 10:10 AM..
# 5  
Old 03-06-2014
@LeftoverStew, tt is a bit more complicated than that. A link in the same directory, means making the link to the file name only (without the path). Also, there are all sorts of error condition that might occur. Also since the match is made line for line by the content of the files, they need to be read at the same time.

For example:

Code:
complain() {
  printf "%s\n" "$*"
  continue
}

while read file && read link<&3
do
  # determine directories
  dir_file=${file%/*}
  dir_link=${link%/*}

  # determine name of new link
  new_link=${link%.*.*}_done.${link#*.}

  # determine name the local link should point to
  local_file=${file##*/}
  
  # complain if there are things wrong
  [ -e "$file" ]                || complain "file $file does not exist"
  [ "$dir_file" = "$dir_link" ] || complain "The directories of $file and $link are not the same, so a local symlink cannot be made"
  [ ! -e "$new_link" ]          || complain "An entry for $new_link already exists"

  # create the link
  ln -s "$local_file" "$new_link"
done < 1.txt 3<2.txt


Last edited by Scrutinizer; 03-06-2014 at 11:10 AM..
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-06-2014
Scrutinizer, you are an awesome human being. I am forever grateful, it does exactly what I wanted!!!
# 7  
Old 03-07-2014
Hi Scrutinizer,

Could you please explain about file descriptor 3 ?

Thanks
Pravin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Linker errors linking to .a files on OS X

Basically my problem is that when I try to compile anything using ./configure && make, it fails because of linker errors. I can reproduce the behavior I'm getting as follows: I have the two following files main.c: #include <stdio.h> extern void func(void); int main(int argc, char... (5 Replies)
Discussion started by: MarshallBanana
5 Replies

2. Shell Programming and Scripting

Search and compare files from two paths

Hi All, I have a 2 path, one with oldfile path in which has several sub folders,each sub folders contains a config file(basically text file), likewise there will be another newfile path which will have sub folders, each sub folders contains a config file. Need to read files from oldfile... (6 Replies)
Discussion started by: Optimus81
6 Replies

3. Shell Programming and Scripting

Script to delete files older than x days and also taking an input for multiple paths

Hi , I am a newbie!!! I want to develop a script for deleting files older than x days from multiple paths. Now I could reach upto this piece of code which deletes files older than x days from a particular path. How do I enhance it to have an input from a .txt file or a .dat file? For eg:... (12 Replies)
Discussion started by: jhilmil
12 Replies

4. Shell Programming and Scripting

how to write a script for existing of IP_add in the files under paticular paths.

Hello , I want to write a script to test the IP_add is hardcoded in the paticular files. the files are available in path . for example. /abs/abn/jkg/frames.sh. like this some files are there in the same path.I manually replaced the IP_ADD with domain name in required files.now i have to... (1 Reply)
Discussion started by: rajkumar_g
1 Replies

5. Shell Programming and Scripting

Find text containing paths and replace with a string in all the python files

I have 100+ python files in a single directory. I need to replace a specific path occurrence with a variable name. Following are the find and the replace strings: Findstring--"projects\\Debugger\\debugger_dp8051_01\\debugger_dp8051_01.cywrk" Replacestring--self.projpath I tried... (5 Replies)
Discussion started by: noorsam
5 Replies

6. UNIX Desktop Questions & Answers

how to display paths of files in a directory

hi guys does anyone know how to display the file paths of the files stored within a directory at the command terminal? e.g. if i have a directory called "home", how do i display the file paths of the files inside the directory? cheers (2 Replies)
Discussion started by: Villaman69
2 Replies

7. Programming

C files searching and Linking

Dear friends, First off all , let me apologize for my inexperience. I am just starting use of Linux and gcc . Actually I ve some .c files in the present directory , and now I am giving +vc <my_file1.c> <my.file2.c> <myfile3.c>. All the c files are in present directory.... (2 Replies)
Discussion started by: user_prady
2 Replies

8. UNIX for Dummies Questions & Answers

Linking Files in UNIX (shortcuts)

Hello, Can anyone tell me how I can make a file link or shortcut in UNIX 4.0, several file links where damaged during an outage examples below. Examples: file -> file libX11.so -> /usr/shlib/libX11.so.pre.O3D All of the file links that were damaged were in /shlib and point to... (3 Replies)
Discussion started by: jays337
3 Replies

9. UNIX for Dummies Questions & Answers

modifying C file and linking back to project files

hi, This is the first time I work in a big C project. All source code files are located in say directory /source/pp and all header files are in /include/pp. I've created a link to both of these directories from my home dir, say /home/ss. So in the /home/ss dir I have the /source/pp and /include/pp... (1 Reply)
Discussion started by: bruins2005
1 Replies

10. Programming

compiling and linking 2 C files ...??

Hi mates, I am trying to copile and link to C programs with command: cc file1,file2 but i raises the error "file not found" ... am i doing the right way? any suggestion will be appreciated. thanks abdul (4 Replies)
Discussion started by: abdul
4 Replies
Login or Register to Ask a Question