barcforex.txt
我有一个包含 3 行的测试文件:
"20200424090011.047Z","New","0232917586","USD",8393000000.0000,"EUR"
"20200424120025.308Z","New","0132910586","INR",8393000000.0000,"USD"
"20200425120025.027Z","New","0132910587","USD",110000000.0000,"INR"
我想提取第一个字段(时间戳)并截掉最后 5 个字符(从.
到Z
),以便输出如下所示:
"20200424090011"
"20200424120025"
"20200425120025"
我的这次尝试几乎成功了:
awk -F',' '{ print $1 }' barcforex.txt | sed 's/.[0-9]Z//g'
这给了我这个输出:
"20200424090011.0"
"20200424120025.3"
"20200425120025.0"
其他尝试,例如:
awk -F',' '{ print $1 }' barcforex.txt | sed 's/^.[0-9]+Z$//g'
和这个:
awk -F',' '{ print $1 }' barcforex.txt | sed 's/^.[0-9]{3}Z$//g'
给出了相同的输出:
"20200424090011.047Z"
"20200424120025.308Z"
"20200424120025.308Z"
如何正确删除时间戳中的最后 5 个字符?
答案1
几种方式;这是三个
awk '{print substr($1,2,14)}' barcforex.txt # Print 14 characters of string from position 2
awk -F'[".]' '{print $2}' barcforex.txt # Split line at double quotes and dots; print 2nd field
grep -Po '(?<=^").*?(?=\.)' barcforex.txt # RE to match between first " and first .
一般来说,如果你正在使用awk
你大概不需要sed
(或grep
)
答案2
$ awk -F'"' '{print int($2)}' file
20200424090011
20200424120025
20200425120025
$ sed 's/"\([^.]*\).*/\1/' file
20200424090011
20200424120025
20200425120025
答案3
通过以下方法完成
命令
awk -F "," '{print substr($1,1,15)substr($1,21,1)}' filename
sed 's/\..*/"/g' filename
python
#!/usr/bin/python
o=open('io.txt','r')
for i in o:
q=i.split(".")[0]
print('{0}"'.format(q))
输出
"20200424090011"
"20200424120025"
"20200425120025"