Match part of string in file2 based on column in file1


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match part of string in file2 based on column in file1
# 1  
Old 05-28-2013
Match part of string in file2 based on column in file1

I have a file containing texts and indexes. I need the text between (and including ) INDEX and number "1" alone in line. I have managed this:
Code:
awk '/INDEX/,/1$/{if (!/1$/)print}' file1.txt

It works for all indexes.

And then I have second file with years and indexes per year, one per line
Code:
1990 INDEX1 ....
1990 INDEX2 .....

I need all indexes for a given year (and ONLY for a given year). I have managed this, too:

Code:
awk 'BEGIN {while (getline < "indexes.txt") {if ($1=="1990") print $2} }';

What I'd like to do is join the two- so that I would get all the texts from file1 that match indexes printed by second command.
any ideas?

Last edited by Franklin52; 05-28-2013 at 08:56 AM.. Reason: Please use code tags
# 2  
Old 05-28-2013
I guess your requirement is unclear for other members in this forum including me.

I would suggest you to post a sample input and desired output in code tags
# 3  
Old 05-29-2013
OK, thanks...
So, as I said, I have two input files.
The first ("yearly book") is structured like this:

Code:
1.01 text I would
like to extract
(including newlines etc.)
1
2
3
(....text I don't need...)
1.02 some more text I would like to extract
in more than one line, again and terminator
is "1" in line
1
2
...

I managed this using:
Code:
awk '/1\.01/,/1$/{if (!/1$/)print}' file1.txt

I have hundreds if those "indexes" (1.01, 1.02,...) and doing it manually would be nonsense.
I also have file with list of them in file with all indexes of all yeary books structured like this:

Code:
1990 1.01 some description I don't need
1990 1.02 more desc...
(...)
1991 1.0 some desc I don't need

I've managed to extract all indexes for given year:

Code:
awk 'BEGIN {while (getline < "indexes.txt") {if ($1=="1990") print $2} }';

What I would like to do is join the two commands (or write a combined one) so it would pass all "indexes" from the command below into the command aboce and I would get all texts from file1 I need...
# 4  
Old 05-29-2013
If I understand what you're trying to do, the following seems to work:
Code:
!/bin/ksh
year=${1:-1990}
awk -v y="$year" '
FNR == NR && $1 == y {
        # Save index...
        idx[$2]
        next
}
$1 in idx,$1 == 1 {
        if($1 != 1) print
}' indexes "yearly book"

If you're using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.

With the input files specified in the 1st message in this thread, the output produced is:
Code:
1.01 text I would
like to extract
(including newlines etc.)
1.02 some more text I would like to extract
in more than one line, again and terminator
is "1" in line

when the script is invoked with no operands and when it is invoked with the operand "1990".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Print sequences from file2 based on match to, AND in same order as, file1

I have a list of IDs in file1 and a list of sequences in file2. I can print sequences from file2, but I'm asking for help in printing the sequences in the same order as the IDs appear in file1. file1: EN_comp12952_c0_seq3:367-1668 ES_comp17168_c1_seq6:1-864 EN_comp13395_c3_seq14:231-1088... (5 Replies)
Discussion started by: pathunkathunk
5 Replies

3. Shell Programming and Scripting

Match single line in file1 to groups of lines in file2

I have two files. File 1 is a two-column index file, e.g. comp11084_c0_seq6:130-468(-) comp12746_c0_seq3:140-478(+) comp11084_c0_seq3:201-539(-) comp12746_c0_seq2:191-529(+) File 2 is a sequence file with headers named with the same terms that populate file 1. ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

4. Shell Programming and Scripting

Based on column in file1, find match in file2 and print matching lines

file1: file2: I need to find matches for any lines in file1 that appear in file2. Desired output is '>' plus the file1 term, followed by the line after the match in file2 (so the title is a little misleading): This is honestly beyond what I can do without spending the whole night on it, so I'm... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

5. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

6. Shell Programming and Scripting

Match one column of file1 with that of file2

Hi, I have file1 like this aaa ggg ddd vvv eeeand file2 aaa 2 aaa 443 xxx 76 aaa 34 ggg 33 wee 99 ggg 33 ddd 1 ddd 10 ddd 98 sds 23 (4 Replies)
Discussion started by: polsum
4 Replies

7. Shell Programming and Scripting

how to find string from file1 in file2

hi; i am looking for simple search script that find string from file1 in file 2 file 1 contain a loot of string like: 204080111111111 204080222222222 204080333333333 in each row and i would like to take the first row for example 204080111111111 from file1 and find it in file2 when it... (1 Reply)
Discussion started by: kpinto
1 Replies

8. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

9. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

10. Shell Programming and Scripting

match value from file1 in file2

Hi, i've two files (file1, file2) i want to take value (in column1) and search in file2 if the they match print the value from file2. this is what i have so far. awk 'FILENAME=="file1"{ arr=$1 } FILENAME=="file2" {print $0} ' file1 file2 (2 Replies)
Discussion started by: myguess21
2 Replies
Login or Register to Ask a Question