Options 1 to 3 have issues with multiple whitespace (but are simple).
That is the reason to develop options 4 and 5, which process multiple white spaces with no problem.
Of course, if options 4 or 5 are used with n=0 both will preserve any leading whitespace as n=0 means no splitting.
Option 1
A simple cut solution (works with single delimiters):
NOTE: The "^["FS"]*" is to accept an input with leading spaces.
Option 5
It is quite possible to build a solution that does not add extra leading or trailing whitespace, and preserve existing whitespace using the function gensub from GNU awk, as this:
Pretty much all the answers currently add either leading spaces, trailing spaces or some other separator issue. To select from the fourth field where the separator is whitespace and the output separator is a single space using awk would be:
AWK printf-based solution that avoids % problem, and is unique in that it returns nothing (no return character) if there are less than 4 columns to print:
The %-5s aligns the result as 5-character-wide columns; if this isn't enough, increase the number, or use %s (with a space) instead if you don't care about alignment.
Options 1 to 3 have issues with multiple whitespace (but are simple). That is the reason to develop options 4 and 5, which process multiple white spaces with no problem. Of course, if options 4 or 5 are used with
n=0
both will preserve any leading whitespace asn=0
means no splitting.Option 1
A simple cut solution (works with single delimiters):
Option 2
Forcing an awk re-calc sometimes solve the problem (works with some versions of awk) of added leading spaces:
Option 3
Printing each field formated with
printf
will give more control:However, all previous answers change all FS between fields to OFS. Let's build a couple of solutions to that.
Option 4
A loop with sub to remove fields and delimiters is more portable, and doesn't trigger a change of FS to OFS:
NOTE: The "^["FS"]*" is to accept an input with leading spaces.
Option 5
It is quite possible to build a solution that does not add extra leading or trailing whitespace, and preserve existing whitespace using the function
gensub
from GNU awk, as this:It also may be used to swap a field list given a count
n
:Of course, in such case, the OFS is used to separate both parts of the line, and the trailing white space of the fields is still printed.
Note1:
["FS"]*
is used to allow leading spaces in the input line.Pretty much all the answers currently add either leading spaces, trailing spaces or some other separator issue. To select from the fourth field where the separator is whitespace and the output separator is a single space using
awk
would be:To parametrize the starting field you could do:
And also the ending field:
AWK printf-based solution that avoids % problem, and is unique in that it returns nothing (no return character) if there are less than 4 columns to print:
Testing:
Input
Output
Use cut:
e.g.: If you have
file1
containing :car.is.nice.equal.bmw
Run :
cut -d . -f1,3 file1
will printcar.is.nice
This isn't very far from some of the previous answers, but does solve a couple of issues:
cols.sh
:Which you can now call with an argument that will be the starting column:
Or:
This is 1-indexed; if you prefer zero indexed, use
i=s + 1
instead.Moreover, if you would like to have to arguments for the starting index and end index, change the file to:
For example:
The
%-5s
aligns the result as 5-character-wide columns; if this isn't enough, increase the number, or use%s
(with a space) instead if you don't care about alignment.