As with many of Rabu's threads, the problem specification isn't really clear.
Scott is absolutely correct that a
for loop is not needed with the specified problem (and would be inefficient if the goal really is to always print the 1st and 3rd fields from every input line).
But, if the real goal is to print every field except the 2nd field, then a
for loop makes sense. But using
print instead of
printf (without a
\n in the format string) would create separate lines for each printed field instead the desired single line of output.
Scott,
The latest update to the POSIX standards says this about a single character ERE being used for FS:
Quote:
2. If
FS is a single character:
- If FS is <space>, skip leading and trailing <blank> and <newline> characters; fields shall be delimited by sets of one or more <blank> or <newline> characters.
- Otherwise, if FS is any other character c, fields shall be delimited by each single occurrence of c.
Note that this is why
FS="." and
FS="*"work without needing to use
FS="[.]" or
FS="\*" when the goal is use a period or asterisk as a field separator without having to worry about them being interpreted as a metacharacters. But, if you want to use period and asterisk as field separators you need something like
FS="[.]|[*]" instead of just
FS=".|*".
Rabu,
Since
{print $1,$3} does exactly what you have requested, please clearly explain why you don't want to use it (or clarify what you are trying to do so we understand why this code is not the best solution for your problem).