I have an issue with using AWK to simply remove a field from a stream, illustrated below:
1 int blah (void)
2 {
3 if (foo) {
4 printf ("blah\n");
5 }
6 return 0;
7 }
I use the following code to remove the first field:
$ awk '{ $1=""; print }' example.out
int blah (void)
{
if (foo) {
printf ("blah\n");
}
return 0;
}
Why is this the case? Is this because AWK removes all whitespace - can this be prevented?
http://awk.freeshell.org/RangeOfFields
Contains a description how to do it. It also links to http://student.northpark.edu/pemente/awk/awktail.txt which contains 3 solutions to the problem. As far as i know, if you assign to a field, then the output field separator is used to concatenate all fields together. So " "+
suddendly is collapsed to one space. Take it with a grain of salt though, i'm no awk expert. For example, try assigning :
to the variable OFS
, and colons instead of spaces will result in between fields in the output:
echo a b c | awk 'BEGIN{ OFS = ":" } { $1=""; print }'
$ :b:c
If you use gawk, then you can use its gensub
extension which i find pretty straight forward to use:
print gensub($1 "[\t ]*(.*)", "\\1", 1);