0

I have a single file contents with thousand of IP's, my problem is how can i add all the IP line with 'allow from' for example as below:

From This:

27.146.0.0/16
49.50.12.0/19
49.50.44.0/20
49.50.60.0/22

To Be this:

allow from 27.146.0.0/16
allow from 49.50.12.0/19
allow from 49.50.44.0/20
allow from 49.50.60.0/22
allow from 49.124.0.0/15
allow from 57.73.15.0/24

Is there any Windows tools can do that or Linux command should be ok too. Please help

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • 2
    on windows, you could open the file with any editor with replace feature (regex supported), for example, vim, emacs, notepad++, ultraedit, editplus, eclipse, (MS-Word?) and replace the beginning of the line with the fixed text. – Kent Aug 20 '13 at 14:37
  • The editors column/block mode (UltraEdit: Alt-C) is even better for the example above (no dialog/mouse). Select the column before the lines and just type "allow from ", done. Don't forget to exit column/block mode again. – svante Aug 20 '13 at 15:14

3 Answers3

2

What's wrong with using a real manly editor?

In Vim/gVim:

  1. gg: place the cursor in the start of the file
  2. Control + v: start visual block mode
  3. G: move the cursor to the end of the file, selecting every line
  4. I: Enter Insert Mode
  5. Type your string: allow from
  6. Press ESC to leave Insert Mode.
  7. :wq Save and exit.
  8. Done!

Sed is probably easier...

KurzedMetal
  • 12,540
  • 6
  • 39
  • 65
  • 1
    I normally find that using vim macros/block mode is the way to go in these types of situations. It gets the job done way faster then screwing around with a complex sed/awk command. – Grammin Aug 20 '13 at 15:02
  • Virtual block mode is great unless you are working on a very large file / data set. Sed/awk are not complicated for things like this. "sed -i 's/^/allow from /' file" – dtorgo Aug 20 '13 at 20:29
1

Linux

With awk:

$ awk '$0="allow from "$0' file
allow from 27.146.0.0/16
allow from 49.50.12.0/19
allow from 49.50.44.0/20
allow from 49.50.60.0/22

As $0 is the whole string, we append text in the beginning of it. Then, it gets printed because the default behaviour of awk is {print $0}.

With sed:

$ sed 's/^/allow from /' file
allow from 27.146.0.0/16
allow from 49.50.12.0/19
allow from 49.50.44.0/20
allow from 49.50.60.0/22

As ^ means beginning of the line, we replace it with the "allow from " text. This way, the "new beginning of file" becomes the text you want to add.

Both examples will show you the output in your screen. To save it, do command file > new_file.

Windows

This answer may help you: Adding text to start of each new line in a .txt file. However, as Kent pointed out in comments, any editor can make it. I strongly recommend Notepad++ for this kind of purposes.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

You can do this with awk

awk '{print "allow from "$0}' <filename>
iamauser
  • 11,119
  • 5
  • 34
  • 52