2016-04-15 41 views
0

我每天14個字符後重復的行鍵值如何在一個固定長度的文件重複的關鍵值每列

我的文件是這樣的:

KEYVALUE1 201604141111I201604141111C201604141111D201604141111E201604141111F 
KEYVALUE1 201604141111G 
KEYVALUE2 201604141111I201604141111C201604141111D201604141111E201604141111F 
KEYVALUE2 201604141111G201604141111F 
KEYVALUE3 201604141111I 
KEYVALUE4 201604141111G201604141111I 

我的輸出應該看起來像這樣,

KEYVALUE1 201604141111I 
KEYVALUE1 201604141111C 
KEYVALUE1 201604141111D 
KEYVALUE1 201604141111F 
KEYVALUE1 201604141111G 
KEYVALUE2 201604141111I 
KEYVALUE2 201604141111C 
KEYVALUE2 201604141111D 
KEYVALUE2 201604141111F 
KEYVALUE2 201604141111G 
KEYVALUE2 201604141111F 
KEYVALUE3 201604141111I 
KEYVALUE4 201604141111G 
KEYVALUE4 201604141111I 

請幫忙。

回答

1

sed的是個人行簡單的換人,這是所有。爲別的,你應該用awk的簡單,清晰,耐用性,便攜性,效率和軟件的大多數其他所需的特性:

$ awk '{ while ($2!="") { print $1, substr($2,1,13); $2=substr($2,14) } }' file 
KEYVALUE1 201604141111I 
KEYVALUE1 201604141111C 
KEYVALUE1 201604141111D 
KEYVALUE1 201604141111E 
KEYVALUE1 201604141111F 
KEYVALUE1 201604141111G 
KEYVALUE2 201604141111I 
KEYVALUE2 201604141111C 
KEYVALUE2 201604141111D 
KEYVALUE2 201604141111E 
KEYVALUE2 201604141111F 
KEYVALUE2 201604141111G 
KEYVALUE2 201604141111F 
KEYVALUE3 201604141111I 
KEYVALUE4 201604141111G 
KEYVALUE4 201604141111I 
+1

謝謝ED ..這工程就像一個寶石: )我只是對這些職位進行了小小的修改。它的14個字符而不是13個。發佈更改後,我的命令如下所示:awk'{while($ 2!=「」){print $ 1,substr($ 2,1,14); $ 2 = substr($ 2,15)}}'$ FILE_NAME> BREAK_FILE – Mahesh

0

你可以試試這個sed

sed -n '/^\([^ ]* \)\(.\{13\}\)/{s/^\([^ ]* \)\(.\{13\}\)/&\n\1/g;P;D;}' file 
+0

該作品罰款,但如果我有像KEYVALUE2 201604141111G201604141111F數據,然後我得到的行只有keyvalue和空白這是不打算。但仍然是一個很好的替代:)我也是一個SED的風扇:) – Mahesh

0

或者這一個:

sed '{ 
    :j 
    /^[^ ]* .\{13\}.\{13\}/ { 
    h 
    s/^\([^ ]*\) \(.\{13\}\)\(.*\)$/\1 \2/ 
    p 
    x 
    s/^\([^ ]*\) \(.\{13\}\)\(.*\)$/\1 \3/ 
    h 
    b j 
    } 
}' file 
1

如果你打開的Perl:

perl -lane 'print "$F[0] $_" for $F[1] =~/.{13}/g' file 
相關問題