If you define a field as any number of non-space characters followed by any number of space characters, then you can remove the first N
like this:
$ sed -E 's/([^[:space:]]+[[:space:]]*){1}//' file
Nospaces
One space
Two spaces
Three spaces
Change {1}
to {N}
, where N
is the number of fields to remove. If you only want to remove 1 field from the start, then you can remove the {1}
entirely (as well as the parentheses which are used to create a group):
sed -E 's/[^[:space:]]+[[:space:]]*//' file
Some versions of sed (e.g. GNU sed) allow you to use the shorthand:
sed -E 's/(\S+\s*){1}//' file
If there may be some white space at the start of the line, you can add a \s*
(or [[:space:]]*
) to the start of the pattern, outside of the group:
sed -E 's/\s*(\S+\s*){1}//' file
The problem with using awk is that whenever you touch any of the fields on given record, the entire record is reformatted, causing each field to be separated by OFS
(the Output Field Separator), which is a single space by default. You could use awk with sub
if you wanted but since this is a simple substitution, sed is the right tool for the job.