简体   繁体   中英

How to print two fields from a text file?

I have a log file in my machine having the below mentioned contents.

2020-06-18 00:01:31|INFO|0||pro_upl_postpaid_trap|ComUpload|CUploadProcessRemoteFile.cpp[95]|rename remote file success,source filename=/file_src/localusers/data/TRAP_CBS/recv/ovou20200617_999_400280.unl.temp,destination filename=/file_src/localusers/data/TRAP_CBS/recv/**ovou20200617_999_400280.unl**
2020-06-18 00:01:32|INFO|0||pro_upl_postpaid_trap|ComUpload|CUploadProcessRemoteFile.cpp[95]|rename remote file success,source filename=/file_src/localusers/data/TRAP_CBS/recv/ocom20200617_512_400106.unl.temp,destination filename=/file_src/localusers/data/TRAP_CBS/recv/ocom20200617_512_400106.unl
2020-06-18 00:01:32|INFO|0||pro_upl_postpaid_trap|ComUpload|CUploadProcessRemoteFile.cpp[95]|rename remote file success,source filename=/file_src/localusers/data/TRAP_CBS/recv/ocom20200617_515_400184.unl.temp,destination filename=/file_src/localusers/data/TRAP_CBS/recv/ocom20200617_515_400184.unl
2020-06-18 00:01:33|INFO|0||pro_upl_postpaid_trap|ComUpload|CUploadProcessRemoteFile.cpp[95]|rename remote file success,source filename=/file_src/localusers/data/TRAP_CBS/recv/odata20200617_517_400092.unl.temp,destination filename=/file_src/localusers/data/TRAP_CBS/recv/odata20200617_517_400092.unl

I wanted to print 3 fields out of this log file like the below:

2020-06-18 00:01:31|**ovou20200617_999_400280.unl**

I was able to print the last field but couldn't able to print both date/time (2020-06-18 00:01:31) and the file name (ovou20200617_999_400280.unl) together..!!!

cat pro_upl_*_trap_$YestodayDate*|grep 'CUploadProcessRemoteFile.cpp' |awk -F "/" '{print $19}'

Any hints/help would be highly appreciated.

Thanks.

Not sure why you're grep-ing for CUploadProcessRemoteFile.cpp when it's present in every line but assuming you do have a reason for it, you never need grep when you're using awk , nor do you need cat to open the input file for awk :

$ awk -F'[|/]' -v OFS='|' '/CUploadProcessRemoteFile\.cpp/{print $1, $NF}' file
2020-06-18 00:01:31|**ovou20200617_999_400280.unl**
2020-06-18 00:01:32|ocom20200617_512_400106.unl
2020-06-18 00:01:32|ocom20200617_515_400184.unl
2020-06-18 00:01:33|odata20200617_517_400092.unl

You can use awk with multiple delimiters:

awk -F'[|/]' '{print $1, $NF}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM