Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
linux字符串操作_Linux - Fatal编程技术网

linux字符串操作

linux字符串操作,linux,Linux,我有一个文件,该文件包含以下日志,有时它会更改,但总有一部分是文件。我想将该文件名提取到.xls cat /pretupsvar/pretups_weblogs/DirectPayOutErrorLog.log | awk '{print $10}'| sed 's/]//g' 当文件名没有空格时,它会显示ok,如下面的日志所示 userList1305631236859_AS_ALL_DIST_ALL_ETOPUP_1305631236859.xls 但是,当文件名包含一些空间时,它会像

我有一个文件,该文件包含以下日志,有时它会更改,但总有一部分是文件。我想将该文件名提取到.xls

cat /pretupsvar/pretups_weblogs/DirectPayOutErrorLog.log | awk '{print $10}'| sed 's/]//g'
当文件名没有空格时,它会显示ok,如下面的日志所示

 userList1305631236859_AS_ALL_DIST_ALL_ETOPUP_1305631236859.xls
但是,当文件名包含一些空间时,它会像

userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week
但在PB的情况下,我需要正确格式的文件名

有什么解决办法吗

我希望在空格中输出::

userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week\ \one\ \payout.xls 
以便我可以将该文件移动到其他位置。如果它不包含空格,那么它只给文件名.xls

Logs are :::::::::::::::::::::::: INFO [main] (DirectPayOutErrorLog.java:113) - :: [MSISDN: 9854798391] [FILE NAME: userList1305631236859_AS_ALL_DIST_ALL_ETOPUP_1305631236859.xls] [MESSAGE: User not active] INFO [main] (DirectPayOutErrorLog.java:113) - :: [MSISDN: 9854803938] [FILE NAME: userList1305631236859_AS_ALL_DIST_ALL_ETOPUP_1305631236859.xls] [MESSAGE: No channel user exist by this mobile number] INFO [main] (DirectPayOutErrorLog.java:113) - :: [MSISDN: 9803493705] [FILE NAME: userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week one payout.xls] [MESSAGE: user does not belong to specified domain] INFO [main] (DirectPayOutErrorLog.java:113) - :: [MSISDN: 9803493706] [FILE NAME: userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week one payout.xls] [MESSAGE: user does not belong to specified domain] INFO [main] (DirectPayOutErrorLog.java:113) - :: [MSISDN: 9803493707] [FILE NAME: userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week one payout.xls] [MESSAGE: user does not belong to specified domain] INFO [main] (DirectPayOutErrorLog.java:113) - :: [MSISDN: 9803590276] [FILE NAME: userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week one payout.xls] [MESSAGE: user does not belong to specified domain] INFO [main] (DirectPayOutErrorLog.java:113) - :: [MSISDN: 9803590277] [FILE NAME: userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week one payout.xls] [MESSAGE: user does not belong to specified domain] INFO [main] (DirectPayOutErrorLog.java:113) - :: [MSISDN: 9803590802] [FILE NAME: userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week one payout.xls] [MESSAGE: user does not belong to specified domain] INFO [main] (DirectPayOutErrorLog.java:113) - :: [MSISDN: NA] [FILE NAME: userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week one payout.xls] [MESSAGE: SQL Exception:Invalid column index] INFO [main] (DirectPayOutErrorLog.java:113) - :: [MSISDN: 9803594351] [FILE NAME: userList1305647896930_PB_PBZONE_DLMP_RLMP_ETOPUP_Week one payout.xls] [MESSAGE: user does not belong to specified domain] 日志为: 信息[main](directPayoterRorLog.java:113)-:[MSISDN:9854798391][文件名:userList1305631236859_AS_ALL_DIST_ALL_ETOPUP_1305631236859.xls][消息:用户未激活] 信息[main](directPayoterRorLog.java:113)-:[MSISDN:9854803938][文件名:userList1305631236859_AS_ALL_DIST_ALL_ETOPUP_1305631236859.xls][消息:此手机号码不存在频道用户] 信息[main](directPayoterRorLog.java:113)-:[MSISDN:9803493705][文件名:userList1305647880920\u PB\u PBZONE\u DLMP\u RLMP\u ETOPUP\u Week one payout.xls][消息:用户不属于指定域] 信息[main](directPayoterRorLog.java:113)-:[MSISDN:9803493706][文件名:userList1305647880920\u PB\u PBZONE\u DLMP\u RLMP\u ETOPUP\u Week one payout.xls][消息:用户不属于指定域] 信息[main](directPayoterRorLog.java:113)-:[MSISDN:9803493707][文件名:userList1305647880920\u PB\u PBZONE\u DLMP\u RLMP\u ETOPUP\u Week one payout.xls][消息:用户不属于指定域] 信息[main](directPayoterRorLog.java:113)-:[MSISDN:9803590276][文件名:userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week one payout.xls][消息:用户不属于指定域] 信息[main](directpayoterrorlog.java:113)-:[MSISDN:9803590277][文件名:userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week one payout.xls][消息:用户不属于指定域] 信息[main](directPayoterRorLog.java:113)-:[MSISDN:9803590802][文件名:userList1305647880920\u PB\u PBZONE\u DLMP\u RLMP\u ETOPUP\u Week one payout.xls][消息:用户不属于指定域] INFO[main](directPayoterRorLog.java:113)-:[MSISDN:NA][文件名:userList1305647880920\u PB\u PBZONE\u DLMP\u RLMP\u ETOPUP\u Week-one-payout.xls][消息:SQL异常:无效列索引] 信息[main](directPayoterRorLog.java:113)-:[MSISDN:9803594351][文件名:userList1305647896930_PB_PBZONE_DLMP_RLMP_ETOPUP_Week one payout.xls][消息:用户不属于指定域] 请帮助

