0

I have a rather large file (single column) with data similar to this:

BT1111
2.2.2.2/3
3.3.3.3/4
7.2.1.1/5
BT6766
2.2.1.1/5
4.5.1.1/7
BT9898
4.4.4.4/2
8.8.8.8/9

I wish to find a function that can align it into two columns, by moving all entries starting with digit one column ($1 to $2) and enrich it with the corresponding BT field, so desired output should be

BT1111;2.2.2.2/3
BT1111;3.3.3.3/4
BT1111;7.2.1.1/5
BT6766;2.2.1.1/5
BT6766;4.5.1.1/7
BT9898;4.4.4.4/2
BT9898;8.8.8.8/9

I can't imagine how to ensure the "look for next occurence" should be performed, but hope there is a function for it I have managed to overlook ?

nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52

2 Answers2

2
perl -nle'if (/^\D/) { $n=$_ } else { print "$n;$_" }' input.txt

See Specifying file to process to Perl one-liner for alternate usages.

ikegami
  • 367,544
  • 15
  • 269
  • 518
1
$ awk '/BT/{a=$1; next}{print a ";" $1}' input.txt
BT1111;2.2.2.2/3
BT1111;3.3.3.3/4
BT1111;7.2.1.1/5
BT6766;2.2.1.1/5
BT6766;4.5.1.1/7
BT9898;4.4.4.4/2
BT9898;8.8.8.8/9
Marc Lambrichs
  • 2,864
  • 2
  • 13
  • 14
  • Thank you very much ! - by parsing it I realized that I had been overlooking lineshifts that caused a mess - upon getting rid of these this simple solution was just what I dreamed for. – Leif Hvitved Sep 05 '17 at 09:48
  • As per the original question, the criterion is *"all entries starting with digit"*, not "entries not starting 'BT'". – Borodin Sep 05 '17 at 10:27
  • `..and enrich it with the corresponding BT field.` The input OP provides is clear. – Marc Lambrichs Sep 05 '17 at 10:31