linux 字符串操作

标签 linux

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

cat /pretupsvar/pretups_weblogs/DirectPayOutErrorLog.log | awk '{print $10}'| sed 's/]//g'

当文件名不是空格时,它显示正常,如下面的日志所示

 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] 

请帮忙

最佳答案

试试这个:

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

关于linux 字符串操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6045378/

相关文章:

c - 如何使用gnu C/C++ tools for arm生成的静态库和Microsoft C/C++ tools for arm

linux - ksh 函数返回值在括号中

regex - 如何根据目录名称更改多个文件的内容?

c - C语言除法程序

linux - 将 $PATH 设置为 $HOME 的所有可能路径

php - 尝试将 PHP imap_open() 与 diff 一起使用。 linux 接口(interface)(eth0 :1, eth0 :2). 有什么想法吗?

java - windows 和 linux 读取 FileInputStream 有什么区别?

python - 树莓派相机写入权限

linux - Linux中的跨进程锁定

linux - 防止 "rm -rf"