尝试以下操作:

sed -n -e 's/.*\[FILE NAME: \([^]]*\)].*/\1/p' /pretupsvar/pretups_weblogs/DirectPayOutErrorLog.log
只要在每次使用文件名时正确引用用于存储文件名的变量(假设您谈论的是shell脚本),就不必“手动”转义空格

如果要对每次事件的每个文件名执行操作,可以执行以下操作:

INPUT_FILE=/pretupsvar/pretups_weblogs/DirectPayOutErrorLog.log
sed -n -e 's/.*\[FILE NAME: \([^]]*\)].*/\1/p' $INPUT_FILE | while read file ; do
  mv "$file" $SOME_DESTINATION
done
如果只想对每个文件执行一次操作,可以轻松地将
sort-u
添加到此管道中:

sed -n -e 's/.*\[FILE NAME: \([^]]*\)].*/\1/p' $INPUT_FILE | sort -u | while read file ; do
  // do stuff with "$file"
done
另一种方法:

$ cut -s -f12,13,14 -d " " --output-delimiter='\ ' log | sed 's#\].*$##'

userList1305631236859_AS_ALL_DIST_ALL_ETOPUP_1305631236859.xls
userList1305631236859_AS_ALL_DIST_ALL_ETOPUP_1305631236859.xls
userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week\ one\ payout.xls
userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week\ one\ payout.xls
userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week\ one\ payout.xls
userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week\ one\ payout.xls
userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week\ one\ payout.xls
userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week\ one\ payout.xls
userList1305647880920_PB_PBZONE_DLMP_RLMP_ETOPUP_Week\ one\ payout.xls
userList1305647896930_PB_PBZONE_DLMP_RLMP_ETOPUP_Week\ one\ payout.xls

如果您的文件名不包含']',则只需要以下内容:

sed 's/.*FILE NAME: //;s/].*//;s/ /\\ /g' logfile

否则搜索
][信息:

感谢您的支持,但如果我要移动此文件,请看PB的情况。它占用空间的地方..用户列表1305631236859_AS_ALL_DIST_ALL_ETOPUP_1305631236859.xls用户列表1305647866843_PB_PBZONE_DLMP_RLMP_ETOPUP_第一周付款。xls用户列表1305647896930_PB_PBZONE_DLMP_RLMP_etopu第一周付款。我如何移动使用此文件时出错name@Sunil,如果您的意思是用
mv
移动文件,则只需在文件名周围加引号,它将被视为一个名称。@Sunil:我发布的代码也适用于包含空格的文件,试试看。