Reading and writing data to and from multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reading and writing data to and from multiple files
# 1  
Old 01-30-2011
Reading and writing data to and from multiple files

Hi,

I have several text files. One main file contains the detail data, other have some information to extract data from the main file, and some are empty files. Examples are shown below:
The main file look like:
MainFile.txt
HTML Code:
>Header1
data1...data1...
>Header2
data2...data2...
...
...
>Header100
data100...data100...
etc.
And the other file looks like:
OtherFile1.txt
HTML Code:
>Header1
OtherFile2.txt
HTML Code:
>Header9
OtherFile3.txt
HTML Code:
[Blank File. no data]
Some of the OtherFiles contain a '>Header', which refer to the '>Header' and it's associated data in the 'MainFile'. For each of this file I want to extract the associated data from the 'MainFile'. Example Output:

OtherFile1.txt
HTML Code:
>Header1
data1...data1...
data1..data1...
OtherFile9.txt
HTML Code:
>Header9
data9...data9...
Thanks for any help or suggestions.
# 2  
Old 01-30-2011
Try:
Code:
awk 'NR==FNR{A[RS$1]=$0;next}A[$1]{print ">"A[$1];next}1' RS=\> infile RS="\n" otherfile

# 3  
Old 01-30-2011
Hi,
Thanks for your reply.
I've tried your code:
awk 'NR==FNR{A[RS$1]=$0;next}A[$1]{print ">"A[$1];next}1' RS=\> MainFile.txt RS="\n" OtherFile*.txt

However, it's just giving me he '>Header', not both '>Header' and 'data'.
Cureent Output:
>Header1
>Header2
>Header7
etc.

Required Output:
>Header1
data1...data1...
data1..data1...
>Header2
data2...data2...
data2..data2...

Thanks.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on writing data from 2 different files to one based on a common factor

Hello all, I have 2 text files. For example: File1.txt contains data A B C D ****NEXT**** X Y Z ****NEXT**** L M N and File2.txt contains data (13 Replies)
Discussion started by: vat1kor
13 Replies

2. UNIX for Dummies Questions & Answers

Writing a for loop to manipulate multiple files

Hi, I have 1000 text files in a folder that are labeled data1.txt all the way to data1000.txt. I want to write a small script that manipulates the text files in this way: (1) cut the 2nd and 9th columns of the text files (2) sort by the numerical value in the 9th column (3) then save the rows... (3 Replies)
Discussion started by: evelibertine
3 Replies

3. Shell Programming and Scripting

Writing a Perl Script that processes multiple files

I want to write a Perl script that manipulates multiple files. In the directory, I have files 250.*chr$.ped where * is from 1 to 1000 and $ is from 1-22 for a total of 22 x 10,000 = 22,000 files. I want to write a script that only manipulates files 250.1chr*.ped where * is from 1 to 22.... (10 Replies)
Discussion started by: evelibertine
10 Replies

4. Shell Programming and Scripting

Difference of two data files & writing to an outfile.

Hi Everyone, I have two files i.e. one file2 contains today's data and the other file1 contains Yesterday's data. The data in the files contains 226 columns and the data for the coulums separated by a Pipe "|" delimiter. Now, I have 4 Primary keys (coulumns) by which I have to compare file2 and... (10 Replies)
Discussion started by: filter
10 Replies

5. UNIX for Dummies Questions & Answers

Using AWK: Extract data from multiple files and output to multiple new files

Hi, I'd like to process multiple files. For example: file1.txt file2.txt file3.txt Each file contains several lines of data. I want to extract a piece of data and output it to a new file. file1.txt ----> newfile1.txt file2.txt ----> newfile2.txt file3.txt ----> newfile3.txt Here is... (3 Replies)
Discussion started by: Liverpaul09
3 Replies

6. Shell Programming and Scripting

Reading data from DataBase and Writing to a file

Hi All, Please help me in writing data to a file in one row. In database there is a column which contains large data which does not fit in the file in one row. The column contains list of paths. I want to write these paths to a file in one row. Please find the code below writes : ... (2 Replies)
Discussion started by: rajeshorpu
2 Replies

7. Shell Programming and Scripting

Writing out 2nd column into one file from multiple files

I have several files that are being generated every 20 minutes. Each file contains 2 columns. The 1st column is Text, 2nd column is Data. I would like to generate one single file from all these files as follows: One instance of 1st column Text, followed by 2nd column Data separated by... (5 Replies)
Discussion started by: subhap
5 Replies

8. Shell Programming and Scripting

Reading data from multiple tables from Oracle DB

Hi , I want to read the data from 9 tables in oracle DB into 9 different files in the same connection instance (session). I am able to get data from one table to one file with below code : X=`sqlplus -s user/pwd@DB <<eof select col1 from table1; EXIT; eof` echo $X>myfile Can anyone... (2 Replies)
Discussion started by: net
2 Replies

9. UNIX for Dummies Questions & Answers

Reading and Writing Files ?

Hello, Our group is just starting to get into UNIX here. We are former REXX users on a VM Mainframe. Can someone point me to the documentation to read a file and write a file in a Unix Shell Script or does this have to be done in another language? Thank you in advance... Dave (3 Replies)
Discussion started by: tracydp
3 Replies

10. Programming

mmap vs shared memory - which is faster for reading data between multiple process

Between mmap and shared memory which is the best method of sharing data between multiple applications, interms of speed? (1 Reply)
Discussion started by: nmds
1 Replies
Login or Register to Ask a Question