2017-05-12 29 views
-3

目前我想更換13日逗號之間的所有逗號左和12逗號從右側的Unix

1856292496,-1863203096,302,918468087151,808648712,405670043170066,919015026101,M,6,T,0,15,2c,Dear Customer, Your Request is under Process,03,11/05/2017 10:00:00,11/05/2017 10:00:00,11/,11/05/2017 10:00:00,0,03,,255,,333,ERecharge_RCOM,919015540301 

開始啓動要求

1856292496,-1863203096,302,918468087151,808648712,405670043170066,919015026101,M,6,T,0,15,2c,Dear Customer Your Request is under Process,03,11/05/2017 10:00:00,11/05/2017 10:00:00,11/,11/05/2017 10:00:00,0,03,,255,,333,ERecharge_RCOM,919015540301 

當前

1856292499,-1863203087,301,918081224379,808648711,405540046666191,919026240102,M,6,T,0,15,8d,Dear Business Partner,your current Core balance is Rs.29.8,GSM balance is Rs.12892.14,MRCOM balance is Rs.1 and MRTL balance is Rs.1.Reliance,03,11/05/2017 10:00:00,11/05/2017 10:00:00,11/,11/05/2017 10:00:00,0,01,,255,,333,BalQuery_RCOM,919835853611 

要求

1856292499,-1863203087,301,918081224379,808648711,405540046666191,919026240102,M,6,T,0,15,8d,Dear Business Partner your current Core balance is Rs.29.8 GSM balance is Rs.12892.14 MRCOM balance is Rs.1 and MRTL balance is Rs.1.Reliance,03,11/05/2017 10:00:00,11/05/2017 10:00:00,11/,11/05/2017 10:00:00,0,01,,255,,333,BalQuery_RCOM,919835853611 

我需要在Unix系統上用空格替換從右邊開始的第13個逗號和從右邊開始的第12個逗號之間的所有逗號。

+1

歡迎來到Stack Overflow。請儘快閱讀[關於]和[問]頁面。它有助於顯示[你有什麼嘗試](http://mattgemmell.com/2008/12/08/what-have-you-tried/)。這也有助於瞭解你會考慮哪些工具。在這種情況下,是Awk還是Perl或Python的選項?您已經正確顯示了輸入和所需的輸出數據,並有兩個精心挑選的示例。唯一可能的抱怨是,也許'左起第五個逗號和右側第七個逗號(或其他小於13和12的數字)可以更容易處理。但是你應該展示你的嘗試。 –

+0

其實我對unix有點新,想用awk嘗試一下,但想不到方法, – AMyth

+0

我所能想出來的就是這個[root @ PMS]#awk -F「,」'print $ 1 「 」$ 2「, 」$ 3「, 」$ 4「, 」$ 10「, 」$ 6「, 」$ 7「, 」$ 8「, 」$ 9「, 」$ 10「, 」$ 11「 $ 12 」$ 13「,」 $ 14}」 CDR_2017051100.txt |更 1856252627,-1863282815,917827516705,302,825819144,405050035380268,919015000105,M 1,T,0 $ 1218 * 302 * 7428196597 * 69 * 7515# 1856252627,-1863282814,302,917827516705,808648712,405150035380268,919015000105 ,M,6,T,0 $ 122c,尊敬的客戶 – AMyth

回答

0

這裏是一個適度簡潔,但主要是高深莫測(如果不是難以理解的)解決方案使用Perl。

#!/usr/bin/perl -anlF, 
use strict; 
use warnings; 

my $lhs = 13; 
my $rhs = 13; 

$, = ",";  # Perl is obscure on occasion! 
my($nflds) = scalar(@F); 
print @F[0 .. $lhs-1], "@F[$lhs .. $nflds-$rhs-1]", @F[$nflds-$rhs .. $nflds-1] 
    if ($nflds > $lhs + $rhs); 
  • 的家當行使用-l使Perl的自動處理換行。見perldoc perlrun
  • 它還使用-F,其中,在Perl 5.20.0和以後,被明確記錄在案,以自動將Perl的成-aawk)模式和-n(讀循環,但不打印)模式。使用,作爲分隔符將輸入行自動分割爲數組F。早期版本的Perl不會由於-F的存在而推斷出-a-n,所以代碼(現在)使用以及-F,。已經顯示更新後的代碼可以與Perl 5.10.0一起使用,並且每個主要版本高達5.18以及5.20和更高版本。
  • use strict;use warnings;行將Perl設置爲挑剔。你應該總是使用它們。
  • 這兩個任務設置了您在問題中指定的值,不同之處在於它似乎是從右側開始的第13個字段,而不是第12個字段。如果需要,它們很容易替換。
  • $, = ",";行設置輸出字段分隔符(OFS在Awk中,而$OFS在Perl中在use English qw(-no_match_vars);下)。見perldoc Englishperldoc perlvars
  • my($nflds) = scalar(@F);行決定字段的數量。
  • print行的條件是有足夠的字段。
  • 它使用Perl的array slices到:
    • 打印字段0..$lhs-1作爲單獨的逗號分隔的字段
    • 結合字段$lhs..$nflds-$rhs-1爲單個空格分隔的字段(藉助於切片周圍的字符串)
    • 打印字段$nflds-$rhs..$nflds-1作爲單獨的逗號分隔的字段

