塞德?從報表文字檔案中刪除所有格式,即換行符號和空格,同時屏蔽某些部分

塞德?從報表文字檔案中刪除所有格式,即換行符號和空格,同時屏蔽某些部分

我正在開發一個項目,其中我需要從文字檔案中刪除所有格式,包括空格和換行符,然後用管道替換任何冒號。我已經取得了一些進展,但我找不到一種方法來掩蓋需要忽略的部分。我是 sed 的新手,對於 Bash 腳本編寫也只是新手,事實上,我並不完全確定 sed 是適合這項工作的工具(也許是 vi?我通常使用 Nano)。我嘗試格式化的文件與此類似

== LUN mysql05-dbdat02 ==

  LUNName:                        mysql05-dbdat02
  CollectionStartTime:            2012-09-20T15:43:03-04:00
  CollectionEndTime:              2012-09-20T15:43:34-04:00
  Capacity
    CurrentCapacity:              512
  IOOperations
    Reads:                        100
    Writes:                       0
    ReadsPerSecond:               0.000000
    WritesPerSecond:              0.000000
    ReadMBPerSecond:              0.000
    WriteMBPerSecond:             0.000
    TotalMBPerSecond:             0.000
    NonOptimizedIOPerSecond:      0.000000
    CacheHitPercentage:           0.000
  PerformanceMetrics
    TotalIOsPerSecond:            0.000
    ReadIOsPerSecond:             0.000
    WriteIOsPerSecond:            0.000
    TotalMBPerSecond:             0.000
    ReadMBPerSecond:              0.000
    WriteMBPerSecond:             0.000
  Performance

== LUN mysql05-dbdat02 ==

  LUNName:                        mysql05-dbdat02
  CollectionStartTime:            2012-09-20T15:43:03-04:00
  CollectionEndTime:              2012-09-20T15:43:34-04:00
  Capacity
    CurrentCapacity:              512
  IOOperations
    Reads:                        100
    Writes:                       0
    ReadsPerSecond:               0.000000
    WritesPerSecond:              0.000000
    ReadMBPerSecond:              0.000
    WriteMBPerSecond:             0.000
    TotalMBPerSecond:             0.000
    NonOptimizedIOPerSecond:      0.000000
    CacheHitPercentage:           0.000
  PerformanceMetrics
    TotalIOsPerSecond:            0.000
    ReadIOsPerSecond:             0.000
    WriteIOsPerSecond:            0.000
    TotalMBPerSecond:             0.000
    ReadMBPerSecond:              0.000
    WriteMBPerSecond:             0.000
  Performance

輸出需要是這樣的,

cm-data-unity01|LUNNam=cm-data-unity01|CollectionStartTim=2012-09-20T15:43:03-04:00|CollectionEndTim=2012-09-20T15:43:34-04:00|Capacity|CurrentCapacit=2048|IOOperations|Read=10|Write=90|ReadsPerSecon=8.000000|WritesPerSecon=76.000000|ReadMBPerSecon=0.430|WriteMBPerSecon=0.542|TotalMBPerSecon=0.973|NonOptimizedIOPerSecon=85.000000|CacheHitPercentag=0.000|PerformanceMetrics|TotalIOsPerSecon=84.000|ReadIOsPerSecon=8.000|WriteIOsPerSecon=76.000|TotalMBPerSecon=0.973|ReadMBPerSecon=0.430|WriteMBPerSecon=0.542|Performance|

或者,全部在一條線上。

我寫了一個非常簡單的 Bash 腳本來格式化它,就像這樣

# Author Christopher George Bollinger
# Comments: This script will modify the snippet.txt file.  
# This script is meant to, first, take a specific bit of unformatted data and  remove all line breaks and non-printable characters.

# Following this, the script is to replace any appropriate colons (those being used as delimiters) and replace them with the equals (=) character.
#!/bin/bash

echo "This script will remove line breaks, remove non-printable characters, and will replace colons used as field delimiters with the equals '(=)' character."
cp snippet.txt snippetwork.txt

RmLB ()
{
tr -d '\n' < snippetwork.txt > snippetwork1.txt

}

RmNonPrint ()
{
tr -cd "[:print:]" < snippetwork1.txt > snippetwork2.txt

}

RplcW ()
{
sed 's/: /=/g' snippetwork2.txt > snippetwork3.txt

}

RmWtSpc ()
{
tr -s ' ' '|' < snippetwork3.txt > snippetgood.txt
sed 'd/(?:[a-z]=) /'
}

QuChek ()
{
cat snippetgood.txt
read -p "Is this satisfactory? (Y/n)" Choice
case $Choice in
    Y|y)
    mv snippetgood.txt snippet.txt
    rm -f snippetwork*
    rm -f snippetgood.txt
    ;;
    N|n)
    exit
    ;;
    *)
    echo "Invalid Input."
    ;;
esac
}

read -p "Would you like to begin? (Y/n)" YorN

case $YorN in
    Y|y)
    RmLB
    RmNonPrint
    RplcW
    RmWtSpc
    QuChek
    ;;
    N|n)
    exit
    ;;
    *)
    echo "Invalid Selection"
    ;;
esac

除了輸出之外哪些函數不太正確,它給出:

==|LUN|mysql05-dbdat02|==|LUNName=|mysql05-dbdat02|CollectionStartTime=|2012-09-20T15:43:03-04:00|CollectionEndTime=|2012-09-20T15:43:34-04:00|Capacity|CurrentCapacity=|512|IOOperations|Reads=|100|Writes=|0|ReadsPerSecond=|0.000000|WritesPerSecond=|0.000000|ReadMBPerSecond=|0.000|WriteMBPerSecond=|0.000|TotalMBPerSecond=|0.000|NonOptimizedIOPerSecond=|0.000000|CacheHitPercentage=|0.000|PerformanceMetrics|TotalIOsPerSecond=|0.000|ReadIOsPerSecond=|0.000|WriteIOsPerSecond=|0.000|TotalMBPerSecond=|0.000|ReadMBPerSecond=|0.000|WriteMBPerSecond=|0.000|Performance|==|LUN|mysql05-dbdat02|==|LUNName=|mysql05-dbdat02|CollectionStartTime=|2012-09-20T15:43:03-04:00|CollectionEndTime=|2012-09-20T15:43:34-04:00|Capacity|CurrentCapacity=|512|IOOperations|Reads=|100|Writes=|0|ReadsPerSecond=|0.000000|WritesPerSecond=|0.000000|ReadMBPerSecond=|0.000|WriteMBPerSecond=|0.000|TotalMBPerSecond=|0.000|NonOptimizedIOPerSecond=|0.000000|CacheHitPercentage=|0.000|PerformanceMetrics|TotalIOsPerSecond=|0.000|ReadIOsPerSecond=|0.000|WriteIOsPerSecond=|0.000|TotalMBPerSecond=|0.000|ReadMBPerSecond=|0.000|WriteMBPerSecond=|0.000|Performance|

問題是等號後面出現的管道。如果有人能給我正確的方向,甚至可以透過線上資源進行一些澄清,我將不勝感激。

有趣的是,最終的結果是,雖然直接的請求是像上面的例子一樣格式化,但將其輸入 Unix cli 繪圖工具(我猜是 gnuplot)。據我了解,gnuplot 要求格式設定為列。如前所述,這對我來說是一個新領域,我將非常感謝您提供的任何建議。

答案1

我不太確定你想做什麼。使用您的第一個輸入文件,我創建了以下輸出:

LUNName=mysql05-dbdat02|CollectionStartTime=2012-09-20T15:43:03-04:00|CollectionEndTime=2012-09-20T15:43:34-04:00|Capacity|CurrentCapacity=512|IOOperations|Reads=100|Writes=0|ReadsPerSecond=0.000000|WritesPerSecond=0.000000|ReadMBPerSecond=0.000|WriteMBPerSecond=0.000|TotalMBPerSecond=0.000|NonOptimizedIOPerSecond=0.000000|CacheHitPercentage=0.000|PerformanceMetrics|TotalIOsPerSecond=0.000|ReadIOsPerSecond=0.000|WriteIOsPerSecond=0.000|TotalMBPerSecond=0.000|ReadMBPerSecond=0.000|WriteMBPerSecond=0.000|Performance|

有了這個 Perl One 襯墊:

perl -pe 's/\n/|/;s/\s*//g; s/:/=/; END{print "\n"}' file

你也可以這樣做:

sed -r 's/\s*//g; s/:/=/;' file | tr '\n' '|'

答案2

 sed -e ':a;N;$!ba;s/\n/\|/g;s/:  */=/g;s/ *//g' '<yourinputfilehere>' > '<youroutputfilehere>'

解釋:第一部分::a;N;$!ba;s/\n/\|/g刪除所有換行符號並替換為 |關於語法的更好解釋在這裡:https://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n

第二部分,;s/: */==/g將所有冒號後面接一個或多個空格替換為 ==

第三部分;s/ *//g刪除所有單一或多個空格。

顯然你的輸入檔和輸出檔需要被替換。如果您想避免輸出檔案中的偵錯輸出,您可以 2> '/dev/null'在末尾添加

我不太明白你的計劃是什麼,但你應該能夠從這裡實施它。

相關內容