Bash KSH脚本删除最后N个字符

时间:2018-12-07 12:52:54

标签: regex bash ksh

我正在尝试从字符串中删除最后15个字符,我必须使用“ ksh”运行该BASH脚本。它与“ bash”一起使用时效果很好,但与“ ksh”一起使用时效果不佳。这是我的代码,

{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"MultiLineString","coordinates":[[[7.0847794888,50.7242091272],[7.0859976701,50.7239505872],[7.0895873541,50.7229008357],[7.0901112195,50.7227917786],[7.0906205998,50.7227372113],[7.090896172,50.7227340802],[7.0913044701,50.7227582187],[7.0917071312,50.7228159564],[7.0922891821,50.7229315872],[7.0927992895,50.7230314285],[7.0932376709,50.7230737208],[7.09341669,50.7230667476],[7.0935968504,50.7230439486],[7.0939103789,50.7230019411],[7.0944357092,50.722919812],[7.0946504307,50.722884129]]]},"properties":{"strecke_id":3,"auswertezeit":"2018-11-20T02:25:00","geschwindigkeit":0,"verkehrsstatus":"aktuell nicht ermittelbar"}},{"type":"Feature","geometry":{"type":"MultiLineString","coordinates":[[[7.0946672837,50.7229570868],[7.0944178379,50.7229966536],[7.0941970784,50.7230271005],[7.0939378936,50.7230727802],[7.0936017728,50.7231367597],[7.0932798529,50.7231763907],[7.0930779816,50.7231703081],[7.09282323,50.7231460101],[7.092602118,50.7231125502],[7.0921737342,50.7230305595],[7.0918270299,50.7229575387],[7.091406783,50.7228782479],[7.091032975,50.7228358133],[7.0906586662,50.7228233843],[7.0902282365,50.722859406],[7.0899174596,50.7229079347],[7.0894046068,50.7230325811],[7.0878981056,50.7234669905],[7.0877036425,50.7235309987],[7.087312093,50.7236397914],[7.0870210939,50.7237343579],[7.0867202207,50.7238234119],[7.086342286,50.7239534871],[7.0858559955,50.72409637],[7.0854881812,50.7241892249],[7.0851333403,50.7242715336],[7.0848519142,50.7243477924]]]},"properties":{"strecke_id":4,"auswertezeit":"2018-11-20T02:25:00","geschwindigkeit":55,"verkehrsstatus":"normales Verkehrsaufkommen"}},......]}

我在做什么错了?

这是#!/bin/bash ggate_location="'$(ps -ef|grep mgr)'" for word in $ggate_location do [[ $word =~ mgr\.prm$ ]] && echo ${word::-15} done

的输出
$(ps -ef | grep mgr)

1 个答案:

答案 0 :(得分:1)

如果性能不是很关键,那么依赖外部可执行文件的以下管道应该可以正常工作:

ps -o cmd= | grep -Ewo '[^::space::]*mgr\.prm' | cut -c -15

ps -o cmd=仅要求ps显示命令行(不包含标题),grep将行过滤为包含以mgr.\prm结尾的单词的行,{{1 }}仅返回该单词的前15个字符。

请注意,cut grep ord-regexp标志不是POSIX定义的,除非您使用的是GNU -w,否则可能不起作用。在那种情况下,我建议您将grepgrep CRE regex风味标志一起使用,并在模式末尾添加一个单词-P边界,或向{模式的结尾。