Collect whitespace-separated fields from string list Allows quick awk-like usage of string lists. Example data (in var a, created by 'a = !ls -l'):: -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPytho
(self, *fields: List[str])
| 186 | return type(self)([el for el in self if not pred(match_target(el))]) # type: ignore [no-untyped-call] |
| 187 | |
| 188 | def fields(self, *fields: List[str]) -> List[List[str]]: |
| 189 | """Collect whitespace-separated fields from string list |
| 190 | |
| 191 | Allows quick awk-like usage of string lists. |
| 192 | |
| 193 | Example data (in var a, created by 'a = !ls -l'):: |
| 194 | |
| 195 | -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog |
| 196 | drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython |
| 197 | |
| 198 | * ``a.fields(0)`` is ``['-rwxrwxrwx', 'drwxrwxrwx+']`` |
| 199 | * ``a.fields(1,0)`` is ``['1 -rwxrwxrwx', '6 drwxrwxrwx+']`` |
| 200 | (note the joining by space). |
| 201 | * ``a.fields(-1)`` is ``['ChangeLog', 'IPython']`` |
| 202 | |
| 203 | IndexErrors are ignored. |
| 204 | |
| 205 | Without args, fields() just split()'s the strings. |
| 206 | """ |
| 207 | if len(fields) == 0: |
| 208 | return [el.split() for el in self] |
| 209 | |
| 210 | res = SList() |
| 211 | for el in [f.split() for f in self]: |
| 212 | lineparts = [] |
| 213 | |
| 214 | for fd in fields: |
| 215 | try: |
| 216 | lineparts.append(el[fd]) |
| 217 | except IndexError: |
| 218 | pass |
| 219 | if lineparts: |
| 220 | res.append(" ".join(lineparts)) |
| 221 | |
| 222 | return res |
| 223 | |
| 224 | def sort( # type:ignore[override] |
| 225 | self, |