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.
this one uses awk to print all except the last field
I want to extend the proposed answers to the situation where fields are delimited by possibly several whitespaces –the reason why the OP is not using
cut
I suppose.I know the OP asked about
awk
, but ased
approach would work here (example with printing columns from the 5th to the last):pure sed approach
Explanation:
s///
is used the standard way to perform substitution^\s*
matches any consecutive whitespace at the beginning of the line\S+\s+
means a column of data (non-whitespace chars followed by whitespace chars)(){4}
means the pattern is repeated 4 times.sed and cut
by just replacing consecutive whitespaces by a single tab;
tr and cut:
tr
can also be used to squeeze consecutive characters with the-s
option.This was irritating me so much, I sat down and wrote a
cut
-like field specification parser, tested with GNU Awk 3.1.7.First, create a new Awk library script called
pfcut
, with e.g.Then, paste in the script below, and save. After that, this is how the usage looks like:
To avoid typing all that, I guess the best one can do (see otherwise Automatically load a user function at startup with awk? - Unix & Linux Stack Exchange) is add an alias to
~/.bashrc
; e.g. with:... then you can just call:
Here is the source of the
pfcut
script:This
awk
function returns substring of$0
that includes fields frombegin
toend
:To get everything starting from field 3:
To get section of
$0
that covers fields 3 to 5:b, e, p, i
nonsense in function parameter list is just anawk
way of declaring local variables.My answer is based on the one of VeeArr, but I noticed it started with a white space before it would print the second column (and the rest). As I only have 1 reputation point, I can't comment on it, so here it goes as a new answer:
start with "out" as the second column and then add all the other columns (if they exist). This goes well as long as there is a second column.
If you want formatted text, chain your commands with echo and use $0 to print the last field.
Example:
Prints: