English 中文(简体)
如果两条或两条以上线从同一字开始,如何在档案中找到独一无二的浏览器
原标题:How to get unique rows in a file if 2 or more lines start with same word [closed]

inputfile.log

HSDA_17_VerifyShippingOrderNoInOrderConfirmationTest): Timed out after 90 seconds
HSDA_17_VerifyShippingOrderNoInOrderConfirmationTest): Session not available
HSDA_1_BuyVoice): Session not available 
HSDA_1_BuyVoice): Unable to locate element:
HSDA_9_CreateAccounAndBuyDeviceInCheckoutTest): Timed out after 90 seconds
HSDA_2_BuyMapswithNewUserAtCheckOutTest): Address already in use: connect
HSDA_69_ChangeBillingAddressInNonUSLocaleForAllFieldsTest): Index: 0, Size: 0

output expected

HSDA_17_VerifyShippingOrderNoInOrderConfirmationTest): Timed out after 90 seconds
HSDA_1_BuyVoice): Session not available 
HSDA_9_CreateAccounAndBuyDeviceInCheckoutTest): Timed out after 90 seconds
HSDA_2_BuyMapswithNewUserAtCheckOutTest): Address already in use: connect
HSDA_69_ChangeBillingAddressInNonUSLocaleForAllFieldsTest): Index: 0, Size: 0
问题回答

使用<代码>wk,我将:

  1. Set field separator to :
  2. For each row where the saved value of the first field is not equal to the first field:
    • print that entire row
    • save the value of the first field

换言之:

awk -F :    $1 != first { print; first = $1 } inputfile.log

http://tinyurl.com/SedAwkBook” rel=“nofollow”

我对@Johnsyweb的答复做了“翻译”:

awk -F :   ($1 in a){next;} !a[$1]++;   input

oh, forgot the test:

kent$  echo "HSDA_17_VerifyShippingOrderNoInOrderConfirmationTest): Timed out after 90 seconds
HSDA_17_VerifyShippingOrderNoInOrderConfirmationTest): Session not available
HSDA_1_BuyVoice): Session not available 
HSDA_1_BuyVoice): Unable to locate element:
HSDA_9_CreateAccounAndBuyDeviceInCheckoutTest): Timed out after 90 seconds
HSDA_2_BuyMapswithNewUserAtCheckOutTest): Address already in use: connect
HSDA_69_ChangeBillingAddressInNonUSLocaleForAllFieldsTest): Index: 0, Size: 0
"|awk -F :   ($1 in a){next;} !a[$1]++; 

HSDA_17_VerifyShippingOrderNoInOrderConfirmationTest): Timed out after 90 seconds
HSDA_1_BuyVoice): Session not available 
HSDA_9_CreateAccounAndBuyDeviceInCheckoutTest): Timed out after 90 seconds
HSDA_2_BuyMapswithNewUserAtCheckOutTest): Address already in use: connect
HSDA_69_ChangeBillingAddressInNonUSLocaleForAllFieldsTest): Index: 0, Size: 0

这应当做到:

awk -F_  !a[$2]++{b[NR]=$0}
END{for(NR=1;NR<=length(b);NR++) if(b[NR]!~/^$/) print b[NR]}  filename

Test:

[jaypal:~/Temp] cat file
HSDA_17_VerifyShippingOrderNoInOrderConfirmationTest): Timed out after 90 seconds
HSDA_17_VerifyShippingOrderNoInOrderConfirmationTest): Session not available
HSDA_1_BuyVoice): Session not available 
HSDA_1_BuyVoice): Unable to locate element:
HSDA_9_CreateAccounAndBuyDeviceInCheckoutTest): Timed out after 90 seconds
HSDA_2_BuyMapswithNewUserAtCheckOutTest): Address already in use: connect
HSDA_69_ChangeBillingAddressInNonUSLocaleForAllFieldsTest): Index: 0, Size: 0

[jaypal:~/Temp] awk -F_  !a[$2]++{b[NR]=$0}END{for (NR=1;NR<=length(b);NR++) if (b[NR]!~/^$/) print b[NR]}  file
HSDA_17_VerifyShippingOrderNoInOrderConfirmationTest): Timed out after 90 seconds
HSDA_1_BuyVoice): Session not available 
HSDA_9_CreateAccounAndBuyDeviceInCheckoutTest): Timed out after 90 seconds
HSDA_2_BuyMapswithNewUserAtCheckOutTest): Address already in use: connect
HSDA_69_ChangeBillingAddressInNonUSLocaleForAllFieldsTest): Index: 0, Size: 0

这或许有助于你:

 sed  :a;$!N;/^([^:]*:).*
1/s/
.*//;ta;P;D  file

或许无法优化文字,它有两个论点(博客档案和产出档案)。

#!/bin/sh

file=$1
output=$2

while read line; do
    word=$(echo $line | awk  { print $1 } )
    grep "$word" $output > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo $line >> $output
    fi
done < $file

使用<代码>的的一种方式:

内容:

## Set label  a 
:a

## While not end of file...
$! {

    ## If pattern space only has one line, read next one.
    /
/! N

    ## If both lines have same characters until first colon, delete
    ## second one.
    /^([^:]*):.*
1/ s/
.*$//

    ## If last substitution command (delete repeated line) go to
    ## label  a  without reading next input line.
    ta  

    ## We are here because last substitution command failed. It
    ## means that first line is different from second one so 
    ##  print && delete  first one and save second one to compare in 
    ## next loop.
    P   
    D   
}

## This point should be only at end of file, and default action is  print  so
## it will print last line left in pattern space.

执行文字:

sed -f script.sed infile

结果:

HSDA_17_VerifyShippingOrderNoInOrderConfirmationTest): Timed out after 90 seconds
HSDA_1_BuyVoice): Session not available 
HSDA_9_CreateAccounAndBuyDeviceInCheckoutTest): Timed out after 90 seconds
HSDA_2_BuyMapswithNewUserAtCheckOutTest): Address already in use: connect
HSDA_69_ChangeBillingAddressInNonUSLocaleForAllFieldsTest): Index: 0, Size: 0




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签