From: Janis Papanagnou on 23 Jul 2010 07:24 On 23/07/10 12:59, ezhil wrote: > On Jul 22, 12:29 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com> > wrote: >> ezhil schrieb: >> >>> Hi All, >> >>> I have a file with 1000 rows and 68 columns. The header for 68 columns >>> is in the 2nd file (2nd column in the file). There is no key field to >>> match 2 files. I know that the order of rows are matched in 2 files, >>> so, it is transposing 2nd column of 2nd file and paste it in the first >>> row. Shamefully, I did this with Excel and wondering how I could do >>> this using AWK or SHELL. >> >> Maybe something like... (untested) >> >> awk 'NR==FNR {printf("%s%s",$2,(NR<68?OFS:ORS));next} 1' file2 file1 >> >> Janis >> >> >> >>> Thanks in advance. >> >>> Kind regards, >>> Ezhil > > Thanks Janis. It prints the first line (taken from file 2) and then > prints the first rows of the file1 and after prints only one column of > file1. I think that's impossible. Since the '1' means: print the whole line unconditionally. So either your data or your description is inaccurate. Here's some test case with a smaller data set... $ cat file1 line 1 field3 and4 and5 and6 line 2 field3 and4 and5 and6 line 3 field3 and4 and5 and6 line 4 field3 and4 and5 and6 $ cat file2 dummy f2a dummy f2b dummy f2c dummy f2d dummy f2e dummy f2f $ awk 'NR==FNR {printf("%s%s",$2,(NR<6?OFS:ORS));next} 1' file2 file1 Output: f2a f2b f2c f2d f2e f2f line 1 field3 and4 and5 and6 line 2 field3 and4 and5 and6 line 3 field3 and4 and5 and6 line 4 field3 and4 and5 and6 Wasn't that what you wanted? Second column from file2 as header and all the data from file1. Anyway. Janis
From: Barry Margolin on 23 Jul 2010 09:54 In article <i2bu65$7i6$1(a)news.m-online.net>, Janis Papanagnou <janis_papanagnou(a)hotmail.com> wrote: > On 23/07/10 12:59, ezhil wrote: > > On Jul 22, 12:29 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com> > > wrote: > >> ezhil schrieb: > >> > >>> Hi All, > >> > >>> I have a file with 1000 rows and 68 columns. The header for 68 columns > >>> is in the 2nd file (2nd column in the file). There is no key field to > >>> match 2 files. I know that the order of rows are matched in 2 files, > >>> so, it is transposing 2nd column of 2nd file and paste it in the first > >>> row. Shamefully, I did this with Excel and wondering how I could do > >>> this using AWK or SHELL. > >> > >> Maybe something like... (untested) > >> > >> awk 'NR==FNR {printf("%s%s",$2,(NR<68?OFS:ORS));next} 1' file2 file1 > >> > >> Janis > >> > >> > >> > >>> Thanks in advance. > >> > >>> Kind regards, > >>> Ezhil > > > > Thanks Janis. It prints the first line (taken from file 2) and then > > prints the first rows of the file1 and after prints only one column of > > file1. > > I think that's impossible. Since the '1' means: print the whole line > unconditionally. So either your data or your description is inaccurate. > > Here's some test case with a smaller data set... > > $ cat file1 > line 1 field3 and4 and5 and6 > line 2 field3 and4 and5 and6 > line 3 field3 and4 and5 and6 > line 4 field3 and4 and5 and6 > > $ cat file2 > dummy f2a > dummy f2b > dummy f2c > dummy f2d > dummy f2e > dummy f2f > > $ awk 'NR==FNR {printf("%s%s",$2,(NR<6?OFS:ORS));next} 1' file2 file1 > > Output: > > f2a f2b f2c f2d f2e f2f > line 1 field3 and4 and5 and6 > line 2 field3 and4 and5 and6 > line 3 field3 and4 and5 and6 > line 4 field3 and4 and5 and6 > > Wasn't that what you wanted? Second column from file2 as header and all > the data from file1. Isn't this simpler: awk '{printf "%s ", $2}' file2 echo '' cat file1 -- Barry Margolin, barmar(a)alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group ***
From: Janis Papanagnou on 23 Jul 2010 10:04 On 23/07/10 15:54, Barry Margolin wrote: > In article <i2bu65$7i6$1(a)news.m-online.net>, > Janis Papanagnou <janis_papanagnou(a)hotmail.com> wrote: > >> On 23/07/10 12:59, ezhil wrote: >>> On Jul 22, 12:29 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com> >>> wrote: >>>> ezhil schrieb: >>>> >>>>> Hi All, >>>> >>>>> I have a file with 1000 rows and 68 columns. The header for 68 columns >>>>> is in the 2nd file (2nd column in the file). There is no key field to >>>>> match 2 files. I know that the order of rows are matched in 2 files, >>>>> so, it is transposing 2nd column of 2nd file and paste it in the first >>>>> row. Shamefully, I did this with Excel and wondering how I could do >>>>> this using AWK or SHELL. >>>> >>>> Maybe something like... (untested) >>>> >>>> awk 'NR==FNR {printf("%s%s",$2,(NR<68?OFS:ORS));next} 1' file2 file1 >>>> >>>> Janis >>>> >>>> >>>> >>>>> Thanks in advance. >>>> >>>>> Kind regards, >>>>> Ezhil >>> >>> Thanks Janis. It prints the first line (taken from file 2) and then >>> prints the first rows of the file1 and after prints only one column of >>> file1. >> >> I think that's impossible. Since the '1' means: print the whole line >> unconditionally. So either your data or your description is inaccurate. >> >> Here's some test case with a smaller data set... >> >> $ cat file1 >> line 1 field3 and4 and5 and6 >> line 2 field3 and4 and5 and6 >> line 3 field3 and4 and5 and6 >> line 4 field3 and4 and5 and6 >> >> $ cat file2 >> dummy f2a >> dummy f2b >> dummy f2c >> dummy f2d >> dummy f2e >> dummy f2f >> >> $ awk 'NR==FNR {printf("%s%s",$2,(NR<6?OFS:ORS));next} 1' file2 file1 >> >> Output: >> >> f2a f2b f2c f2d f2e f2f >> line 1 field3 and4 and5 and6 >> line 2 field3 and4 and5 and6 >> line 3 field3 and4 and5 and6 >> line 4 field3 and4 and5 and6 >> >> Wasn't that what you wanted? Second column from file2 as header and all >> the data from file1. > > Isn't this simpler: > > awk '{printf "%s ", $2}' file2 > echo '' > cat file1 > No, it's not simpler, it's different. The equivalent to your code based on my above code would be awk 'NR==FNR {printf("%s ",$2; next} 1' file2 file1 Janis
From: Janis Papanagnou on 23 Jul 2010 10:06 Sorry, typo. On 23/07/10 16:04, Janis Papanagnou wrote: > On 23/07/10 15:54, Barry Margolin wrote: >> >> Isn't this simpler: >> >> awk '{printf "%s ", $2}' file2 >> echo '' >> cat file1 >> > > No, it's not simpler, it's different. The equivalent to your code > based on my above code would be > > awk 'NR==FNR {printf("%s ",$2; next} 1' file2 file1 awk 'NR==FNR {printf("%s ",$2); next} 1' file2 file1 > > > Janis
From: Janis Papanagnou on 23 Jul 2010 10:21 On 23/07/10 15:54, Barry Margolin wrote: [...] > > Isn't this simpler: > > awk '{printf "%s ", $2}' file2 > echo '' > cat file1 > And here is something simpler (without the spurious blank)... echo $(cut -d\ -f2 file2) ; cat file1 (but I wouldn't necessarily recommend it). Janis
First
|
Prev
|
Pages: 1 2 Prev: Need some help Next: CPA AFFILIATE NETWORKS AWESOME EARNINGS FROM YOUR HOME |