Computing.Net > Forums > Unix > how to fetch the fileds from 2 text files?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

how to fetch the fileds from 2 text files?

Reply to Message Icon

Name: psiva_unix
Date: May 2, 2009 at 05:16:28 Pacific
OS: Windows XP
CPU/Ram: -
Product: Sun / --
Subcategory: General
Comment:

Hi All,

i have two text files (temp.txt and temp1.txt) like this
temp.txt has three columns
col11 col12 col13


temp1.txt has four columns
col21 col22 col23 col24 col25

i want to get the all columns from temp.txt file and fetch the col24 and cold25 from temp1.txt file

expected output is:
col11 col12 col13 col24 col25

How can i get the values using unix command?


Thanks in advance



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: May 2, 2009 at 13:54:16 Pacific
Reply:

There are probably any number of ways to do this. One way is two create a temp file with the required 2 columns then use the paste command to glue the files together:

awk ' { printf ("%s %s", $4, $5) } ' temp1.txt > ./mytemp
paste -d " " temp.txt ./mytemp


0

Response Number 2
Name: psiva_unix
Date: May 3, 2009 at 00:13:43 Pacific
Reply:

Thanks Nails

Let me check your command.

Thanks


0

Response Number 3
Name: ghostdog
Date: May 3, 2009 at 19:43:23 Pacific
Reply:

no need for temp file or paste.

awk 'FNR==NR{_[++d]=$0;next}{print _[FNR],$4,$5}' file1 file2


0

Response Number 4
Name: lankrypt0
Date: May 4, 2009 at 07:51:15 Pacific
Reply:

ghostdog: would you mind breaking down that awk for me? my knowledge of awk is pretty limited and your example is interesting and potentially very useful; i'd love to understand the working behind it.


0

Response Number 5
Name: ghostdog
Date: May 4, 2009 at 18:28:57 Pacific
Reply:

i would love to explain to you, but i am not good at explaining. pls look at the manual . it explains what is FNR and NR as well as the basics you will need.


0

Related Posts

See More



Response Number 6
Name: nails
Date: May 4, 2009 at 21:47:43 Pacific
Reply:

ghostdog that's a neat trick. I'll take a shot at explaining it:

awk 'FNR==NR{_[++d]=$0;next}{print _[FNR],$4,$5}' file1 file2

First, two definitions:
FNR is the input record number of the current input file
NR is the total number of input records read, so far,regardless of the number of input files.

The awk one-liner can be split into 2 pieces. The first piece builds the array _[] from the first file, file1, The array is built because FNR always equals NR while file1 is read. The next statement gets the next line of file1 preventing the script from continuing until the file1 is read.

Each line of file1 is set to an element of the array indexed by d. The index increases by 1 after the line, $0, is assigned to the array element.

The second part of the script involves printing out each element of the array and fields 4 and 5 of file2. When the second input file2, becomes the input file, the FNR variable resets to 0. Now, with FNR being the index into thte array, it is insync with each line read from file2.

I don't know if I did a good job of explaining so here is another variation that might be easier to understand:

# using nawk for Solaris
nawk ' BEGIN { d=0;
while(getline < "temp.txt" > 0)
   _[++d]=$0
}
{
print _[NR],$4,$5
} ' temp1.txt

In the above scirpt, file1 is read and the array is built in the BEGIN section. Then, the main section reads file2, and each record number of file2, NR, is used as the index into the array.
.



0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: how to fetch the fileds from 2 text files?

how to fetch the Nth line from file www.computing.net/answers/unix/how-to-fetch-the-nth-line-from-file/8345.html

how to store the output file in .xls format www.computing.net/answers/unix/how-to-store-the-output-file-in-xls-format/8402.html

how to manage files in diskette in unix www.computing.net/answers/unix/how-to-manage-files-in-diskette-in-unix/2062.html