I have a text file with groups of line, from that I need only first three lines from each group.
File:
test1|pass
test1|pass
test1|pass
test1|pass
test1|pass
test2|fail
test2|fail
test2|fail
test2|fail
test3|pass
test3|pass
test3|pass
test3|pass
Expected Output:
test1|pass
test1|pass
test1|pass
test2|fail
test2|fail
test2|fail
test3|pass
test3|pass
test3|pass
What I have tried so far:
BEGIN {
FS = "|"
}
$1==x {
if (NR % 5 <= 3) {
print $0
}
next
}
{
x=$1
print $0
}
END {
printf "\n"
}
You can do this fairly concisely like this:
Output:
Explanation
ais an associative array. We use the first element of each line ($1) as a key intoaand increment its value. This value is then compared to3and if the comparison is true, the default block is executed ({print $0}).