Unix/Linux Go Back    


Windows & DOS: Issues & Discussions All Windows and DOS questions should go here as well. Discuss UNIX to Windows (Desktop or Server) here!

gawk: Merge N files into one

Windows & DOS: Issues & Discussions


Tags
awk, join, merge

Closed    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 05-16-2012
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 14 October 2017, 3:52 AM EDT
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
Quote:
Originally Posted by sameeribraimo View Post
And can you explain what represent 1 in the code: {next}1'. I tried 2,3,etc numbers and it works.

Thanks for your time but I am a zero in gawk
An awk statement has the form:

condition {action}

Conditions in awk control the execution of actions and actions are executed when the condition is true (1, 2, 3 or any value other then 0 or "" ).
If the condition is true and there are no actions between braces, awk prints the current record by default.

Try this:

Code:
awk 'NR==1{
  print;nf=NF;next
}
FNR==1{next}
nf!=NF{
  system("rm -f outfile"); exit
}1' file1.txt file2.txt file3.txt > outfile

Sponsored Links
    #9  
Old Unix and Linux 05-16-2012
sameeribraimo sameeribraimo is offline
Registered User
 
Join Date: May 2012
Last Activity: 12 July 2012, 4:39 PM EDT
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks in advance.

If the files have the same number of columns it works fine.
But if any file does not have the same number of columns gives error

i have error:
'0 Is not recognized as an internal or external command, operable program or batch file.
Sponsored Links
    #10  
Old Unix and Linux 05-16-2012
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 14 October 2017, 3:52 AM EDT
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
I don't get any error.... try nawk instead of awk.
    #11  
Old Unix and Linux 05-16-2012
sameeribraimo sameeribraimo is offline
Registered User
 
Join Date: May 2012
Last Activity: 12 July 2012, 4:39 PM EDT
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
I'm working on windows so i change:


Code:
NR==1{
  print;nf=NF;next
}
FNR==1{next}
nf!=NF{
  system("del -f outfile"); exit
}1

and error is:

The process can not access the file because it is being used by othe
the process.

Last edited by Franklin52; 05-16-2012 at 03:34 PM.. Reason: Please use code tags for data and code samples, thank you
Sponsored Links
    #12  
Old Unix and Linux 05-16-2012
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 14 October 2017, 3:52 AM EDT
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
I don't use awk in windows... but you can use the code of post #4 to check the columns of the files and the code of post #2 to merge the file.
Sponsored Links
    #13  
Old Unix and Linux 05-16-2012
neutronscott's Unix or Linux Image
neutronscott neutronscott is offline Forum Advisor  
script kiddie
 
Join Date: Jun 2011
Last Activity: 21 September 2017, 10:02 PM EDT
Location: South Carolina, USA
Posts: 941
Thanks: 31
Thanked 303 Times in 281 Posts
I've used gawk on Windows before, but I don't have it available now to play with. You could try using redirection within the script so you may close the file before trying to delete it, and/or storing the output in memory before dumping it into an output file. This shows the latter:


Code:
[mute@geek ~/temp/sameeribraimo]$ cat merge.awk
BEGIN {
        outfile=ARGV[--ARGC]
        print "output: " outfile
}
NR==1{nf=NF;a[r++]=$0}
NF!=nf{exit 1}
FNR!=1{a[r++]=$0}
END {for (i=0;i<r;i++) print a[i] > outfile}


Code:
[mute@geek ~/temp/sameeribraimo]$ gawk -f merge.awk file?.txt output.txt
output: output.txt
[mute@geek ~/temp/sameeribraimo]$ cat output.txt
a b c
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10

Sponsored Links
    #14  
Old Unix and Linux 05-17-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 17 October 2017, 2:39 AM EDT
Location: Amsterdam
Posts: 11,564
Thanks: 508
Thanked 3,348 Times in 2,954 Posts
moving to Windows forum
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading in all files from parent directory (GAWK) gd9629 Shell Programming and Scripting 11 07-18-2011 02:25 PM
Gawk / Awk Merge Lines based on Key Jamesfirst Shell Programming and Scripting 9 10-28-2010 09:22 AM
Using GAWK to combine files paragkalra Shell Programming and Scripting 11 11-14-2009 09:08 AM
Merge files of differrent size with one field common in both files using awk shashi1982 Shell Programming and Scripting 2 03-03-2009 06:12 AM
gawk - reading two files & re arrange the columns ezhil01 Shell Programming and Scripting 2 06-20-2008 10:54 AM



All times are GMT -4. The time now is 11:04 AM.