Bash修剪或sed换行

时间:2015-10-13 17:09:02

标签: linux awk sed newline trim

拥有要删除的新行的日志文件(不是全部);我已经尝试了许多修饰方法和没有运气的sed(通常会结束一条大线)。

我认为我需要在修剪或整理时进行特定搜索,但不确定。

示例文件:

JOBV2|MISC: PURCHASE_DENIED|Purchasemode = sync ; iErrorCode = 992 ; terminalId = XXXXXXXXXXXX; offeringId = XXXXXX; rentalType = ps ; price = 1649 ; rentalWindowHrs = 0 ; IsAdult = False ; IsRedeemTitle = False ; TransactionID = IsUG=N;TId=X;VID=XXXX;Price=16.49;Pid=0;;CP=0 ; RedeemPoints =  ; PointsRatio =  ; TotalMRPoints =  ; NewTransactionID = IsUG=N;TId=XXXXXXXXXXXX;VID=XXXX;Pid=0;RetPrice=16.49;DisPrice=0.00;NetPrice=16.49;MRPoints=0;P2DVal=0;TotalPoints=0; ; |2015-10-07 00:00:14.320
JOBV2|MISC: Non-Purchase_Denied Error string: BILLING SYSTEM IS DOWN|^@|2015-10-07 00:02:13.457
JOBV3|MISC: PURCHASE FAILED: PURCHASE_DENIED   at STRING.JOBV2(PurchaseParams objPurchaseParams)
   at STRING.JOBV3(String strInputParams)|strInputParams = strUnitAddress=XXXXXXXXXXXX,terminalid=XXXXXXXXXXXX,offeringid=XXXXXX,rentalType=PS,price=1649,rentalWindowHrs=0,IsAdult=false,IsRedeemTitle=false,promotionType=,isUpgrade=N,catalogprice=0,strpromotionid=0; ; |2015-10-07 00:00:14.320
JOBV2|MISC: Non-Purchase_Denied Error string: BILLING SYSTEM IS DOWN|^@|2015-10-07 00:02:13.457
JOBV3|MISC: PURCHASE FAILED: PURCHASE_DENIED   at STRING.JOBV2(PurchaseParams objPurchaseParams)
   at STRING.JOBV3(String strInputParams)|strInputParams = strUnitAddress=XXXXXXXXXXXX,terminalid=XXXXXXXXXXXX,offeringid=XXXXXX,rentalType=PS,price=1649,rentalWindowHrs=0,IsAdult=false,IsRedeemTitle=false,promotionType=,isUpgrade=N,catalogprice=0,strpromotionid=0; ; |2015-10-07 00:00:14.320
JOBV2|MISC: PURCHASE_DENIED|Purchasemode = sync ; iErrorCode = 992 ; terminalId = XXXXXXXXXXXX ; offeringId = XXXXXX; rentalType = ps ; price = 1649 ; rentalWindowHrs = 0 ; IsAdult = False ; IsRedeemTitle = False ; TransactionID = IsUG=N;TId=XXXXXXXXXXXX;VID=XXXX;Price=16.49;Pid=0;;CP=0 ; RedeemPoints =  ; PointsRatio =  ; TotalMRPoints =  ; NewTransactionID = IsUG=N;TId=XXXXXXXXXXXX:VID=XXXX;Pid=0;RetPrice=16.49;DisPrice=0.00;NetPrice=16.49;MRPoints=0;P2DVal=0;TotalPoints=0; ; |2015-10-07 00:00:09.313
JOBV2|MISC: Non-Purchase_Denied Error string: BILLING SYSTEM IS DOWN|^@|2015-10-07 00:02:13.457

需要:

