How to remove comments of ASM code [closed] - linux

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>.

Related

Renaming All Files in a Directory

I split a large text file into 60 chunks, which are are named xaa, xab, xac,...xcg. I want to rename these files so that they all end with .txt
How can I do this from the linux command line?
Looked in the split command for the ability to customize the filenames. Looked on Stack Overflow for other solutions but the ones I've come across are all too specific to the OP's situation.
Assuming that your shell is the default Bash:
for f in x??; do mv "$f" "$f.txt"; done
If you want to be more specific, you could say x[abc][a-z] instead of x??.
This is good enough for a one-liner. In a script you would want to check that "$f" exists before trying to rename it.

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.

Remove colour code from linux files [duplicate]

This question already has answers here:
Can I programmatically "burn in" ANSI control codes to a file using unix utils?
(2 answers)
Closed 6 years ago.
I have an output file from a testing script (which I cannot alter), the output looks great in the terminal thanks to the encoding, which displays the output in nice colours.
However when I vim the file, I get the following:
^[[1m0024^[[0m, ^[[36munknown.10^[[0m --> ^[[32mUNKNOWN^[[0m
I would rather the file contained:
0024, unknown.10 --> UNKNOWN
There are a couple of similar questions on stackover flow, but so far I have not found a solution that works for me.
Any help would be greatly appreciated!
Many thanks!
Additional info:
I don't want to conceal the colour characters, I would like to remove them from the file.
The output goes into an evidence file, and then that file is pushed up to a GIT for the team to review. It is difficult to the GIT UI with those colour codes :(
To remove color control character, you may use the following sed command:
sed 's/\x1b\[[^\x1b]*m//g' file
As indicated in here, the a color code is composed of <Esc>[FormatCodem.
The escape character is \x1b in hexadecimal (sometimes noted as \e or \033).
The command looks for the sequence escape followed by square bracket \x1b\[ until the character m, if found it deletes it.
Everything in between these 2 characters is allowed except the escape character itself [^\x1b]*. This allows to have the shortest regex.
If you can't remove them from the tool producing the output, you could still remove them afterwards with the following sed command :
sed -r 's/\^\[\[[0-9]{1,2}m//g'
Example :
$ echo """^[[1m0024^[[0m, ^[[36munknown.10^[[0m --> ^[[32mUNKNOWN^[[0m""" | sed -r 's/\^\[\[[0-9]{1,2}m//g'
0024, unknown.10 --> UNKNOWN

How to Insert Before IP [closed]

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>

bash auto complete [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Command completion in bash(1) is quite handy; I like this feature very much. But I have a question.
For example, we all have Documents and Downloads in $HOME directory.
So if I input cd ~ and then press TAB, then it will traverse all the directory under $HOME. When Documents come up and the shell now displays cd ~/Documents, I want to go deeper into this directory. For example, there is a work directory in Documents. My intention is to cd to the work directory.
So what should I do?
I usually input a w then press TAB, but when this action repeats too much I found it quite annoying. And sometimes I don't know exactly what files are under this directory.
Do you have any good ideas?
ps: In fact, i met this problem when i was using vim. I input :e then TAB, it will cycle the subfolders, the only way i found to stop the cycling and enter the next level is input a /. So the path has double '/' in the end.
You could switch your shell to ZShell, which allows you to tab through directories without typing the first letter.
For example, you can type
cd ~/
Pressing TAB will bring up a list of all subfolders (like in bash), but pressing TAB multiple times will allow you to cycle through the subfolders. Pressing the -> or / key will allow you to start tab-completing inside that directory, and so on.
You can sometimes use chsh (man chsh for more info) to change your shell, if it's your own personal machine, or your network might have a special way to change your shell. You might also want to Google for some common .zshrc / .zshenv settings or migrate your old .bashrc / .cshrc / .profile settings.
Zsh also has ways to set up TAB completion for other tasks, for example, it can TAB complete svn files based on those that aren't already in SVN (for svn add). To get these features, add
autoload -U compinit
compinit
to your .zshrc file. There are plenty more ways to customize your TAB completion in zsh (such as with case-insensitivity, or for arguments to different programs), but if you're interested in those, you can probably find more information than I know by searching.
Like the Jack Toole's zsh suggestion (which is a perfectly fine shell), if you have to use bash, you can stick this in your .bashrc for the same "continue to hit tab cycle through possibilities" feature:
test -n "$PS1" && bind TAB:menu-complete
There's also a project called bash-completion that provides a lot of other kinds of completion other than pathname completion.
If you don't know what is in the directory, just press TAB one more time.

Resources