從t輸出帽子,給你的輸入數據是:

1856292496,-1863203096,302,918468087151,808648712,405670043170066,919015026101,M,6,T,0,15,2c,Dear Customer Your Request is under Process,03,11/05/2017 10:00:00,11/05/2017 10:00:00,11/,11/05/2017 10:00:00,0,03,,255,,333,ERecharge_RCOM,919015540301 
1856292499,-1863203087,301,918081224379,808648711,405540046666191,919026240102,M,6,T,0,15,8d,Dear Business Partner your current Core balance is Rs.29.8 GSM balance is Rs.12892.14 MRCOM balance is Rs.1 and MRTL balance is Rs.1.Reliance,03,11/05/2017 10:00:00,11/05/2017 10:00:00,11/,11/05/2017 10:00:00,0,01,,255,,333,BalQuery_RCOM,919835853611 

注意,在第一行中的一個字段的前導空格被保留。

我沒有立即想出。我生成了一個更詳細的解決方案,如下所示:第一個:

#!/usr/bin/env perl -l 
use strict; 
use warnings; 

my $lhs = 13; 
my $rhs = 13; 

while (<>) 
{ 
    chomp; 
    my(@fields) = split /,/; 
    my($nflds) = scalar(@fields); 
    my(@output) = @fields; 
    if ($nflds > $lhs + $rhs) 
    { 
     my(@combine) = @fields[$lhs .. $nflds-$rhs-1]; 
     my $composite = "@combine"; 
     @output = (@fields[0 .. $lhs-1], $composite, @fields[$nflds-$rhs .. $nflds-1]); 
    } 
    local $, = ","; 
    print @output; 
} 

這產生與其他腳本相同的輸出。我有腳本調用rs13.pl(詳細)和rs17.pl(緊湊型),並檢查了他們這樣的(data包含輸入數據的兩行):

diff <(perl rs13.pl data) <(perl rs17.pl data) 

沒有什麼區別。

有多種方法可以使緊湊型解決方案更緊湊,但我不確定他們有多大幫助。


這裏是使用splicejoin函數而不是陣列切片另一個版本。在某些方面,它比其他兩種更加整潔,但對於領域太少的線條卻沒有相同的保護。

#!/usr/bin/perl -anlF, 
use strict; 
use warnings; 

my $lhs = 13; 
my $rhs = 13; 

$, = ",";  # Perl is obscure on occasion! 
my($nflds) = scalar(@F); 
splice(@F, $lhs, $nflds - $lhs - $rhs, join(' ', @F[$lhs .. $nflds-$rhs-1])); 
print @F; 

它產生與其他兩個腳本相同的結果。


是的,你可以在Awk中編寫代碼;它不會像這樣緊湊。