How to Insert Before IP [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
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

What's wrong with using a real manly editor?
In Vim/gVim:
gg: place the cursor in the start of the file
Control + v: start visual block mode
G: move the cursor to the end of the file, selecting every line
I: Enter Insert Mode
Type your string: allow from
Press ESC to leave Insert Mode.
:wq Save and exit.
Done!
Sed is probably easier...

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.

You can do this with awk
awk '{print "allow from "$0}' <filename>

Related

Bash add at the end of the line not working [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I have a bash script that adds " at the end of some lines. In order to do this, I used sed 's/$/"/g', however, at most of the lines, it prints it at start of the line, replacing the first character. Example:
a
silly
example =>>>
"
"illy
example"
This behavior means you have DOS line ending in your file. You should convert your file to Linux file first.
In case you cannot do that, here is an alternate sed solution that takes into consideration presence of an optional \r (carriage return) before line break:
sed -E $'s/\r?$/"&/g' file

I cant find the files I save in vim [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
I am trying to learn code using a vim editor as I hear I can get superior long term benefits when I start making long codes and documents. I am a pretty big fan of keyboard shortcuts so I do not mind taking the long road to superior editing abilities. Any way the Linux book I am reading has recommended it and even has many exercises that require typing and saving in vim. The problem is that I cannot find the files I save in vim. I enter the escape mode hit the colon key and use the w option. The vim tells me that the file is saved but when later try to find it in my Linux directory I can find nothing. I have tried using the type, file, find, and locate commands as well as whereis even though this is for commands. Thank you for help in advance.
There is no such thing as escape mode. Most likely you are referring to normal mode. The easiest way to enter normal mode is by hitting the escape key, Esc. Once in normal mode you can save the contents of the current buffer into the file by typing :w. Alternatively you can specify a new file name with :w filename. Then you can quit vim with :q. These steps can be combined by typing :wq.
You mention that Vim tells you the file has been saved but you cannot find the file. In this case type Ctrl-g after saving the file, the file name relative to the current directory should be displayed.
Your "Linux book" almost certainly tells you to do something like:
$ vim filename
with filename being an existing file or a non-existing file. In both cases, doing :w<CR> in Vim, will write filename…
in the working directory if filename is just a file name, without anything "path-like":
$ cd /tmp
$ vim foo.txt
(do your thing)
:wq
$ echo $PWD/$_
/tmp/foo.txt
at the given location relative to the working directory if filename is a relative path:
$ cd ~/Documents
$ vim exercices/foo.txt
(do your thing)
:wq
$ echo $PWD/$_
/home/rubberbandface/Documents/exercices/foo.txt
at the given location if filename is an absolute path:
$ vim ~/Documents/exercices/foo.txt
(do your thing)
:wq
$ echo $_
/home/rubberbandface/Documents/exercices/foo.txt
That said, you shouldn't learn Linux, Vim, and programming at the same time.

How to remove comments of ASM code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have an directory with about 300 source code in ASM.
So, I need to remove the comments (";") of this codes.
Until now, I removed the comments of 3 files, where each file has 100 lines of code.
Somebody know a script that can help me?
Sed is your friend:
find <asm_dir> -type f | xargs sed -i -e '/^;/d' -e 's/^\([^;]*\);.*$/\1/'
The first expression deletes lines that begin with the comment character ;. The second expression strips inline comments (from ; to the end of the line).
You can simply accomplish this in e.g. Vim or Emacs. I'm going to discuss Vim here.
Assuming you have all your .asm files in one directory go to your shell and do something like
cd /path/to/my/files
gvim *.asm
This will open all your .asm files for editing in gvim (graphical vim). If you are not familiar with Vim it's a great text editor and we are going to record a macro to do our job. Only do what I say or you will mess things up :) To be on the safe side you should backup your files in case you mistype something and delete more than you wish to.
Type these characters (or press the appropriate thing on your keyboard as specified in <>'s):
qa:g/^\s*;/d<Enter>:n<Enter>q
This will start recording a macro [q], store it in [a], execute a global deletion of every line in the first file that starts with optional whitespace followed by a semicolon [:g/^\s*;/d<Enter>], move to the next file [:n<Enter>] and save the macro [q]. All you have to do now is run this macro as many times as you have files left. You can either get this by running ls *.asm | wc -l in your directory with the saved files or you can simply overshoot it and input a larger number, Vim will stop on the last file and notify you there's no more files to edit. So with the overshooting example you could type
1000#a
and the macro will start running through all the files. This may take some time so be patient. Once it's done we still haven't saved our files so we could check the results before commiting to them. You can check a couple of files if they look OK and if yes type
:wa<Enter>
All your files are saved now and you can exit Vim with ZQ or :q<Enter>.

How to add a character in front of multiple words in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is a text file containing many words,each is separated by space breaks or line breaks.
Now I want to add a character,like "#" "$" "#" in front of each of them,
and I found doing this job one by one will take too much time,
are there any better ways,in bash?
Try using sed
sed -r 's/([^ ]+)/#\1/g' file
Or more concisely,
sed -r 's/[^ ]+/#&/g' file
Sample input
abc def pqr-stu xyz
Output
#abc #def #pqr-stu #xyz
Using sed, you could say:
sed 's/\b\w/#&/g' inputfile
This would append # before every word.

Clear a terminal screen for real [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
Using the clear command on the terminal only fools the user into thinking the screen has been cleared...you can still see output from the previous commands when you scroll using the mouse. This makes life difficult when you are drowning in a tsunami of text.
Various solutions (escape code etc.) which can be found on the Internet are only variations of what the clear command already does.
So how do you clear the contents of a terminal in Linux for real?
Use the following command to do a clear screen instead of merely adding new lines ...
printf "\033c"
yes that's a 'printf' on the bash prompt.
You will probably want to define an alias though...
alias cls='printf "\033c"'
Explanation
\033 == \x1B == 27 == ESC
So this becomes <ESC>c which is the VT100 escape code for resetting the terminal. Here is some more information on terminal escape codes.
Edit
Here are a few other ways of doing it...
printf "\ec" #\e is ESC in bash
echo -en "\ec" #thanks #Jonathon Reinhart.
# -e Enable interpretation of of backslash escapes
# -n Do not output a new line
KDE
The above does not work on the KDE console (called Konsole) but there is hope! Use the following sequence of commands to clear the screen and the scroll-back buffer...
clear && echo -en "\e[3J"
Or perhaps use the following alias on KDE...
alias cls='clear && echo -en "\e[3J"'
I got the scroll-back clearing command from here.
Try reset. It clears up the terminal screen but the previous commands can be accessed through arrow or whichever key binding you have.
tput reset
That will do the trick!
None of the answers I read worked in PuTTY, so I found a comment on this article:
In the settings for your connection, under "Window->Behavior" you'll find a setting "System Menu Appears on ALT alone". Then CTRL + L, ALT, l (that's a lower case L) will scroll the screen and then clear the scrollback buffer.
(relevant to the OP because I am connecting to an Ubuntu server, but also apparently relevant no matter what your server is running.)
Clean the visible screen
clear
Clean screen and clear buffer
clear && clear
Clean and 1-sec delay
reset
Clean without 1-sec delay
tput reset
My favorite human friendly command for this is:
reset
Tested on xterm and VT100. It also helps after an abnormal program termination.
Keeps the command buffer, so up-arrow will cycle through previous commands.
I know the solution employing printing of new lines isn't much supported, but if all else fails, why not? Especially where one is operating in an environment where someone else is likely to be able to see the screen, yet not able to keylog. One potential solution then, is the following alias:
alias c="printf '\r\n%.0s' {1..50}"
Then, to "clear" away the current contents of the screen (or rather, hide them), just type c+Enter at the terminal.
The following link will explain how to make that alias permanent so you don't have to keep typing it in.
https://askubuntu.com/questions/17536/how-do-i-create-a-permanent-bash-alias
These are the steps detailed at that link.
vim ~/.bashrc or gedit ~/.bashrc or what ever text editor you like
put alias cls='printf "\033c"' at the bottom of the file
save and exit
. ~/.bashrc (and yes there should be a space between . and ~)
now check to see if everything worked!
I take no credit for this information just passing it along.
Just to add that tmux scroll buffer does not clear with clear, reset or printf. You need to :clear-history. See link.
With KDE and Ubuntu 12.04 LTS and the "Konsole" terminal, none of the posted answers work. However, pressing default keyboard shortcut CTRL+Shift+X does work! Source:
https://bugs.kde.org/show_bug.cgi?id=288913
echo -e "\e[3J"
This works in Linux Machines
Compile this app.
#include <iostream>
#include <cstring>
int main()
{
char str[1000];
memset(str, '\n', 999);
str[999] = 0;
std::cout << str << std::endl;
return 0;
}

Resources