Extracting lines from text files in folder based on the numbers in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting lines from text files in folder based on the numbers in another file
# 1  
Old 05-25-2013
Extracting lines from text files in folder based on the numbers in another file

Hello,

I have a file ff.txt that looks as follows

Code:
*ABNA.txt
356
24
36
112
*AC24.txt
457
458
321
2

ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and create new files with the existing file names in another folder foo2.
If the third or fourth column of ABNA.txt file contain 356,24,36,112 numbers, extract the lines and save it to another folder foo2 as ABNA.txt.

ABNA.txt file in the folder foo1 looks as follows

Code:
dfg  qza  356  245
hjb  hkg  455  24
ghf  qza  12   123
dfg  qza  36   55

AC24.txt file in the folder foo1 looks as follows

Code:
hjb  hkg  457  167
ghf  qza  2       165
sar  sar  234  321
dfg  qza  345  345

Output:

ABNA.txt file in the folder foo2

Code:
dfg  qza  356  245
hjb  hkg  455  24
dfg  qza  36     55

AC24.txt file in the folder foo2

Code:
hjb  hkg  457  167
ghf  qza  2        165
sar  sar   234  321


Last edited by Scrutinizer; 05-25-2013 at 06:53 AM.. Reason: code tags for the THIRD and LAST time
# 2  
Old 05-25-2013
Try something like:

Code:
cd foo1
awk '
  NR==FNR{
    if(NF>1)f=$2
    else A[f,$1]
    next
  }
  (FILENAME,$3) in A || (FILENAME,$4) in A {
    print > ( todir "/" FILENAME )
  }
' todir=/path/to/foo2 FS=\* /path/to/ff.txt FS=" " *

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-26-2013
hi scrutinizer,

Excellent!! code works well. Thank you so much for your help!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting values based on line-column numbers from multiple text files

Dear All, I have to solve the following problems with multiple tab-separated text file but I don't know how. Any help would be greatly appreciated. I have access to Linux mint (but not as a professional). I have multiple tab-delimited files with the following structure: file1: 1 44 2 ... (5 Replies)
Discussion started by: Bastami
5 Replies

2. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

3. Shell Programming and Scripting

extracting lines based on condition and copy to another file

hi i have an input file that contains some thing like this aaa acc aa abc1 1232 aaa abc2.... poo awq aa abc1 aaa aaa abc2 bbb bcc bb abc1 3214 bbb abc3.... bab bbc bz abc1 3214 bbb abc3.... vvv ssa as abc1 o09 aaa abc4.... azx aaq aa abc1 900 aqq abc19.... aaa aa aaaa abc1 899 aa... (8 Replies)
Discussion started by: anurupa777
8 Replies

4. Shell Programming and Scripting

Extracting lines based on identifiers into multiple files respectively

consider the following is the contents of the file cat 11.sql drop procedure if exists hoop1 ; Delimiter $$ CREATE PROCEDURE hoop1(id int) BEGIN END $$ Delimiter ; . . . . drop procedure if exists hoop2; Delimiter $$ CREATE PROCEDURE hoop2(id int) BEGIN END $$ (8 Replies)
Discussion started by: vivek d r
8 Replies

5. Shell Programming and Scripting

Extracting few lines from a file based on identifiers dynamically

i have something like this in a file called mysqldump.sql -- -- Table structure for table `Table11` -- DROP TABLE IF EXISTS `Table11`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Table11` ( `id` int(11) NOT NULL... (14 Replies)
Discussion started by: vivek d r
14 Replies

6. UNIX for Dummies Questions & Answers

Extracting rows from a text file based on the first column

I have a tab delimited text file where the first column can take on three different values : 100, 150, 250. I want to extract all the rows where the first column is 100 and put them into a separate text file and so on. This is what my text file looks like now: 100 rs3794811 0.01 0.3434 100... (1 Reply)
Discussion started by: evelibertine
1 Replies

7. UNIX for Dummies Questions & Answers

Extracting rows from a text file based on the first column

I have a tab delimited text file where the first column can take on three different values : 100, 150, 250. I want to extract all the rows where the first column is 100 and put them into a separate text file and so on. This is what my text file looks like now: 100 rs3794811 0.01 0.3434... (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

9. Shell Programming and Scripting

Extracting lines in file based on time

Hi, anyone has any ideas on how do we extract lines from a file with format similiar to this: (based on current time) Jun 18 00:16:50 .......... ............. ............ Jun 18 00:17:59 .......... ............. ............ Jun 18 01:17:20 .......... ............. ............ Jun 18... (5 Replies)
Discussion started by: faelric
5 Replies

10. Shell Programming and Scripting

Extracting data from text file based on configuration set in config file

Hi , a:) i have configuration file with pattren <Range start no>,<Range end no>,<type of records to be extracted from the data file>,<name of the file to store output> eg: myfile.confg 9899000000,9899999999,DATA,b.dat 9899000000,9899999999,SMS,a.dat b:) Stucture of my data file is... (3 Replies)
Discussion started by: suparnbector
3 Replies
Login or Register to Ask a Question