[英]awk replace part of string in first field
Please, I have a file that contains fields separated by the character |
请给我一个文件,其中包含用字符
|
分隔的字段|
, and the first column contain a date and hour 2014-10-09/10:00:00.00
. ,并且第一列包含日期和小时
2014-10-09/10:00:00.00
。 here is the file: 这是文件:
2014-10-09/10:01:00.27| tha| 99| awfkj| kiuaj| oauhhg|
2014-10-09/10:02:49.00| okh| 69| azakj| wklkj| hjjhhg|
2014-10-09/10:15:30.06| hnt| 19| klkkj| kjhkj| okjhhg|
I want to change the format of date and replace the hour with "000000" it must be like this: 我想更改日期格式并将小时替换为“ 000000”,它必须是这样的:
09102014000000| tha| 99| awfkj| kiuaj| oauhhg|
09102014000000| okh| 69| azakj| wklkj| hjjhhg|
09102014000000| hnt| 19| klkkj| kjhkj| okjhhg|
I don't know what I need to use. 我不知道我需要用什么。 For example this?
例如这个?
awk '{ gsub("..", "..", $1) ; print }'
thank you 谢谢
You could simply do this through sed, 您可以通过sed轻松完成此操作,
GNU sed, GNU sed,
$ sed -r 's~^([0-9]{4})-([0-9]{2})-([0-9]{2})[^\|]*~\3\2\1000000~' file
09102014000000| tha| 99| awfkj| kiuaj| oauhhg|
09102014000000| okh| 69| azakj| wklkj| hjjhhg|
09102014000000| hnt| 19| klkkj| kjhkj| okjhhg|
Basic sed, 基本sed
$ sed 's~^\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)[^\|]*~\3\2\1000000~' file
09102014000000| tha| 99| awfkj| kiuaj| oauhhg|
09102014000000| okh| 69| azakj| wklkj| hjjhhg|
09102014000000| hnt| 19| klkkj| kjhkj| okjhhg|
sed 's/\([0-9]*\)-\([0-9]*\)-\([0-9]*\)[^|]*/\3\2\1000000/'
尝试这个:
sed 's/^\(....\)-\(..\)-\(..\)\/\(..:..:.....\)/\3\2\1000000/' input.txt
As this is about handling dates, I would use a little bash script and call the date
command. 由于这是关于日期的处理,因此我将使用一些bash脚本并调用
date
命令。 This way, you can tune it as you wish, just using the date
format options: 这样,您可以使用
date
格式选项随意调整它:
while IFS="|" read -r a b
do
a=$(date -d"${a%/*}" +"%d%m%Y")
echo "${a}000000| $b"
done < file
a=$(date -d"${a%/*}" +"%d%m%Y")
gets the first block of text, removes from the slash /
and converst it into a date on the format DDMMYYYY
. a=$(date -d"${a%/*}" +"%d%m%Y")
获取第一个文本块,从斜杠中删除/
并将其转换为DDMMYYYY
格式的DDMMYYYY
。
$ while IFS="|" read -r a b; do a=$(date -d"${a%/*}" +"%d%m%Y"); echo "${a}000000| $b"; done < file
09102014000000| tha| 99| awfkj| kiuaj| oauhhg|
09102014000000| okh| 69| azakj| wklkj| hjjhhg|
09102014000000| hnt| 19| klkkj| kjhkj| okjhhg|
$ sed -r 's/(^[0-9]{4})-([0-9]{2})-([0-9]{2}).{13}/\3\2\1000000|/g'
09102014000000| tha| 99| awfkj| kiuaj| oauhhg|
09102014000000| okh| 69| azakj| wklkj| hjjhhg|
09102014000000| hnt| 19| klkkj| kjhkj| okjhhg|
A gawk
specific way gawk
方式
awk '{a=gensub(/(.+)-(.+)-(.+)(\/.+)/,"\\3\\2\\1000000","g",$1);sub($1,a);print}' input.txt
Output: 输出:
09102014000000| tha| 99| awfkj| kiuaj| oauhhg|
09102014000000| okh| 69| azakj| wklkj| hjjhhg|
09102014000000| hnt| 19| klkkj| kjhkj| okjhhg|
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.