Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"]" as field separator in awk 3

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
0
0
PL

hello,
is there any mistake in separator list given as

Code:
"[\[|\/|\]]"

The first two separators [ and / seems to work but the last ] not.

Code:
$ grep saved out|tail -10
2015-03-10 11:12:12 (846 KB/s) - 'U862861.bff' saved [345088/345088]
2015-03-10 11:12:13 (39.9 MB/s) - 'U862862.bff' saved [6144/6144]
2015-03-10 11:12:13 (58.3 MB/s) - 'U862863.bff' saved [8192/8192]
2015-03-10 11:12:13 (49.2 MB/s) - 'U862864.bff' saved [7168/7168]
2015-03-10 11:12:13 (38.8 MB/s) - 'U862865.bff' saved [6144/6144]
2015-03-10 11:12:13 (41.7 MB/s) - 'U862866.bff' saved [5120/5120]
2015-03-10 11:12:13 (43.4 MB/s) - 'U862867.bff' saved [6144/6144]
2015-03-10 11:12:14 (42.7 MB/s) - 'U862868.bff' saved [7168/7168]
2015-03-10 11:12:14 (44.8 MB/s) - 'U862869.bff' saved [5120/5120]
2015-03-10 11:12:14 (28.2 MB/s) - 'U862870.bff' saved [13312/13312]
$ grep saved out|tail -10|awk -F"[\[]" '{print $NF}'
345088/345088]
6144/6144]
8192/8192]
7168/7168]
6144/6144]
5120/5120]
6144/6144]
7168/7168]
5120/5120]
13312/13312]
$ grep saved out|tail -10|awk -F"[\[|\/]" '{print $NF}'
345088]
6144]
8192]
7168]
6144]
5120]
6144]
7168]
5120]
13312]
$ grep saved out|tail -10|awk -F"[\[|\/|[highlight #AD7FA8]\][/highlight]]" '{print $NF}'
2015-03-10 11:12:12 (846 KB/s) - 'U862861.bff' saved [345088/345088]
2015-03-10 11:12:13 (39.9 MB/s) - 'U862862.bff' saved [6144/6144]
2015-03-10 11:12:13 (58.3 MB/s) - 'U862863.bff' saved [8192/8192]
2015-03-10 11:12:13 (49.2 MB/s) - 'U862864.bff' saved [7168/7168]
2015-03-10 11:12:13 (38.8 MB/s) - 'U862865.bff' saved [6144/6144]
2015-03-10 11:12:13 (41.7 MB/s) - 'U862866.bff' saved [5120/5120]
2015-03-10 11:12:13 (43.4 MB/s) - 'U862867.bff' saved [6144/6144]
2015-03-10 11:12:14 (42.7 MB/s) - 'U862868.bff' saved [7168/7168]
2015-03-10 11:12:14 (44.8 MB/s) - 'U862869.bff' saved [5120/5120]
2015-03-10 11:12:14 (28.2 MB/s) - 'U862870.bff' saved [13312/13312]
$ grep saved out|tail -10|sed s/$/\ extra/|awk -F"[\[|\/|[highlight #AD7FA8]\][/highlight]]" '{print $NF}'
2015-03-10 11:12:12 (846 KB/s) - 'U862861.bff' saved [345088/345088] extra
2015-03-10 11:12:13 (39.9 MB/s) - 'U862862.bff' saved [6144/6144] extra
2015-03-10 11:12:13 (58.3 MB/s) - 'U862863.bff' saved [8192/8192] extra
2015-03-10 11:12:13 (49.2 MB/s) - 'U862864.bff' saved [7168/7168] extra
2015-03-10 11:12:13 (38.8 MB/s) - 'U862865.bff' saved [6144/6144] extra
2015-03-10 11:12:13 (41.7 MB/s) - 'U862866.bff' saved [5120/5120] extra
2015-03-10 11:12:13 (43.4 MB/s) - 'U862867.bff' saved [6144/6144] extra
2015-03-10 11:12:14 (42.7 MB/s) - 'U862868.bff' saved [7168/7168] extra
2015-03-10 11:12:14 (44.8 MB/s) - 'U862869.bff' saved [5120/5120] extra
2015-03-10 11:12:14 (28.2 MB/s) - 'U862870.bff' saved [13312/13312] extra

$ grep saved out|tail -10|awk -F"[\[|\/]" '{print $(NF-1),$NF}'|awk -F"]" '{print $1}'
345088 345088
6144 6144
8192 8192
7168 7168
6144 6144
5120 5120
6144 6144
7168 7168
5120 5120
13312 13312
 
Hi

The backslash ( \ ) is meaningful for the shell too. And there you seem to mix character classes and alternation. These work with [tt]gawk[/tt] and [tt]mawk[/tt] running in [tt]bash[/tt] :
[ul]
[li][tt]-F "[\\\\[/\\\\]]"[/tt][/li]
[li][tt]-F "\\\\[|/|\\\\]"[/tt][/li]
[li][tt]-F '[\\[/\\]]'[/tt][/li]
[li][tt]-F '\\[|/|\\]'[/tt][/li]
[/ul]

Feherke.
feherke.ga
 
thank you, these two worked on AIX in ksh and awk:

Code:
# grep saved out|tail -10|awk -F "\\\\[|/|\\\\]" '{print $(NF-2),$(NF-1)}'
345088 345088
6144 6144
8192 8192
7168 7168
6144 6144
5120 5120
6144 6144
7168 7168
5120 5120
13312 13312
# grep saved out|tail -10|awk -F '\\[|/|\\]' '{print $(NF-2),$(NF-1)}'
345088 345088
6144 6144
8192 8192
7168 7168
6144 6144
5120 5120
6144 6144
7168 7168
5120 5120
13312 13312
 
Another trick, if the first character in a character class is ], it is treated literally. Likewise if the last character is [. So this should also work:

Code:
awk -F '[]/[]' '{print $(NF-2),$(NF-1)}'

And you aren't in \\\\\escape hell... :)

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top