Jack Anderson
Jack Anderson

Reputation: 13

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.

Upvotes: 1

Views: 91

Answers (2)

user890739
user890739

Reputation: 733

You can use awk with multiple delimiters:

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

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203229

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

Upvotes: 1

Related Questions