Editing multiple lines for a given file in linux - linux

I have to edit multiple lines in a given file in linux.
Currently i am using sed and awk to edit my configurations file inline.
I tried using sed and awk, both work fine.
But I wanted to know if there are anymore options/command to do the same?
I am new to linux. Appreciate your help!

You can use nano cmd to edit your file simply.
To open a file, pass the filename as an argument:
nano ~/public_html/index.html
You can also open files at a specific line or column number:
nano +LINE /path/to/file
nano +LINE,COLUMN /path/to/file
To open a file as read only:
nano -v myfile
To search for text in a document, use ^W. This will open a search prompt and a submenu of search-related commands.
^G Get Help
^Y First Line
^T Go To Line
^W Beg of Par
M-J FullJstify
M-B Backwards
^C Cancel
^V Last Line
^R Replace
^O End of Par
M-C Case Sens
M-R Regexp
To save your work, use ^O or “WriteOut”. This will save the document and leave nano open for you to continue working.
Use ^X to exit nano. If you have not saved your work, you will be prompted to save the changes or cancel the exit routine.
If Nano cmd is not supporting in your OS run above cmd:
For Debian/Ubuntu:
apt install nano
For CentOS/Fedora:
yum install nano
For More Information visit this link What is Nano Command

Related

Nano - File already being edited - How do I switch to the open file?

I have a tendency to miss-type while trying to Ctrl-X to exit nano.
Every now and then I'll try to re-open the file and get the Nano "File xxx is being edited". How do I switch back to the open file? And also, what am I miss-typing to cause this to happen?
First check if there is a process editing the file.
$> jobs
You will get a list of background jobs, look for Stopped ones.
If there is and it's your editor, try re-attaching to it:
$> fg <n>
Else, if there is no running nano, make sure there is no lock-file.
ls -A *.swp
If there is, remove it.
rm nnn.swp
If you still have trouble editing the file, reboot and restart above.
Maybe there are more things to try, but that's all I can think of...
nano creates a lock file once you try to edit the file, I would say just try to move it and it might work.

How to edit a text file in my terminal

I'm using Linux mint and using the vi command to create text files, now that I created a text file and saved it. How do I get back into to edit the text file again?
vi helloWorld.txt
Try this command:
sudo gedit helloWorld.txt
it, will open up a text editor to edit your file.
OR
sudo nano helloWorld.txt
Here, you can edit your file in the terminal window.
Open the file again using vi. and then press " i " or press insert key ,
For save and quit
Enter Esc
and write the following command
:wq
without save and quit
:q!
Open the file again using vi. and then press the insert button to begin editing it.
If you are still inside the vi editor, you might be in a different mode from the one you want. Hit ESC a couple of times (until it rings or flashes) and then "i" to enter INSERT mode or "a" to enter APPEND mode (they are the same, just start before or after current character).
If you are back at the command prompt, make sure you can locate the file, then navigate to that directory and perform the mentioned "vi helloWorld.txt". Once you are in the editor, you'll need to check the vi reference to know how to perform the editions you want (you may want to google "vi reference" or "vi cheat sheet").
Once the edition is done, hit ESC again, then type :wq to save your work or :q! to quit without saving.
For quick reference, here you have a text-based cheat sheet.
You can open the file again using vi helloworld.txt and then use cat /path/your_file to view it.
you doesn't have root access type this
sudo su
If you want to use nano editor to create file in same directory where you headed
nano filename.format
eg:- nano attendaence.txt
Ctrl+o
Enter
Ctrl+x
If you want to use nano editor to create file in other directory where you headed
nano location/filename.txt
eg1:- nano home/ec2-user/public_html/attendence.php
eg2:- nano /home/ec2-user/public_html/attendence.php
2. Ctrl+o
3. Enter
4. Ctrl+x
Create file without editor in same directory
touch filename.format
eg:- touch helloworld.html
Create file without editor in another directory
touch location/filename.format
eg:- touch var/www/index.html

Can not edit cronjobs file in Debian with crontab -e

I have had several Debian servers and always edited cronjobs in this way:
crontab -e
and
Ctrl+x
Just got a new server and can not do it in this way anymore.
When I enter crontab -e, the file opens but I can't write anything. I can move cursor up and down but can't write. I even can not exit from this file because Ctr+x doesn't work.
When I open a file there is some information and the rest empty lines contain tildes ~ in the beginning of each line.
Any ideas how can I edit this file?
Thanks.
You need to turn on insert mode. After entering crontab -e, press i to turn on insert, enter your full line, press esc to finish entering, and then hold down shift and press z twice to save the file. This is how I managed to do it in vi/vim
As one other person has suggested, vim is obviously the default editor on your new server. You can test this by running
EDITOR=pico crontab -e
Substituting whatever is your actual preferred editor (sounds like it may be nano or pico). If that works, you should try one of the following:
edit your login script to set that environment variable on login (sets the editor just for that user)
Make sure your favourite editor is is installed and run the following (as root): update-alternatives --config sensible-editor
You can then choose the default editor for all users (they can override it individually by doing option 1).
~ would suggest that you are now editing your crontab using vi/vim instead of your usual editor
so Ctrl-X wont work, try Esc :wq
Do you have the right permissions? maybe you should open it as root user if not.
check it doing this:
ls -all $(which crontab)
if not you can change them..take a look here too..maybe you find something more!
hope it helps.

