Hello all,
I hope someone can help me understand this strange behaviour of awk:
file input.txt:
A, B, C,
D, E, Y,
G, H, I,
Awk command I use:
cat input.txt | awk -F, '{if ($3 == "Y") {$1=$3}; {print $0}'
What I expect the output to be is:
A, B, C,
Y, E, Y,
G, H, I,
What the output actually is:
A, B, C,
Y E Y
G, H, I,
(note the field delimiter on the second line is switched back to the
default one, that is <space>, the comma is lost)
The only way I can get the desired output is:
cat input.txt | awk -F, '{(if $1 == "D") {$1=X};
{printf("%s,%s,%s,\n"),$1,$2,$3}'
I know there are probably many other ways to accomplish the same result,
but that's not the point: the point is: Why does print $0 remove the
commas? It look like, once it finds a line to process (test is true), it
processes it, then forgets that it was using a non-default delimiter and
when it recreates $0 it uses the default delimiter...?? :(
Any clarification would be greatly appreciated.
TIA
/andrea
|
|
0
|
|
|
|
Reply
|
anfi
|
1/16/2004 2:29:00 PM |
|
anfi <anfi2000@hotmail.com> wrote:
> Hello all,
> I hope someone can help me understand this strange behaviour of awk:
> file input.txt:
> A, B, C,
> D, E, Y,
> G, H, I,
> Awk command I use:
> cat input.txt | awk -F, '{if ($3 == "Y") {$1=$3}; {print $0}'
> What I expect the output to be is:
> A, B, C,
> Y, E, Y,
> G, H, I,
> What the output actually is:
> A, B, C,
> Y E Y
> G, H, I,
> (note the field delimiter on the second line is switched back to the
> default one, that is <space>, the comma is lost)
> The only way I can get the desired output is:
> cat input.txt | awk -F, '{(if $1 == "D") {$1=X};
> {printf("%s,%s,%s,\n"),$1,$2,$3}'
> I know there are probably many other ways to accomplish the same result,
> but that's not the point: the point is: Why does print $0 remove the
> commas? It look like, once it finds a line to process (test is true), it
> processes it, then forgets that it was using a non-default delimiter and
> when it recreates $0 it uses the default delimiter...?? :(
Because you didn't set OFS, try something in the lines of:
awk 'BEGIN{FS=OFS=","}{if ($3~"Y") $1=$3} {print}' input.txt
--
Michael Heiming
Remove +SIGNS and www. if you expect an answer, sorry for
inconvenience, but I get tons of SPAM
|
|
1
|
|
|
|
Reply
|
Michael
|
1/16/2004 2:45:45 PM
|
|
> Because you didn't set OFS, try something in the lines of:
>
> awk 'BEGIN{FS=OFS=","}{if ($3~"Y") $1=$3} {print}' input.txt
Thanks for the quick answer! That did the trick ;)
/andrea
|
|
0
|
|
|
|
Reply
|
anfi
|
1/16/2004 3:08:11 PM
|
|
|
2 Replies
857 Views
(page loaded in 0.064 seconds)
Similiar Articles: removing given field and field separator - comp.lang.awk ...print $0 removes the field delimiter - comp.lang.awk removing given field and field separator - comp.lang.awk ... Hi, What's the best way to remove a field ... comp.lang ... print all fields but $NF - comp.lang.awkprint $0 removes the field delimiter - comp.lang.awk print all fields but $NF - comp.lang.awk... awk '{ NF=NF-1 ; print $0 ... removed then remove it together with the ... select last field of line (cut/sed/awk etc) - comp.unix.shell ...Can you advise me of ways to select the last field of ... ps PID TTY TIME CMD 3282 pts/0 00:00 ... sed, remove last new line - comp.lang.awk select last field ... change field separator - comp.lang.awkprint $0 removes the field delimiter - comp.lang.awk change field separator - comp.lang.awk removing given field and field separator - comp.lang.awk ... change field ... using a word as delimiter in scan - comp.soft-sys.sasprint $0 removes the field delimiter - comp.lang.awk It look like, once it finds a line to process (test is true), it > processes it, then forgets that it was using a non ... print columns from bash variable - comp.lang.awkprint $0 removes the field delimiter - comp.lang.awk print columns from bash variable - comp.lang.awk... file like this: cat $FICHEIRO_TEMP | awk --field-separator ... Export to Excel without losing field names - comp.databases ...Export to Excel without losing field names - comp.databases ..... not perfect: DBF limits field names to 10 characters ... print $0 removes the field delimiter - comp ... sed: printing only between {} - comp.unix.shell... Or, with awk: awk '/{/,/}/ { print $0 }' ## '{ print $0 ... sed, remove last new line - comp.lang.awk I am ... but only on the first occurrence ... delimiters. say ... skip=0 ... Tab-delimited records in list-directed READ - comp.lang.fortran ...print $0 removes the field delimiter - comp.lang.awk Tab-delimited records in list-directed READ - comp.lang.fortran ... If enabled, WYLBUR will use them in output ... Query Show All records if parameter leaved blank - comp.databases ...print $0 removes the field delimiter - comp.lang.awk Hello all, I hope someone can help me understand this ... Answer: Excel Export, Query Defs, SQL IN criteria - comp ... sed deletestext between delimiter - Computer Tech Support Forum ...i have a file where i need to remove text between the delimiters. ... skip == 0 { print $0 } ' myfile ... sed join lines › awk remove last field leave delimiter field delimiter with a space or more - The UNIX and Linux ForumsIs there some way to specify a field delimiter for a space or ... Thanked 0 Times in 0 Posts ... ll | awk -F" " '{print $5}' will print a list at ... 7/23/2012 10:20:04 AM
|