Extracts files names and write those to another file in different directory


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Extracts files names and write those to another file in different directory
# 1  
Old 06-17-2012
Extracts files names and write those to another file in different directory

Hi , Need to shell script to extracts files names and write those to another file in different directory.

input file is
inputfile.txt
Code:
abc|1|bcd.dat
123 david123
123 rudy2345
124 tinku5634
abc|1|def.dat
123 jevid123
123 qwer2345
124 ghjlk5634
abc|1|pqr.txt
123 vbjnnjh435
123 jggdy876
124 gjksda456

the output is :
output.txt
Code:
bcd.dat
123 david123
123 rudy2345
124 tinku5634
def.dat
123 jevid123
123 qwer2345
124 ghjlk5634
pqr.txt
123 vbjnnjh435
123 jggdy876
124 gjksda456

the output.txt file needs to be in some other directory.

Thanks,
Siva Santosh

Last edited by pludi; 06-18-2012 at 08:02 AM..
# 2  
Old 06-18-2012
awk

Hi,

Try this one,
Code:
awk -v tdir="target/path" 'BEGIN{FS="|";}{if($NF){file=$3}else{print >tdir"/"fie;}}' file

Cheers,
Ranga:-)
# 3  
Old 06-18-2012
sorry..there is small mistake in the question..
Code:
abc|1|bcd.dat
123 david123
123 rudy2345
124 tinku5634
abc|2|def.dat
123 jevid123
123 qwer2345
124 ghjlk5634
abc|3|pqr.txt
123 vbjnnjh435
123 jggdy876
124 gjksda456

i dont know how many will come like this..
but always the pattern is same like abc|3|pqr.txt ...3rd element is filename which we need to extracts these file name with the associated data to some other file which should be another directory..

Thanks again..
Siva Santosh

Last edited by Scrutinizer; 06-19-2012 at 04:20 AM.. Reason: code tags
# 4  
Old 06-18-2012
Have you tried the above command.?
# 5  
Old 06-19-2012
I dont have a one line answer.. try this..

Code:
#!/usr/bin/bash
grep '\.' inputfile.txt |  cut -d'|' -f3 > tmp
total_lines=`wc -l tmp | awk {'print $1'}`
for (( i=0; i<$total_lines; i++ ))
do
    first_file=`awk "NR==$i" tmp`;
    j=$(($i+1));
    second_file=`awk "NR==$j" tmp`;
    echo "count i is $i : $first_file and $second_file";
    sed -n "/$first_file/,/$second_file/p" inputfile.txt | egrep -v "$first_file|$second_file" > $first_file
done
rm tmp;

PS: There is a logical flaw. Last file wont be written. Hope you can rectify it..
# 6  
Old 06-19-2012
Try:
Code:
awk -F\| '{print $NF}' file > newfile

# 7  
Old 06-19-2012
my script will write multiple files as contained in the inputfile.txt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directory containing files,Print names of the files in the directory that are exactly same content.

Given a directory containing say a few thousand files, please output a list of all the names of the files in the directory that are exactly the same, i.e. have the same contents. func(a_directory_name) output -> {“matches”: , ... ]} e.g. func(“/home/my/files”) where the directory... (7 Replies)
Discussion started by: anuragpgtgerman
7 Replies

2. Shell Programming and Scripting

Compare Only "File Names" in 2 Files with file lists having different directory structure

I have a tar arcive arch_all.tar.gz and 4 batched tar archive . These batches are supposed to have all the files form arch1.all.tar.gz arch1_batch1.tar.gz arch1_batch2.tar.gz arch1_batch3.tar.gz arch1_batch4.tar.gz my issue is that the directory structure in "arch_all.tar.gz" is... (6 Replies)
Discussion started by: sumang24
6 Replies

3. Shell Programming and Scripting

How to list files names and sizes in a directory and output result to the file?

Hi , I'm trying to list the files and output is written to a file. But when I execute the command , the output file is being listed. How to exclude it ? /tmp file1.txt file2.txt ls -ltr |grep -v '-' | awk print {$9, $5} > output.txt cat output.txt file1.txt file2.txt output.txt (8 Replies)
Discussion started by: etldeveloper
8 Replies

4. Shell Programming and Scripting

Remove all files with specific file names in directory

If I have 5 files in a directory, what is the best way to remove specific files in it? For example, snps.ivg probes.ivg Desired output probes.ivg probes.txt all.txt Basically, removing those files with "snp" in the filename regardless of extension. Thank you :). (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

6. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

7. Shell Programming and Scripting

How to find empty files in a directory and write their file names in a text?

I need to find empty files in a directory and write them into a text file. Directory will contain old files as well, i need to get the empty files for the last one hour only. (1 Reply)
Discussion started by: vel4ever
1 Replies

8. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

9. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

10. Shell Programming and Scripting

How to have a Script that extracts files?

Hi, I know there is a way to have a script that include files in a way that once we run the script, it would extract those files. Kinda similar to how zip files work. For example: - I have a script called test.sh - Once I run this script, it will extract file1, file2 and file3 and then... (2 Replies)
Discussion started by: pgarousi
2 Replies
Login or Register to Ask a Question