-1

我有一个包含 2 月 1 个月数据的文件,我需要每天拆分Feb文件,即将文件拆分为Feb_1, Feb_2... Feb_29

这是我的逻辑:

Inputfile= $1
monthname = "Feb"
while getopts :
datefield="1"
outfile="Feb_1"

while read line    
do
s = `echo "$line" | awk '{print $2}'`
t = `echo "$line" | awk '{print $3}'`
if [ "$s" = "$monthname" ]
    if [ "$t" = "$datefield" ] 
         echo $line > "$outfile"
    else
         datefield = $t
         outfile =$monthaname"_"$t
         echo $line > "$outfile"
    fi
else
  echo $line > "$outfile"
fi 
done < "$inputfile"

s = echo "$line" | awk '{print $2}'

这并没有给我第二个词,因为我正在使用$2它要求第二个命令行参数。我试过把'之前放$2如下。

s = echo "$line" | awk '{print '$2}'

在这种情况下,它会抛出一个新错误,假设第一行是Wed Feb 1它的抛出错误,因为Wed Feb 1它不存在。

这是示例数据:

Wed Feb 1 00:10:00 cpu usage    
KLOGENT.exe 3068 SYSTEM 00 0:00:00 17345K 15467 BELOW NORMAL    
SGHT.exe 3868 SYSTEM 00 0:00:00 18845K 15499 BELOW NORMAL    
.......    
.......    
Wed Feb 1 00:15:00 cpu usage    
KLTREENT.exe 3068 SYSTEM 00 0:00:00 17345K 15767 BELOW NORMAL    
KJTRT.exe 3868 SYSTEM 00 0:00:00 18845K 13699 BELOW NORMAL    
..............   
...........    
Wed Feb 1 23:55:00 cpu usage    
HTR.exe 3068 SYSTEM 00 0:00:00 1785K 4532 BELOW NORMAL    
KLU.exe 3868 SYSTEM 00 0:00:00 15645K 678 BELOW NORMAL    
...............   
.................   
Thu Feb 2 00:10:00 cpu usage
JUYT.exe 3068 SYSTEM 00 0:00:00 143245K 157767 BELOW NORMAL   
GFD.exe 3868 SYSTEM 00 0:00:00 18845K 879 BELOW NORMAL    
.........................    
.......................    
Thu Feb 28 00:15:00 cpu usage    
FRE.exe 3068 SYSTEM 00 0:00:00 143245K 157767 BELOW NORMAL    
YUT.exe 3868 SYSTEM 00 0:00:00 18845K 879 BELOW NORMAL    
............................    
...................    
Thu Feb 28 23:55:00 cpu usage    
TRE.exe 3068 SYSTEM 00 0:00:00 143245K 157767 BELOW NORMAL    
KJH.exe 3868 SYSTEM 00 0:00:00 18845K 879 BELOW NORMAL
4

2 回答 2

2

盯着我的水晶球来确定输入数据,你想要:

awk '{print > $2 "_" $3 }' input-file

或者可能

awk '$2 == "Feb" {print > $2 "_" $3 }' input-file

如果知道输入的实际格式,提供解决方案会简单得多。

于 2013-02-27T19:23:21.550 回答
1
awk '/Feb/{file=$2"_"$3}{print $0>file".txt"}' your_log
于 2013-03-01T12:37:22.903 回答