Stefan
Stefan

Reputation: 23

Grouping output data with sed or awk

I'm trying to get a flatfile database filename=iplocationdata to be regrouped into sections.

This file gets updated frequently and it contains a couple of thousand lines, so I'd like to find a smart solution to script the outputs.

What I get looks like this:

NA
10.0.0.0/16
Dallas1
NA
10.1.0.0/16
Houston1
EMEA
10.2.0.0/16
Paris1
EMEA
10.3.0.0/16
London1
APAC
10.4.0.0/16
Hong-Kong1
APAC
10.5.0.0/16
Shanghai1

I need two different outputs.

The first one I solved with this:

awk 'NR%3{printf $0" ";next;}1' iplocationdata

NA, 10.0.0.0/16, Dallas1
NA, 10.1.0.0/16, Houston1
EMEA, 10.2.0.0/16, Paris1
EMEA, 10.3.0.0/16, London1
APAC, 10.4.0.0/16, Hong-Kong1
APAC, 10.5.0.0/16, Shanghai1

The second list, I want looking like this:

NA
10.0.0.0/16, Dallas1
10.1.0.0/16, Houston1
EMEA
10.2.0.0/16, Paris1
10.3.0.0/16, London1
APAC
10.4.0.0/16, Hong-Kong1
10.5.0.0/16, Shanghai1

This I have not been able to solve. I'd prefer a one liner if possible, does someone have a good solution for this?

Upvotes: 2

Views: 74

Answers (3)

jas
jas

Reputation: 10865

EDIT: Added missing ,

$ awk '$1==p{next} NR%3==1{print; p=$1} NR%3==2{printf "%s, ",$0} NR%3==0' s.txt

NA
10.0.0.0/16, Dallas1
10.1.0.0/16, Houston1
EMEA
10.2.0.0/16, Paris1
10.3.0.0/16, London1
APAC
10.4.0.0/16, Hong-Kong1
10.5.0.0/16, Shanghai1

Upvotes: 4

karakfa
karakfa

Reputation: 67467

some cryptic awk

$ awk 'NR%6!=4{ORS=(++c%5-3)^2==1?", ":RS; print}' file

NA
10.0.0.0/16, Dallas1
10.1.0.0/16, Houston1
EMEA
10.2.0.0/16, Paris1
10.3.0.0/16, London1
APAC
10.4.0.0/16, Hong-Kong1
10.5.0.0/16, Shanghai1

perhaps more readable as

$ awk '{c=NR%6} c!=4{ORS=c==2||c==5?", ":RS; print}' file

Upvotes: 0

Etan Reisner
Etan Reisner

Reputation: 80931

awk 'NR%3 {
    printf $0", "
    next
} 7' iplocationdata | tee outfile1 | awk 'BEGIN {
    FS=OFS=", "
}
$1 != p {
    p=$1
    print $1
}
{
    print $2,$3
}' > outfile2

Your original awk script for the first part (with the addition of the comma you showed in your output.

The second awk script uses the first script's output as input and saves the first field whenever it differs from the previously saved field, prints $1 for those lines and prints $2,$3 for every line.

Upvotes: 0

Related Questions