LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-07-2005, 09:49 AM   #1
denalitastic
LQ Newbie
 
Registered: May 2005
Location: cleveland
Posts: 7

Rep: Reputation: 0
awk question


Hello I am trying to use awk to do this.

I have two files lets say file1.txt and file2.txt. file1.txt has 1 column. And file2.txt has 3 columns. I want to insert the column from file1.txt into file2.txt as the new column 3 or for that matter any column. But lets stick to the first case, So then I would have a 4 column file2.txt that would look like

file2colum1 file2colum1 file1colum1 file2colum3.

How do I do this, is there a command for that? I dont want any matching etc like the join command uses. I just want to insert the column.

-denalitastic
 
Old 06-07-2005, 10:42 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Using awk:

Code:
#!/bin/bash

awk  '
  BEGIN {
          # load array with contens f1.txt
          while ( getline < "f1.txt" > 0 )
            {
              f1_counter++
              f1[f1_counter] = $1
            }
  }
  { 
    print $1, $2, f1[NR], $3
   } ' f2.txt
In the BEGIN part all content of f1.txt is loaded into an array. The correct array entry can be printed by using the NR variable that holds the current record value. So print $1, $2, f1[NR], $3 first prints fields 1 and 2 from f2.txt, then f1[NR], which is the corresponding entry from f1.txt. Last the third field from f2.txt is printed.


cat f1.txt
F1column1
F1column1
F1column1
F1column1
F1column1

cat f2.txt
f2column1 f2column2 f2column3
f2column1 f2column2 f2column3
f2column1 f2column2 f2column3
f2column1 f2column2 f2column3
f2column1 f2column2 f2column3

Output above script:

f2column1 f2column2 F1column1 f2column3
f2column1 f2column2 F1column1 f2column3
f2column1 f2column2 F1column1 f2column3
f2column1 f2column2 F1column1 f2column3
f2column1 f2column2 F1column1 f2column3

Hope this helps.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk question puishor Programming 2 08-26-2005 09:44 AM
AWK question paraiso Linux - Newbie 5 05-12-2005 01:37 PM
awk question jonlake Linux - Software 2 05-04-2005 03:58 PM
awk question dilberim82 Programming 2 10-01-2003 07:12 PM
awk question (mandrake 8.2) Frauke Linux - Newbie 0 09-19-2002 04:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration