This line worked until I had whitespace in the second field.
svn status | grep '\!' | gawk '{print $2;}' > removedProjs
is there a way to have awk print everything in $2 or greater? ($3, $4.. until we don't have anymore columns?)
I suppose I should add that I'm doing this in a Windows environment with Cygwin.
I wasn't happy with any of the
awk
solutions presented here because I wanted to extract the first few columns and then print the rest, so I turned toperl
instead. The following code extracts the first two columns, and displays the rest as is:The advantage compared to the
perl
solution from Chris Koknat is that really only the first n elements are split off from the input string; the rest of the string isn't split at all and therefor stays completely intact. My example demonstrates this with a mix of spaces and tabs.To change the amount of columns that should be extracted, replace the
3
in the example with n+1.will print all but very first column:
will print all but two first columns:
If you don't want to reformat the part of the line that you don't chop off, the best solution I can think of is written in my answer in:
How to print all the columns after a particular number using awk?
It chops what is before the given field number N, and prints all the rest of the line, including field number N and maintaining the original spacing (it does not reformat). It doesn't mater if the string of the field appears also somewhere else in the line.
Define a function:
And use it like this:
Output maintains everything, including trailing spaces
In you particular case:
If your file/stream does not contain new-line characters in the middle of the lines (you could be using a different Record Separator), you can use:
The first case will fail only in files/streams that contain the rare hexadecimal char number 1
Awk examples looks complex here, here is simple Bash shell syntax:
Where
1
is your nth column counting from 0.Example
Given this content of file (
in.txt
):here is the output:
Because of a wrong most upvoted anwser with 340 votes, I just lost 5 minutes of my life! Did anybody try this answer out before upvoting this? Apparantly not. Completely useless.
I have a log where after $5 with an IP address can be more text or no text. I need everything from the IP address to the end of the line should there be anything after $5. In my case, this is actualy withn an awk program, not an awk oneliner so awk must solve the problem. When I try to remove the first 4 fields using the most upvoted but completely wrong answer:
it spits out wrong and useless response (I added [..] to demonstrate):
There are even some sugestions to combine substr with this wrong answer. Like that complication is an improvement.
Instead, if columns are fixed width until the cut point and awk is needed, the correct answer is:
which produces the desired output:
This would work if you are using Bash and you could use as many 'x ' as elements you wish to discard and it ignores multiple spaces if they are not escaped.