Monday 17 February 2014

Editing Multiple Files in Linux

Editing multiple file in linux can enhance user's working speed , here I am discussing some basic commands which may be helpful for all of us. Please download the file from here.

Editing multiple files in linux for advanced users
Here the list of some commands by using this you can directly switch between multiple files. This is very useful for the frequent linux users, it can enhance your working speed.

1. Put all the file names with vi
vi file1 file2 .... (you can write name of files you want to edit or write something)
In terminal first file will open, by using normal vi commands like Esc : i you can easily insert, anything
Esc :w (for write the file)
       :n (you can switch to the next file)
Esc :wq (for save all the files)

2. No need to put all the names of the file, first you give only single file name
vi file1 (you can edit this file using normal vi commands)
Esc :w (for write the file)
      :e file2 (file2 is the name of file you want to open after file1)

3.  vi remember two file names at the same time current and alternative file names,
symbol % (current file name)
symbol # (alternative)
vi file1 file2
you wrote something in file1 using Esc -i, Esc :w write, than you move to the next file either by :n or :e here you made some changes in file2, but you don't want to save current changes and want to go back in first file, for this you can use the following command.
:e!#  this command will discard your edits in the current file and return to the last saved version of the current file (file1)
:w %.new  this command will make copy of your current edited file with suffix .new (output: file1.new)

You want to find and replace one string in multiple files in the same directory, you can do it with the following command in vi
vi *.txt (to open all the files)
:argdo %s/findme/replaceme/g | wq

you can use sed command also for the same

sed -i 's/findme/replaceme/g' *.txt

Tuesday 11 February 2014

Basic commands for linux

Linux commands are very useful for file handling, here I am discussing some basic commands for linux. Please download printable version here.


***** cut ****
cut command is used to extract bytes/characters/fields (separated by the delimiter) from each line when input used as a file.

Syntax
cut [-b] [-c] [-f ] [-d] [file]    (User can use any of the option with or without delimiter)

-b:  give the range of the bytes which will be returned
-c: to print characters of line by position you can use the particular position or range for example
                cut -c2 file    (Outputs the second character of every line of the file)
                cut -c1,4 file (Outputs the first & fourth character from the every line of the file)
                cut -c2-5 file (Outputs the second to fifth character of every line of the file)
                cut -c-5 file (Outputs the first five characters, b/c only last position defined )
                cut -c2- file (Outputs from second to last, because only first position defined)
-f: to print specific fields separated by delimiter like space(' '), tab('\t'), :, ; etc
-d : option for delimiter
                cut -d' ' -f3 file (Outputs third field in each line by treating space as a delimiter)
                cut -d' ' -f3,4 file (Outputs more fields, by specifying the fields positions)
                cut -d' ' -f2-5 file (Outputs fields range, by specifying the fields positions)
                cut -d' ' -f-5 file (Outputs the first five fields, b/c only last position defined )
                cut -d' ' -f2- file (Outputs second to last field, b/c only first position defined )

**** comm ****
comm command is used for comparing the two randomly sorted files line by line.

Syntax
comm [options] file1 file2

                comm file1 file2  (Outputs three column, first column contains unique in first, second column contains unique in second, and third column contains common in both files)
                comm -12 file1 file2 (Output common in both files, here -12 suppress the first and second column)
                comm -13 file1 file2 (Output unique in second file, here -13 suppress the first and third column)
                comm -23 file1 file2 (Output unique in first file, here -23 suppress the second and third column)

**** paste and cat ****
These two commands used for combining two or more files.

                paste file1 file2 file3.... (Output content of all files in a single file pasted side by side)
                cat file1 file2 file3 ...    (Output content of all files in a single file pasted below the file1 in the same order)

**** rm ****
rm command is used for removing the files.

Syntax
rm [options] file

            rm file (remove file from the file system)
            rm -i file (prompt before removing the file)
            rm -I file1 file2 file3 ... (prompt once before removing more than three files)
            rm -fr file (remove entire thing recursively)
            rm -r directory/ (remove directories and their contents recursively)

******* The End ******


Saturday 8 February 2014

Basic commands for the Linux vi Editor

vi is a powerful screen oriented text editor for Linux/Unix operating system, that's why very useful for file handling mainly in the field of computational biology. Please download printable version here

*** Some Basic vi Commands *** 

For editing the file

1. vi filename  [create or edit file]

2. vi -r filename [recover the filename that was being edited]

3. Esc i [for inserting text]

4. Esc I [inserting text at the beginning of the current line]

5. Esc o [open and put a text in a new line below current line]

6. Esc O [open and put a text in a new line above current line]

7. Esc colon wq [for save and quit]

8. Esc colon q! [for quit only not for save]

9. Esc u [for undo whatever you just did]

10. Esc $ [move cursor to the end of the current line]

11. arrow keys for move up (k), down (j), left (h) and right (l)

12. :s/findme/replaceme/g [for finding and replacing with particular character/word]

For deleting the text

1. Esc x [delete single character under the cursor if Nx means N number of characters]

2. Esc dw [delete the single word beginning with character under cursor if dNw same as previous]

3. dd [delete entire current line, if Ndd same delete N lines beginning with the current line]

For copy paste

4. yy [copy the current line]

5. p [paste the line]

for more information you can type vi -h 

*******   The end ******