Reputation: 11
I have output from a firewall configuration that contains lines with data in a specific format. I need to transform this data for a spreadsheet to split groups of 4 columns from each line into a new, while retaining the first and last delimited fields of the line from which it came, then removing the first line.
I've attempted to use awk
, ed
, rs
, tr
, and can't seem to figure out how to properly do this. Each line is a different length, but the fields will always be a group of 4 starting at the second delimited field. So fields 2,3,4,5; 6,7,8,9; 10,11,12,13; 14,15,16,17,
etc... length of lines could be up to 128 fields
The data i'm attempting to transform looks as follows
startoflinesampletext_25;10.2.8.13;25;10.2.8.13%2;25;10.2.8.14;25;10.2.8.14%2;25;10.2.8.15;25;10.2.8.15%2;25;10.2.8.56;25;10.2.8.56%2;25;10.2.8.57;25;10.2.8.57%2;25;endoflinesampletext
The expected result I'm hoping for would look like this :
startoflinesampletext_25;10.2.8.13;25;10.2.8.13%2;25;endoflinesampletext
startoflinesampletext_25;10.2.8.14;25;10.2.8.14%2;25;endoflinesampletext
startoflinesampletext_25;10.2.8.15;25;10.2.8.15%2;25;endoflinesampletext
startoflinesampletext_25;10.2.8.56;25;10.2.8.56%2;25;endoflinesampletext
startoflinesampletext_25;10.2.8.57;25;10.2.8.57%2;25;endoflinesampletext
Upvotes: 1
Views: 52
Reputation: 58391
This might work for you (GNU sed):
sed -E 's/^(([^;]*)(;[^;]*){4})(.*(;.*))$/\1\5\n\2\4/;P;D' file
Replace the fourth ;
by the last field, followed by a newline, followed by the first field, print/delete the first line and repeat. The iteration whittles down the string until pattern matching no longer works and the last record is printed by default.
Upvotes: 0
Reputation: 67477
awk
to the rescue!
$ awk -F';' 'BEGIN{OFS=FS} {for(i=2;i<NF;i+=4) print $1,$i,$(i+1),$(i+2),$(i+3),$NF}' file
startoflinesampletext_25;10.2.8.13;25;10.2.8.13%2;25;endoflinesampletext
startoflinesampletext_25;10.2.8.14;25;10.2.8.14%2;25;endoflinesampletext
startoflinesampletext_25;10.2.8.15;25;10.2.8.15%2;25;endoflinesampletext
startoflinesampletext_25;10.2.8.56;25;10.2.8.56%2;25;endoflinesampletext
startoflinesampletext_25;10.2.8.57;25;10.2.8.57%2;25;endoflinesampletext
note that there is no validation that the fields are multiple of four.
Upvotes: 1