From: Rakesh Sharma on 22 Apr 2010 10:58 On Apr 22, 3:38 pm, ezhil <ezhi...(a)gmail.com> wrote: > Hi, > > I have a file with 12 fields and 3000 row. For each row, I would like > to print col 1 and then when it comes to col 7, it should print 2 > columns together followed by a field separation till the end. I have > tried something like: > > awk '{printf("%s\t", $1); for(i=7; i<NF; (i+2)) {printf("%s%s\t",$i, $ > (i+1) ) }; {printf("\n")} }' file1 > > This prints only the col 1 and col 7 & 8 for the first record (many > times and I have to use ctrl-c to interrupt). Could you please help me > to fix this? > > Thanks in advance. > > Kind regards, > Ezhil Assumptions for 'sed': i) only spaces no TABS as whitespace. ii) no leading & trailing spaces. iii) no multiple spaces sed -e ' h ;# store the line for reuse ;# extract Col1 & store it for later use s/ /\ /;s/\n.*//;x ;# strip Cols1-6 s/ /\ /6;s/.*\n// ;# label odd space occurence with newline & even with tab :loop s/ /\n/ s//\t/ tloop s/\n/ /g ;# then change all newlines back to space H;g;s/\n/\t/ ;# join col1 to cols7,8, onwards. ' yourfile perl -pale ' $_ = $F[0]; shift @F for 1..6; $_ = join "\t",$_,map{"$F[2*$_] $F[2*$_+1]"} 0..$#F/2; ' yourfile ### note: assuming an even number of columns. untested. --Rakesh
From: Janis Papanagnou on 23 Apr 2010 06:37 Rakesh Sharma schrieb: > On Apr 22, 3:38 pm, ezhil <ezhi...(a)gmail.com> wrote: >> Hi, >> >> I have a file with 12 fields and 3000 row. For each row, I would like >> to print col 1 and then when it comes to col 7, it should print 2 >> columns together followed by a field separation till the end. I have >> tried something like: >> >> awk '{printf("%s\t", $1); for(i=7; i<NF; (i+2)) {printf("%s%s\t",$i, $ >> (i+1) ) }; {printf("\n")} }' file1 >> >> This prints only the col 1 and col 7 & 8 for the first record (many >> times and I have to use ctrl-c to interrupt). Could you please help me >> to fix this? >> >> Thanks in advance. >> >> Kind regards, >> Ezhil > > > Assumptions for 'sed': > i) only spaces no TABS as whitespace. > ii) no leading & trailing spaces. > iii) no multiple spaces Why a "solution" with such strong restrictions if the OP's original awk approach (modulo the simple errors in his code) doesn't require any such "assumptions". > > sed -e ' > h ;# store the line for reuse > > ;# extract Col1 & store it for later use > s/ /\ > /;s/\n.*//;x > > ;# strip Cols1-6 > s/ /\ > /6;s/.*\n// > > ;# label odd space occurence with newline & even with tab > :loop > s/ /\n/ > s//\t/ > tloop > s/\n/ /g ;# then change all newlines back to space > > H;g;s/\n/\t/ ;# join col1 to cols7,8, onwards. > ' yourfile This is not yet cryptic enough; any chance to provide an Intercal solution? > > perl -pale ' > $_ = $F[0]; > shift @F for 1..6; > $_ = join "\t",$_,map{"$F[2*$_] $F[2*$_+1]"} 0..$#F/2; > ' yourfile > > ### note: assuming an even number of columns. > > untested. > > --Rakesh
|
Pages: 1 Prev: how to log comman to a file ? Next: Question using sed replace text file \t |