Look up file in list and write its path into another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Look up file in list and write its path into another file
# 1  
Old 07-29-2011
Look up file in list and write its path into another file

Hi

I have a script that writes the path of files into a text file. I'd like to extend it so that it writes only the paths of certain files, which are given on a list, into this text file.

The initial code is as follows:

Code:
for file in `ls /path/*.lab` ; do
    base=`basename $file .lab` ; 
    wavfile=/otherpath/$base.wav ;
    if [ -e $wavfile ] ; then
        echo $wavfile >> train.testvoice_11.wav ;
        echo $file >> train.testvoice_11.lab ;
    fi
done

I wanted to define the list at the top and then write a loop like
Code:
list=xy.txt
for file in `ls /path/*.lab` ; do
     if $file in list ; do
            echo $file >> train.testvoice_11.lab ;
     done
done
wavfile=/otherpath/$base.wav ;
if [ -e $wavfile ] ; then
    echo $wavfile >> train.testvoice_11.wav ;
fi

Can someone help please?

Cheers!
# 2  
Old 07-29-2011
Just take the comparable parts of each file name and grep for supporting lines in the list file. Make sure your pattern has ^ and $ or -x so you do hot hit on prefix or suffix matches.
# 3  
Old 07-30-2011
Hmm...the filenames look the same except from their numeration:

testfile_1
testfile_2
testfile_3

So if I just want testfile_1 and testfile_3 I don't really know how to grep for these lines

---------- Post updated 07-30-11 at 09:04 AM ---------- Previous update was 07-29-11 at 12:54 PM ----------

Am still not any further. Is there noone who can help?
# 4  
Old 07-30-2011
???
Code:
cat >list.file
testfile_1
testfile_2
testfile_3

touch testfile_1 testfile_3

for f in testfile*; do
  if grep -qx "$f" list.file; then
    echo "$f"
  fi
done

testfile_1
testfile_3

OR:
Code:
cat >list.file
testfile_1
testfile_3

touch testfile_1 testfile_2 testfile3

...

The results are the same though.

Last edited by yazu; 07-30-2011 at 12:01 PM..
# 5  
Old 07-31-2011
Hm...that doesn't seem to work for me...

When I do this:

Code:
cat >file
touch ./path/dir/*
for f in ./path/dir/* ; do
  if grep -qx "$f" file; then
    echo "$f"
  fi
done

nothing happens at all...
# 6  
Old 07-31-2011
Oh... I wrote not exactly what you should do but just as an example to show that you can use "grep -qx ..." instead of "f in list".
# 7  
Old 07-31-2011
Ah, alright! Will try to relate that to my own code then.
If someone else has a more specific idea how I could change my code so that it works, I would appreciate that. I'm relatively new to shell scripting and still have problems to think around corners ;-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I add a log file path to a vi file to monitor all the changes made to the file?

I'm curious to know how do I add an empty log file (test1.log) to an existing text file to monitor all the changes made to a.txt. Is this expression export PATH=$PATH:/home/test1.log right to be added to the text file a.txt? (5 Replies)
Discussion started by: TestKing
5 Replies

2. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

3. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

4. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

5. UNIX for Dummies Questions & Answers

Removing path name from list of file names

I have this piece of code printf '%s\n' $pth*.msf | tr ' ' '\n' | sort -t '-' -k7 -k6r \ | awk -F- '{c=($6$7!=p&&FNR!=1)?ORS:"";p=$6$7}{printf("%c%s\n",c,$0)}' When I run it I get /home/chrisd/tatsh/branches/terr0.50/darwin/n02-z30-dsr65-terr0.50-dc0.002-8x6drw-csq.msf... (8 Replies)
Discussion started by: kristinu
8 Replies

6. Shell Programming and Scripting

Write a scripts to kill idle user for 60 min. & email user list to admin in text file

Folks, I have written one script for following condition by referring some of online post in this forum. Please correct it if I'm missing something in it. (OS: AIX 5.3) List the idle user. (I used whoidle command to list first 15 user and get username, idle time, pid and login time).... (4 Replies)
Discussion started by: sumit30
4 Replies

7. UNIX for Advanced & Expert Users

Rsync building file list/catalog path/location

Where is the file list created by rsync when it says building file list ? (1 Reply)
Discussion started by: glev2005
1 Replies

8. Shell Programming and Scripting

list file with full path

This has been bugging me for a while. How can i list file to show full path. /directory/test $ ls file.tst file.tst $ desired output: /directory/test/file.tst (2 Replies)
Discussion started by: ryandegreat25
2 Replies

9. UNIX for Advanced & Expert Users

list all files with full path of the file

How can i list every single file on a sun solaris server running 2.8 starting from '/' with the full path included in it? example. / ... ... ... /etc/inetd.conf /etc/passwd /etc/shadow ... ... ... /var/adm/messages /var/adm/messages.0 /var/adm/messages.1 ... ... ...... (4 Replies)
Discussion started by: Sowser
4 Replies

10. UNIX for Dummies Questions & Answers

vi - replacing a relative path with absolute path in a file

Hi, I have a file with about 60 lines of path: app-defaults/boxXYZ....... I want to change this to /my/path/goes/here/app-defaults/boxXYZ, but of course vi doesn't like the regualr :s/old/new/ command. Is there any other quick way to do this? Thanks ;) (2 Replies)
Discussion started by: Yinzer955i
2 Replies
Login or Register to Ask a Question