Friday, May 17, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 1012  / 3 Years ago, wed, october 13, 2021, 2:19:47

I need to combine the first value ($1) of consecutive rows if their fourth value ($4) is the same (I-PER).



I managed to filter the values I need simply using awk:



awk ' ($4 == "I-PER") {printf $1; printf "
" }


I also found how to merge rows with duplicate column values but not consecutive ones.



Example (Input):



Comandante  comandante  NP00000 I-PER
de de SPS00 I-PER
la el DA0FS0 I-PER
Guardia guardia NP00000 I-PER
Civil civil NP00000 I-PER
Pamplona pamplona NP00000 I-LOC
Poblador poblador NP00000 I-PER


Example (Output):



Comandante de la Guardia Civil
Poblador

More From » command-line

 Answers
4

Another awk solution to avoid printing repeated
ewlines if condition didn't meet in any line:



awk '($4=="I-PER"){ printf SEP$1; SEP=" "; C=1; next } 
C==1{ SEP=""; print ""; C=0} END{print ""}' infile


example input:



Comandante  comandante  NP00000 I-PER
de de SPS00 I-PER
la el DA0FS0 I-PER
Guardia guardia NP00000 I-PER
Civil civil NP00000 I-PER
no I-PER in fourth column
anotherline no I-PER in fourth column
Pamplona pamplona NP00000 I-LOC
Poblador poblador NP00000 I-PER


The output is:



Comandante de la Guardia Civil
Poblador

[#8657] Friday, October 15, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pardsea

Total Points: 290
Total Questions: 115
Total Answers: 98

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
;