Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I write the follwoing awk script:

% echo /var/sysconfig/network/my_functions  alpha beta gama  | \
      awk -v word=alpha '$2  == word { print $0 }'

how to tell awk that I want to print all line except $1 (/var/sysconfig/network/my_functions PATH ) so I will get the following:

alpha beta gama

instead of

/var/sysconfig/network/my_functions alpha beta gama

remark: line content can be anything and not limit by strings/word quantity

share|improve this question
up vote 2 down vote accepted

I think in awk there's no way but to remove the first field manually. (There are other ways if you're willing to normalize the inter-field space.)

awk '$2 == word {match($0, "("FS")+"); print substr($0, RSTART+RLENGTH);}'
share|improve this answer

If you set $1 to "" you will leave the delimiting space. If you don't want to do that you have to iterate over the fields:

awk '{for (f=2; f<=NF; ++f) { if (f!=2) {printf("%s",OFS);} printf("%s",$f)}; printf "\n" }'

Edit: fixed per Gilles' comment.

Another way to do the same thing:

awk '{d = ""; for (f=2; f<=NF; ++f) {printf("%s%s", d, $f); d = OFS}; printf("\n") }'
share|improve this answer
    
Errrm, this prints the fields on separate lines (after s/$NF/NF/). You can use if (f!=2) {printf("%s",OFS);} printf("%s",$f); as the loop body. – Gilles Nov 21 '10 at 16:10
    
@Gilles: Oops, I got in too big a hurry. – Dennis Williamson Nov 21 '10 at 16:14
    
The second proposal needs to start by resetting d to ""; as written, it will only work as intended for the first line. – dubiousjim Apr 19 '12 at 11:07
    
@dubiousjim: Thanks, I've corrected my answer. – Dennis Williamson Apr 19 '12 at 11:16
 % echo /var/sysconfig/network/my_functions  alpha beta gama | \
      awk -v word=alpha | \
             '$2  == word { $1=""; print $0 }'
share|improve this answer
1  
With the caveat already observerd by Dennis that this leaves the whitespace before $2. – Gilles Nov 21 '10 at 16:12
    
(Some necromancy here, but this thread popped up among the active questions just now): The extra whitespace can be trimmed with e.g. cut as in awk -v word=alpha '$2==word {$1=""; print}' | cut -b2- (if the poster doesn't want to use cut directly as in deltaray's answer, but that lacks the selection on "$2" part (which could be fixed with grep instead of awk)). – Daniel Andersson Apr 19 '12 at 11:55

Somehow I think this would be so much easier and more intuitive to do with the cut command:

echo /var/sysconfig/network/my_functions  alpha beta gama | cut -d' ' -f 2-

The only problem is that cut doesn't support multiple different types of whitespace at once for delimiters. So if you have spaces or tabs, it won't work.

share|improve this answer
    
That won't work if the first field varies in length. – Dennis Williamson Nov 21 '10 at 15:44
2  
@Dennis: Yes, it will. It won't work if the first field delimiter varies in length (or uses a tab), as deltaray wrote. – Gilles Nov 21 '10 at 16:11
    
@Gilles: I guess I just never use cut except for fixed width data. – Dennis Williamson Nov 21 '10 at 16:16
    
@Dennis, you're probably thinking of the -c option for cut, which is to cut based on the characters. -f is for fields. I wish cut was a bit more robust like awk is with separator length, because its quicker to use for things like this. – deltaray Nov 21 '10 at 17:12

Everyone always makes it harder than it is

$ echo /var/sysconfig/network/my_functions alpha beta gama |
awk -v word=alpha '$2==word{print substr($0,index($0,$2))}'
alpha beta gama
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .