塞德?从报告文本文件中删除所有格式,即换行符和空格,同时屏蔽某些部分

塞德?从报告文本文件中删除所有格式,即换行符和空格,同时屏蔽某些部分

我正在开发一个项目,其中我需要从文本文件中删除所有格式,包括空格和换行符,然后用管道替换任何冒号。我已经取得了一些进展,但我找不到一种方法来掩盖需要忽略的部分。我是 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'在末尾添加

我不太明白你的计划是什么,但你应该能够从这里实施它。

相关内容