英文:
Sed command for handling string replacements by matching the pattern
问题
我正在使用sed创建一个命令,它应该适用于这三种情况。
以下是已经产生了期望结果的命令。
基本上,它会重新打印给定的输入记录,同时将日期转换为DD-MMM-YYYY
格式,如上面的结果所示。
但是,输入记录的格式稍有变化,其中第二个日期为空(-----)。为了处理该记录格式,我稍微修改了命令,如下所示。
并且还有第三种情况,记录格式中的两个日期都为空,如下所示。
它应该只是按原样重新打印这第三种格式的记录。
我一直在尝试编写一个单行命令,可以处理上述所有3种记录格式,但一直没有成功。
有人能为此提供一些帮助吗?
英文:
I am create a command using sed, which should work for these three scenarios.
echo "a_0001_hst_script5v00.sh 06/09/2023 06:30:30 06/09/2023 06:32:45 SU 307404729/1 0" |sed -E "s#([^/]*)?([0-9]{2}/[0-9]{2}/[0-9]{4})#echo $(date -d '\' +'%d-%^h-%Y')#g;s/echo //2ge"
is producing the desired result as below.
a_0001_hst_script5v00.sh 09-JUN-2023 06:30:30 09-JUN-2023 06:32:45 SU 307404729/1 0
essentially its reprinting the given input record while converting the date to DD-MMM-YYYY
format as shown in the result above.
but there is slight variation of the input record where second date is blank (-----). so to process that record format, i had to modify the command slightly as shown below.
echo "a_0001_hst_script5v00.sh 06/09/2023 06:30:30 ----- RU 307404729/1 0" |sed -E "s#([^/]*)?([0-9]{2}/[0-9]{2}/[0-9]{4})#echo $(date -d '\' +'%d-%^h-%Y')#g;e"
and produced the desired result as below one.
a_0001_hst_script5v00.sh 09-JUN-2023 06:30:30 ----- RU 307404729/1 0
and there is a third scenario where the record format having the both dates go blank like shown below.
a_0001_hst_script5v00.sh ----- ----- OH 307404729/1 0
and it should just reprint this 3rf format record as it is.
i have been trying to write this command in single line which works for al these 3 scenarios of the record formats given above and struggling.
could some one throw some light on this?
答案1
得分: 3
你可以更改命令执行的位置,即在去除重复的 echo
之前的第一次操作,并更改去除额外的 echo
的条件。
英文:
You can change where the command is executed i.e the first pass before removing the duplicate echo
and change the condition for the removal of additional echo
$ echo "a_0001_hst_script5v00.sh 06/09/2023 06:30:30 06/09/2023 06:32:45 SU 307404729/1 0
a_0001_hst_script5v00.sh 06/09/2023 06:30:30 ----- RU 307404729/1 0
a_0001_hst_script5v00.sh ----- ----- OH 307404729/1 0" | \
sed -E "s#([^/]*)?([0-9]{2}/[0-9]{2}/[0-9]{4})#echo $(date -d '\' +'%d-%^h-%Y') #ge;s/echo //g"
a_0001_hst_script5v00.sh 09-JUN-2023 06:30:30 09-JUN-2023 06:32:45 SU 307404729/1 0
a_0001_hst_script5v00.sh 09-JUN-2023 06:30:30 ----- RU 307404729/1 0
a_0001_hst_script5v00.sh ----- ----- OH 307404729/1 0
答案2
得分: 2
以下是您要求的翻译代码部分:
awk '
BEGIN{
split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec",months,",")
}
{
line=$0
while(match(line,/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/)){
val=substr(line,RSTART,RLENGTH)
split(val,arr,"/")
val2=arr[2]"-"months[sprintf("%01d",arr[1])]"-"arr[3]
sub(val,val2)
line=substr(line,RSTART+RLENGTH)
}
}
1
' Input_file
请注意,代码中的双引号和单引号在翻译时保持不变。
英文:
With your shown samples please try following awk
code. Written and tested in GNU awk
should work in any version.
awk '
BEGIN{
split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec",months,",")
}
{
line=$0
while(match(line,/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/)){
val=substr(line,RSTART,RLENGTH)
split(val,arr,"/")
val2=arr[2]"-"months[sprintf("%01d",arr[1])]"-"arr[3]
sub(val,val2)
line=substr(line,RSTART+RLENGTH)
}
}
1
' Input_file
答案3
得分: 0
sed 's/ ../../.... / $(date -d & +"%d-%^h-%Y") /g;s/.*/echo "&"/e' 文件
用命令替代替换每个日期,最后评估回显的行。
另一种只使用sed的替代方法:
sed -E 's/$/\n01JAN02FEB03MAR04APR05MAY06JUN07JUL08AUG09SEP10OCT11NOV12DEC/
:a;s/ (..)/(..)/(....) (.\n.\1(...))/ \2-\5-\3 \4/;ta;P;d' 文件
英文:
This might work for you (GNU sed):
sed 's/ ..\/..\/.... / $(date -d & +"%d-%^h-%Y") /g;s/.*/echo "&"/e' file
Replace each date with a command substitution and finally evaluated the echoed line.
Alternative using only sed:
sed -E 's/$/\n01JAN02FEB03MAR04APR05MAY06JUN07JUL08AUG09SEP10OCT11NOV12DEC/
:a;s/ (..)\/(..)\/(....) (.*\n.*(...))/ -- /;ta;P;d' file
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论