簡體   English   中英

單行多行,並從某些行中刪除重復項

[英]mutiple lines in single line and remove duplicates from some lines

這是我所擁有的,這是整個序列:

aa="aa1"
etime="1436262792"
from="joe"
test="blah"
test="blah1"
test="blah2"
addr="aa2";
aa="aa1"
etime="1436262792"
from="bob"
test="blah3"
addr="aa2";
etc...

有時測試行被限制為一個,但是每個行都用“;”分隔。 當有多個“測試”時,我只需要保留最后一個。

我正在嘗試獲得以下結果:

aa="aa1" etime="1436262792" from="joe" test="blah2" addr="aa2";
aa="aa1" etime="1436262793" from="bob" test="blah3" addr="aa2";

這是我目前的發現;

cat file | xargs | tr ';' '\n;'

輸出看起來像這樣:

aa="aa1" etime="1436262793" from="joe" test="blah" test="blah2" test="blah3" addr="aa2"
aa="aa1" etime="1436262793" from="bob" test="blah3" test="blah3" addr="aa2"

感謝您的任何建議,

我會從出現的test開始跟蹤最后一行,並在一行沒有這樣開始時打印它:

$ awk '/^test/ {t=$0; next} {if (t) print t; print; t=""}' file
aa="aa1"
etime="1436262792"
from="joe"
test="blah2"
addr="aa2";
aa="aa1"
etime="1436262792"
from="bob"
test="blah3"
addr="aa2";

然后管道傳輸到xargs -n 5以在每行5個字段的塊上打印:

$ awk '/^test/ {t=$0; next} {if (t) print t; print; t=""}' file | xargs -n 5
aa=aa1 etime=1436262792 from=joe test=blah2 addr=aa2;
aa=aa1 etime=1436262792 from=bob test=blah3 addr=aa2

sed

sed '/test/h;//!{x;//p;x;h};${//p};//d' file

將測試行保存在緩沖區中,覆蓋前一行直到沒有一行,然后刪除該行。

假設已保存,則在下一個無測試行或文件末尾,它將打印最后一個測試。

我發現這是另一種...計數每行字的數量...

    while read line;
    do
    COUNT=$(echo "${line}" | wc -w)

    if [ "${COUNT}" == "7" ]
    then
    echo ${line} | awk '{print $1,$2,$3,$6,$7}' >> tmp8
    elif [ "${COUNT}" == "6" ]
    then
    echo ${line} | awk '{print $1,$2,$3,$5,$6}' >> tmp8
    else
    echo ${line} | awk '{print $1,$2,$3,$4,$5}' >> tmp8
    fi
    done < tmp7

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM