Hi,
I'm using awk to print columns from a tab delimited text file:
awk '{print " "$2" "$3" $6"}' file
The problem I have is column 6 contains text with spaces etc which means awk only prints the first word.
How can I tell awk to print the whole column content as column 6?
Thanks,
keenboy100:
Hi,
I'm using awk to print columns from a tab delimited text file:
awk '{print " "$2" "$3" $6"}' file
The problem I have is column 6 contains text with spaces etc which means awk only prints the first word.
How can I tell awk to print the whole column content as column 6?
Thanks,
How are your columns separated?
Can you post a sample input?
Hi,
Sorry!
The following sample is similar:
Col1 Col2 Col3 Col4
09:12:37 host2 OFFLINE host2 is offline and has been for 7 minutes
09:23:15 host1 ONLINE host1 is online
Column 4 is the column with the spaces similar to column 6 in my original question.
Thanks,
keenboy100:
Hi,
Sorry!
The following sample is similar:
Col1 Col2 Col3 Col4
09:12:37 host2 OFFLINE host2 is offline and has been for 7 minutes
09:23:15 host1 ONLINE host1 is online
Column 4 is the column with the spaces similar to column 6 in my original question.
Thanks,
Hi Keenboy,
I assumed that col1, col2, col3 and col4 are separated by tabs and col4 has spaces.
The following command print one empty column before col1 and after col4
awk -F"\t" '{print " "$1" "$2" "$3" "$4" "}' file
Subbeh
March 20, 2014, 11:38am
5
use awk -F'\t' ...
to set the field separator to tabs
Sorry again!
It's a weird file. Column 1 and 2 are separated by a space, 2 and 3 seem to separated by a tab (or 4 spaces)....
It's very inconsistent.
Would it be best replacing all white spaces with a comma except the spaces in the column containing the text and then print with awk?
You can define your FS to be strictly composed of 2 or more space characters whick will let you yank off the 6th field intact...
awk -F" {2,}" '{print $2, $3, $6}' file
2 Likes
Thanks Shamrock, I'm getting closer!
My problem now is column 1 and 2 are separated by a space! If only I could use your solution and separate 1 and 2 as well and I'll be sorted.
I'll keep digging.
Thanks for all of your help
Note: not every awk understands the {,}
repetition operators, out of the box. mawk does not know them and gawk 3 needs an option to switch them on ( --re-interval
or --posix
). An alternative would be to alternatively use two spaces followed by a +
operator.
If we combine that with alternation ( |
), we get:
awk -F ' +|\t' '{print $2, $3, $4}' file
which, combined with the input from post #3 gives:
Col2 Col3 Col4
host2 OFFLINE host2 is offline and has been for 7 minutes
host1 ONLINE host1 is online
--edit--
If the separator between the first two fields is a single space you could add and exception like this:
awk -F ' +|\t' '{split($1,F,/[ ]/); print F[2], $2, $3}' file
Another option could be to get the columns based on spacing, rather than character delimited. For example, in your example data above, you could print the 1st & 4th column like such:
perl -lne '@m = /(.{14})(.{12})(.{14})(.*)/; print $m[0].$m[3]' file
keenboy100:
Thanks Shamrock, I'm getting closer!
My problem now is column 1 and 2 are separated by a space! If only I could use your solution and separate 1 and 2 as well and I'll be sorted.
I'll keep digging.
Thanks for all of your help
In that case you'd have split up $1 into its sub-fields...
awk -F" +" '{printf("%s %s %s\n", a[split($1, a, " ")], $2, $5)}' file