Home Man
Search
Today's Posts
Register

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

gawk: Merge N files into one

Tags
awk, join, merge

Login to Reply

 
Thread Tools Search this Thread
# 8  
Old 05-16-2012
Quote:
Originally Posted by sameeribraimo
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

# 9  
Old 05-16-2012
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.
# 10  
Old 05-16-2012
I don't get any error.... try nawk instead of awk.
# 11  
Old 05-16-2012
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
# 12  
Old 05-16-2012
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.
# 13  
Old 05-16-2012
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

# 14  
Old 05-17-2012
moving to Windows forum
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Merge files and generate a resume in two files jiam912 Shell Programming and Scripting 14 12-04-2013 05:03 PM
Checking in a directory how many files are present and basing on that merge all the files srikanth_sagi Shell Programming and Scripting 10 01-02-2013 03:39 AM
merge two files with awk xshang Shell Programming and Scripting 7 10-01-2012 12:54 AM
Merge different xml files in one corfuitl Shell Programming and Scripting 0 04-09-2012 11:41 AM
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
How to merge files lestat_ecuador Shell Programming and Scripting 3 06-07-2007 06:45 AM


All times are GMT -4. The time now is 06:58 PM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password