JOBV2|MISC: PURCHASE_DENIED|Purchasemode = sync ; iErrorCode = 992 ; terminalId = XXXXXXXXXXXX; offeringId = XXXXXX; rentalType = ps ; price = 1649 ; rentalWindowHrs = 0 ; IsAdult = False ; IsRedeemTitle = False ; TransactionID = IsUG=N;TId=X;VID=XXXX;Price=16.49;Pid=0;;CP=0 ; RedeemPoints =  ; PointsRatio =  ; TotalMRPoints =  ; NewTransactionID = IsUG=N;TId=XXXXXXXXXXXX;VID=XXXX;Pid=0;RetPrice=16.49;DisPrice=0.00;NetPrice=16.49;MRPoints=0;P2DVal=0;TotalPoints=0; ; |2015-10-07 00:00:14.320
JOBV2|MISC: Non-Purchase_Denied Error string: BILLING SYSTEM IS DOWN|^@|2015-10-07 00:02:13.457
JOBV3|MISC: PURCHASE FAILED: PURCHASE_DENIED   at STRING.JOBV2(PurchaseParams objPurchaseParams) at STRING.JOBV3(String strInputParams)|strInputParams = strUnitAddress=XXXXXXXXXXXX,terminalid=XXXXXXXXXXXX,offeringid=XXXXXX,rentalType=PS,price=1649,rentalWindowHrs=0,IsAdult=false,IsRedeemTitle=false,promotionType=,isUpgrade=N,catalogprice=0,strpromotionid=0; ; |2015-10-07 00:00:14.320
JOBV2|MISC: Non-Purchase_Denied Error string: BILLING SYSTEM IS DOWN|^@|2015-10-07 00:02:13.457
JOBV3|MISC: PURCHASE FAILED: PURCHASE_DENIED   at STRING.JOBV2(PurchaseParams objPurchaseParams) at STRING.JOBV3(String strInputParams)|strInputParams = strUnitAddress=XXXXXXXXXXXX,terminalid=XXXXXXXXXXXX,offeringid=XXXXXX,rentalType=PS,price=1649,rentalWindowHrs=0,IsAdult=false,IsRedeemTitle=false,promotionType=,isUpgrade=N,catalogprice=0,strpromotionid=0; ; |2015-10-07 00:00:14.320
JOBV2|MISC: PURCHASE_DENIED|Purchasemode = sync ; iErrorCode = 992 ; terminalId = XXXXXXXXXXXX ; offeringId = XXXXXX; rentalType = ps ; price = 1649 ; rentalWindowHrs = 0 ; IsAdult = False ; IsRedeemTitle = False ; TransactionID = IsUG=N;TId=XXXXXXXXXXXX;VID=XXXX;Price=16.49;Pid=0;;CP=0 ; RedeemPoints =  ; PointsRatio =  ; TotalMRPoints =  ; NewTransactionID = IsUG=N;TId=XXXXXXXXXXXX:VID=XXXX;Pid=0;RetPrice=16.49;DisPrice=0.00;NetPrice=16.49;MRPoints=0;P2DVal=0;TotalPoints=0; ; |2015-10-07 00:00:09.313
JOBV2|MISC: Non-Purchase_Denied Error string: BILLING SYSTEM IS DOWN|^@|2015-10-07 00:02:13.457

任何大大帮助的帮助!

3 个答案:

答案 0 :(得分:1)

您可以使用awk执行此操作:

awk 'BEGIN {ORS=""} /^ /{ print $0; next;} {print "\n"$0;}' input_file | sed 's/\r//g'

给出了

JOBV2|MISC: PURCHASE_DENIED|Puchasemode = sync ; iEoCode = 992 ; teminalId = XXXXXXXXXXXX; offeingId = XXXXXX; entalType = ps ; pice = 1649 ; entalWindowHs = 0 ; IsAdult = False ; IsRedeemTitle = False ; TansactionID = IsUG=N;TId=X;VID=XXXX;Pice=16.49;Pid=0;;CP=0 ; RedeemPoints =  ; PointsRatio =  ; TotalMRPoints =  ; NewTansactionID = IsUG=N;TId=XXXXXXXXXXXX;VID=XXXX;Pid=0;RetPice=16.49;DisPice=0.00;NetPice=16.49;MRPoints=0;P2DVal=0;TotalPoints=0; ; |2015-10-07 00:00:14.320
JOBV2|MISC: Non-Puchase_Denied Eo sting: BILLING SYSTEM IS DOWN|^@|2015-10-07 00:02:13.457
JOBV3|MISC: PURCHASE FAILED: PURCHASE_DENIED   at STRING.JOBV2(PuchasePaams objPuchasePaams)   at STRING.JOBV3(Sting stInputPaams)|stInputPaams = stUnitAddess=XXXXXXXXXXXX,teminalid=XXXXXXXXXXXX,offeingid=XXXXXX,entalType=PS,pice=1649,entalWindowHs=0,IsAdult=false,IsRedeemTitle=false,pomotionType=,isUpgade=N,catalogpice=0,stpomotionid=0; ; |2015-10-07 00:00:14.320
JOBV2|MISC: Non-Puchase_Denied Eo sting: BILLING SYSTEM IS DOWN|^@|2015-10-07 00:02:13.457
JOBV3|MISC: PURCHASE FAILED: PURCHASE_DENIED   at STRING.JOBV2(PuchasePaams objPuchasePaams)   at STRING.JOBV3(Sting stInputPaams)|stInputPaams = stUnitAddess=XXXXXXXXXXXX,teminalid=XXXXXXXXXXXX,offeingid=XXXXXX,entalType=PS,pice=1649,entalWindowHs=0,IsAdult=false,IsRedeemTitle=false,pomotionType=,isUpgade=N,catalogpice=0,stpomotionid=0; ; |2015-10-07 00:00:14.320
JOBV2|MISC: PURCHASE_DENIED|Puchasemode = sync ; iEoCode = 992 ; teminalId = XXXXXXXXXXXX ; offeingId = XXXXXX; entalType = ps ; pice = 1649 ; entalWindowHs = 0 ; IsAdult = False ; IsRedeemTitle = False ; TansactionID = IsUG=N;TId=XXXXXXXXXXXX;VID=XXXX;Pice=16.49;Pid=0;;CP=0 ; RedeemPoints =  ; PointsRatio =  ; TotalMRPoints =  ; NewTansactionID = IsUG=N;TId=XXXXXXXXXXXX:VID=XXXX;Pid=0;RetPice=16.49;DisPice=0.00;NetPice=16.49;MRPoints=0;P2DVal=0;TotalPoints=0; ; |2015-10-07 00:00:09.313
JOBV2|MISC: Non-Puchase_Denied Eo sting: BILLING SYSTEM IS DOWN|^@|2015-10-07 00:02:13.457