adding a shell script to a configuration file

I'm pretty new to shell scripting and linux in general. Basically, I need to change the configuration file for logging out so that when a user logs out, a certain shell script is run.
Now, I've located the logout configuration file and opened it with vi using this command
$ vi ~/.bash_logout
At this point, I'm experiencing some very weird behavior. When I try to type a character, the cursor jumps around seemingly erratically. What could this be due to? I'm running the latest version of ubuntu.
And once I get that figured out, what's the command to run a .sh file from within this configuration file?
If you're having trouble with vi, try using nano instead. nano .bash_logout
If you do need to use vi for some reason, "i" will put the editor into insert mode, and ESC will take it out of insert mode when you're done. ":wq" will write and quit the editor.
To run a command, just put it in the .bash_logout file as you would type it on the commandline.
Some other useful commands:
a insert after selected character
o insert at next line
O insert at previous line
r replace a single character
R replace mode
:q! quit without saving
:w save
:wq save and quit
To get familiar with Vi and its brother Vim ("VI improved") I recommend the book "A Byte of Vim", you can read it online or download for free at http://www.swaroopch.com/notes/Vim
You can permanently change your editor option. To find out what your current one is, type this:
export | grep -i edit
To change it on Ubuntu:
sudo update-alternatives –config editor
On any other BASH prompt, just do this:
export EDITOR="nano"
Replace 'nano' with 'vi', 'emacs', or any other preferred editor. You can also add this to your .bashrc by typing the following:
echo 'EDITOR="nano"' >> ~/.bashrc

Open Vim from within a Bash shell script

I want to write a Bash shell script that does the following:
Opens a file using Vim;
Writes something into the file;
Saves the file and exits.
echo 'About to open a file'
vim file.txt # I need to use vim application to open a file
# Now write something into file.txt
...
# Then close the file.
...
echo 'Done'
Is that possible? I found something called Vimscript, but not sure how to use it.
Or something like a here document can be used for this?
Update: I need to verify that Vim is working fine over our file
system. So I need to write script that invokes Vim, executes some
command, and closes it. My requirements do not fit into doing stuffs
like echo 'something' > file.txt. I got to open the file using Vim.
ex is the commandline version for vi, and much easier to use in scripts.
ex $yourfile <<EOEX
:%s/$string_to_replace/$string_to_replace_it_with/g
:x
EOEX
Vim has several options:
-c => pass ex commands. Example: vim myfile.txt -c 'wq' to force the last line of a file to be newline terminated (unless binary is set in some way by a script)
-s => play a scriptout that was recorded with -W. For example, if your file contains ZZ, then vim myfile.txt -s the_file_containing_ZZ will do the same as previously.
Also note that, invoked as ex, vim will start in ex mode ; you can try ex my_file.txt <<< wq
You asked how to write "something" into a text file via vim and no answer has necessarily covered that yet.
To insert text:
ex $yourfile <<EOEX
:i
my text to insert
.
:x
EOEX
:i enters insert mode. All following lines are inserted text until . is seen appearing by itself on its own line.
Here is how to search and insert as well. You can do something such as:
ex $yourfile <<EOEX
:/my search query\zs
:a
my text to insert
.
:x
EOEX
This will find the first selection that matches regex specified by :/, place the cursor at the location specified by \zs, and enter insert mode after the cursor.
You can move \zs to achieve different results. For example:
ex $yourfile <<EOEX
:/start of match \zs end of match
:a
my text to insert
.
:x
EOEX
This will change the first occurrence of "start of match end of match" to "start of match my text to insert end of match".
If you want to allow any amount of whitespace in your searches between keywords, use \_s*. For example, searching for a function that returns 0: :/\_s*return\_s*0}
If you are wanting to see the work being done inside vim or gvim you can use --remote-send
gvim --servername SHELL_DRIVER
bashpromt# cat mybash.sh
#!/bin/bash
echo "about to open $1"
gvim --servername SHELL_DRIVER $1 #I need to use vim application to open a file
#now write something into file.txt and close it
gvim --servername SHELL_DRIVER --remote-send '<ESC>i something to the file<ESC>:wq<CR>'
echo "done."
This will be slow but will do what you want it to.
First we open a gvim in which we can open all of our files (for efficiency)
With the first gvim line we open the file in the previously opened gvim.
On the second gvim line we send a command to the previously opened instance of gvim (with the desired file still open).
The command is as follows:
<ESC> - get out of any mode that gvim might have been in
i something to the file - go into insert mode and type " something to the file"
<ESC> - exit insert mode
:wq - write the file and quit vim
Recently, I have answered a similar question, “Automated editing
of several files in Vim”. May be the solution that I describe there
will satisfy your needs.

Resources