Bash Regex解析blkid引号内的USB名称

时间:2018-05-15 22:08:57

标签: regex bash

我正试图从 const deleteNotification$ = (id, { ajax }) => ajax({ url: `api/delete/{id}` }); // showYesNo is an action to eventually show a dialog using this approach https://stackoverflow.com/a/35641680/235659 const showYesNo = payload => ({ type: SHOW_MODAL, modalType: MODAL_TYPE_YES_NO, modalProps: { ...payload }, }); const deleteNotificationEpic = (action$, store, dependencies) => { let uid = dependencies.uid; // dependencies.uid is added here to allow passing the uid during unit test. return merge( // Show confirmation dialog. action$.pipe( ofType(NOTIFICATION_DELETE_REQUEST), map(action => { uid = shortid.generate(); return showYesNo({ message: 'NOTIFICATION_DELETE_CONFIRMATION', payload: { notificationId: action.notificationId, uid, }, }) }), ), // Deletes the notification if the user clicks on Yes action$.pipe( ofType(MODAL_YES_CLICKED), filter(({ payload }) => payload.uid === uid), mergeMap(({ payload }) => deleteNotification$(payload.notificationId, dependencies).pipe( mergeMap(() => of(deleteNotificationSuccess())), catchError(error => of(deleteNotificationSuccess(error))), ), ), ), ); }; 命令中获取USB名称(MYUSB)。目前,我有以下输出。我想获取以blkid开头的USB名称。所以输出只是LABEL。我怎样才能做到这一点?

MYUSB

1 个答案:

答案 0 :(得分:1)

尝试blkid | grep -o -P '(?<=LABEL=")([^"]*)'

这应该将blkid的输出传递给grep,它只返回与正则表达式匹配的字符串部分。

应该注意的是,如果blkid的输出中出现多个标签,则会输出所有标签,每个标签都在一个新行上。因此,要仅获取最后一个标签,请使用:

blkid | grep -o -P '(?<=LABEL=")([^"]*)' | tail -1

正则表达式解释:

  • (?<=LABEL=") - 断言字符串中当前位置前面的内容为LABEL="
  • ([^"]*) - 匹配并捕获零个或多个不是"的字符(引号之间的部分)。