替代方法:

awk 'BEGIN {ORS=""} /\)$/{print $0; next;} {print $0"\n";}' input_file | sed 's/\r//g'

答案 1 :(得分:0)

另一种解决方案,您可以使用sed

sed -r ':a;N;$!ba;s/\n\s+/ /g' file

或者,不使用扩展正则表达式选项-r

sed ':a;N;$!ba;s/\n[ \t][ \t]*/ /g' file

你得到了

JOBV2|MISC: PURCHASE_DENIED|Purchasemode = sync ; iErrorCode = 992 ; terminalId = XXXXXXXXXXXX; offeringId = XXXXXX; rentalType = ps ; price = 1649 ; rentalWindowHrs = 0 ; IsAdult = False ; IsRedeemTitle = False ; TransactionID = IsUG=N;TId=X;VID=XXXX;Price=16.49;Pid=0;;CP=0 ; RedeemPoints =  ; PointsRatio =  ; TotalMRPoints =  ; NewTransactionID = IsUG=N;TId=XXXXXXXXXXXX;VID=XXXX;Pid=0;RetPrice=16.49;DisPrice=0.00;NetPrice=16.49;MRPoints=0;P2DVal=0;TotalPoints=0; ; |2015-10-07 00:00:14.320
JOBV2|MISC: Non-Purchase_Denied Error string: BILLING SYSTEM IS DOWN|^@|2015-10-07 00:02:13.457
JOBV3|MISC: PURCHASE FAILED: PURCHASE_DENIED   at STRING.JOBV2(PurchaseParams objPurchaseParams) at STRING.JOBV3(String strInputParams)|strInputParams = strUnitAddress=XXXXXXXXXXXX,terminalid=XXXXXXXXXXXX,offeringid=XXXXXX,rentalType=PS,price=1649,rentalWindowHrs=0,IsAdult=false,IsRedeemTitle=false,promotionType=,isUpgrade=N,catalogprice=0,strpromotionid=0; ; |2015-10-07 00:00:14.320
JOBV2|MISC: Non-Purchase_Denied Error string: BILLING SYSTEM IS DOWN|^@|2015-10-07 00:02:13.457
JOBV3|MISC: PURCHASE FAILED: PURCHASE_DENIED   at STRING.JOBV2(PurchaseParams objPurchaseParams) at STRING.JOBV3(String strInputParams)|strInputParams = strUnitAddress=XXXXXXXXXXXX,terminalid=XXXXXXXXXXXX,offeringid=XXXXXX,rentalType=PS,price=1649,rentalWindowHrs=0,IsAdult=false,IsRedeemTitle=false,promotionType=,isUpgrade=N,catalogprice=0,strpromotionid=0; ; |2015-10-07 00:00:14.320
JOBV2|MISC: PURCHASE_DENIED|Purchasemode = sync ; iErrorCode = 992 ; terminalId = XXXXXXXXXXXX ; offeringId = XXXXXX; rentalType = ps ; price = 1649 ; rentalWindowHrs = 0 ; IsAdult = False ; IsRedeemTitle = False ; TransactionID = IsUG=N;TId=XXXXXXXXXXXX;VID=XXXX;Price=16.49;Pid=0;;CP=0 ; RedeemPoints =  ; PointsRatio =  ; TotalMRPoints =  ; NewTransactionID = IsUG=N;TId=XXXXXXXXXXXX:VID=XXXX;Pid=0;RetPrice=16.49;DisPrice=0.00;NetPrice=16.49;MRPoints=0;P2DVal=0;TotalPoints=0; ; |2015-10-07 00:00:09.313
JOBV2|MISC: Non-Purchase_Denied Error string: BILLING SYSTEM IS DOWN|^@|2015-10-07 00:02:13.457

答案 2 :(得分:0)

如果您正在做的是将以空格开头的行连接到上一行,则可以在bash中执行此操作。

last=""
while IFS="" read line; do
  if [[ "$line" =~ ^[[:space:]] ]]; then
    echo -n "$last "
  else
    echo "$last"
  fi
  last="$line"
done < inp
echo "$line"

甚至在POSIX(非bash)shell中:

last=""
while IFS="" read line; do
  if expr "$line" : '[[:space:]]' >/dev/null; then
    echo -n "$last "
  else
    echo "$last"
  fi
  last="$line"
done < inp
echo "$line"