I think the following shell and awk script does what you want. It was written and tested using a Korn shell, but should work with any other POSIX conforming shell (such as bash). If you are using a Solaris/SunOS system, change the script to use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk:
If you save the above code in a file named tester, make it executable with:
and execute it with the desired keys (B and D) in your example:
or:
when File1 and File2 contain the data specified in your example; File3 will contain:
which matches the output you said you wanted except that there is no space at the end of the 2nd line of output here even though there was a trailing space at the end of the 2nd line of File3 in you example.
I hope the comments are sufficient to explain how it works. If it doesn't work or you still have questions, let us know what diagnostics are produced or what shell or awk commands you can't decipher.
This User Gave Thanks to Don Cragun For This Post:
Hi,
I'm having trouble pulling out columns where the headers match a file of key ID's I'm interested in and was looking for some help.
file1.txt
I
Name
34
56
84
350
790
1215
1919
7606
9420
file2.txt
I Name 1 1 2 2 3 3 ... 34 34... 56 56... 84 84... 350 350...
M 1 A A A A... (20 Replies)
Hi Gurus,
I want to extract certain columns from file 2 and combine with file 1.
I am using the following script to extract the columns.
$ awk 'FNR>1{print $2, $9, FILENAME}' *.lim > out1
However, this script does not print the titles of the columns 2 and 9.
Can somebody help me in... (1 Reply)
Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns.
I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Hello,
I have some tab delimited text files with a three header rows. The headers look like, (sorry the tabs look so messy).
index group Name input input input input input input input input input input input... (9 Replies)
Hi all,
I am kind of stuck with printing my desired output. Please help me if you know how it can work.
My input file(tab separated):
NW_0068.1 41,16 100,900
NW_0699.1 4,2,19 200,700,80
My Output file (desired):
NW_0068.1 41,16 100,900 100 - 141
NW_0068.1 41,16 100,900 ... (3 Replies)
Hi Friends,
I have files with columns like this. This sample input below is partial.
Please check below for main file link. Each file will have only two rows.
... (8 Replies)
I have this code below that only prints out certain columns from the first two rows (doesn't affect rows 3 and beyond). How can I do the same on a partial header pattern “G_TP” instead of having to know specific column numbers (e.g. 374-479)? I've tried many other commands within this pipe with no... (4 Replies)
Hello,
I have to fish out some specific columns from a file based on the header value. I have the list of columns I need in a different file. I thought I could read in the list of headers I need,
# file with header names of required columns in required order
headers_file=$2
# read contents... (11 Replies)
Hi,
I have data which has many columns but i need to extract only three column from below all records have one space in begining.
i need to extract only column as below ,any tric awk or sed trick?, please advise.
cloumn no : 3 : ORD NO
coulmn no : 6: P A R T N U M B E R
coulmn no : 9 :... (7 Replies)