LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-17-2007, 07:24 AM   #1
opensource82
LQ Newbie
 
Registered: Aug 2007
Posts: 16

Rep: Reputation: 0
Awk


Hi all,
I have a question about awk. I need to put a field in a file to another field in another line. for example let us say that file1 contain the following:

A B C

and file 2 :

E F G

I want to take field number 2 "$2" from file1 which is (B) to put it as field number 4 "$4" in file 2. How can i do that.

Regards,
 
Old 09-17-2007, 08:25 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982
If you want to do this from command line, without a more complex GAWK script, you can use a for loop to extract $2 from each line of file1 and append the value at the end of the corresponding line in file2, as in
Code:
count=0
for item in `gawk '{print $2}' file1`
> do
> count=$((count + 1))
> gawk -v var=$item -v num=$count '{if ( NR == num ) print $0, var}' file2
> done
However, the dis-advantage of this approach is the great number of calls to gawk, if file1 has a huge number of lines.
 
Old 09-17-2007, 09:12 AM   #3
opensource82
LQ Newbie
 
Registered: Aug 2007
Posts: 16

Original Poster
Rep: Reputation: 0
So is there is another way to do that without awk?
 
Old 09-17-2007, 09:58 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982
Actually I was thinking about an awk script to send output to an external file, anyway here is a one line command which do the same thing
Code:
gawk '{print $2}' file1 | paste -d \  file2 -
this uses the command paste which comes with coreutils. Please note the double space after -d \ . This tells to use a blank space (escaped) as delimiter, instead of the default tab. See man paste for details.
 
Old 09-18-2007, 06:23 AM   #5
opensource82
LQ Newbie
 
Registered: Aug 2007
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks for the new command but we still didn't detremine the field in the second file. any idea?
 
Old 09-19-2007, 09:08 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982
Do you mean to insert a column from file1 as any column in file2? Assuming that the two files have the same number of columns and there are no blank elements, you can try something like
Code:
{ getline line < ifile
  split(line,array)
  for (i = 1; i <= ocol - 1; i++)
      printf("%s ", $i)
  printf("%s ", array[icol])
  for (i = ocol; i <= NF; i++)
      printf("%s ", $i)
  printf("\n")
}
where icol is the column to read from file1 (that is the second field to extract, in your example), ocol is the column to insert in file2 and ifile is the input file (file1). If you put the above code in a file, e.g script.awk, you can launch it as
Code:
gawk -v icol=2 -v ocol=2 -v ifile=file1 -f script.awk file2
You can play with this code adding some controls. For details about the usage of the getline statement in gawk, the reference is the Gawk: Effective AWK Programming guide, section 3.8.4. Bye
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk aggressivebloodcell Linux - Newbie 3 07-02-2007 11:51 PM
using awk vkmgeek Linux - General 1 03-27-2007 09:24 AM
Using awk vkmgeek Linux - General 2 03-14-2007 07:07 AM
using awk meniscus Linux - Newbie 6 10-05-2006 12:39 PM
using awk wedgeworth Linux - Newbie 9 02-20-2004 07:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:24 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