我正在尝试编写一个sed命令来替换字符串,直到第一次出现分隔符。例如,我在文件中有以下行,其中'〜'是分隔符:
ab c1~10/20/2010 00:00:00 ~1234~10.02~530.55
ab c2~10/10/2010T00:00Z:~12346~11.03~531
abc3~10/10/2010 00:00:00 00-000~122~12~532.44
abc4~10/11/2010~110~13~533
我想将所有日期(第二列)替换为" 2010-10-10T00:00:00Z"这种格式。正如您所看到的,日期可以是不同的格式,内容是" MM / dd / yyyy"对我来说无关紧要,我想忽略它并用" T00:00:00Z"取而代之。我写了以下命令:
SEPAR="\([ \/._-]\)\{1\}";
sed -i "s/\(0[1-9]\|1[012]\)$SEPAR\([123][0]\|[012][1-9]\|3[1]\)$SEPAR\(\(10\|20\)[0-9][0-9]\).*~/\5\-\1\-\3T00:00:00Z~/g" $file_name;
但它会替换最后一列的所有内容,例如它会生成以下输出(请注意缺少两列):
ab c1~2010-10-20T00:00:00Z~530.55
ab c2~2010-10-10T00:00:00Z~531
abc3~2010-10-10T00:00:00Z~532.44
abc4~2010-10-11T00:00:00Z~533
我的预期输出是:
ab c1~2010-10-20T00:00:00Z~1234~10.02~530.55
ab c2~2010-10-10T00:00:00Z~12346~11.03~531
abc3~2010-10-10T00:00:00Z~122~12~532.44
abc4~2010-10-11T00:00:00Z~110~13~533
请帮我写下最后一部分"。*〜"这取代了一切。
答案 0 :(得分:1)
您可以使用#!/usr/bin/perl
use Expect;
use Switch;
use POSIX;
use XML::Simple;
use ATS::DB::connect;
use ATS::DB::build;
use ATS::Misc::helperFuncs;
use ATS::L2L3::Object;
use ATS::L2L3::Router;
use ATS::L2L3::NS;
use ATS::DB::testinstance;
use ATS::DB::L1;
use ATS::DB::resource;
use Term::ANSIColor ;
use Term::ANSIColor qw(:constants);
use Term::ExtendedColor qw(:all);
use Data::Dumper;
BEGIN{ push @INC,"//home/atsuser/Scripts/LSN/";
push @INC,"/export/home/userAutomation/LSN/Scripts/";}
use LSN;
use File::Basename;
BEGIN{ push @INC,"//home/atsuser/Scripts/Failover/";}
use FileSync;
##############
Script Flow:
1.Determine the devices which are reachable.
2.Load the build passed to script on all UP devices
3.Check Devices are UP after buildUpdate.
4.Load config on devices which are UP.
5.Exit IF any config failed Or devices are n't UP after buildUpdate.
6.Proceed to Ping test
Clt -> SNIP on R1
R1 -> SNIP on HA
Prim -> SNIP on R2
R2 -> SNIP on Server
7. EXIT IF ping test failed
8. Proceed to CURL test.
Clt -> service on Server
#START:parser inserted variables
:
awk