Search This Blog

Thursday, January 23, 2020

Delete matched line with range in vim

You can use . to refer to the current line.

1) :g/foo/.,+2d will delete line with text foo and 2 lines after it

2) :g/foo/-3,.d will delete line with text foo and 3 lines before it

3) :g/foo/-1,+1d will delete line with foo, one line before it and one line after it

vim go to the definition

In vim rather than searching for the function definition or variable initialization you can type gd in command mode which will go to the local declaration of the word where cursor is type gD if you want to go to the global declaration.

Sunday, October 13, 2019

Remove lines using vim

To remove all the lines in vim that contain certain string execute:
:g/string/d

To do the opposite i.e remove the lines that do not contain certain string execute:
:v/string/d

Saturday, July 27, 2019

dd

dd is a command-line utility for Unix and Unix-like operating systems, the primary purpose of which is to convert and copy files.
dd stands for copy and convert (called dd because cc is already in use by C compiler).

[Options]
bs : BYTES read and write up to BYTES bytes at a time (default: 512)
if : FILE read from FILE instead of stdin
of : FILE write to FILE instead of stdout
status : LEVEL The LEVEL of information to print to stderr; 'none' suppresses everything but error messages, 'noxfer' suppresses the final transfer statistics, 'progress' shows periodic transfer statistics

[Examples]
1 - Wipe the disk partition
# dd if=/dev/zero out=/dev/sd<?><n>

2  - Make USB stick bootable
Step 1 : Find device name of USB stick using lsblk
Step 2 : Copy ISO
# dd if=/home/user/download/arch.iso of=/dev/sd<?> status=progress

? = disk name
n = partition number

Reverse of making USB stick bootable
# dd if=/dev/sd<?> of=backup.iso status=progess

Above command can be used to create single image of stick containing all the partitions/data which then can be restored on another machine by simply mounting image using
# mount backup.iso /mnt/pd -o loop

Monday, July 8, 2019

Special shell parameters

The shell treats several parameters specially.
These parameters may only be referenced; assignment to them is not allowed.

1 - $?
It expands to the exit status of the most recently executed foreground *pipeline.
0 exit status means the command was successful without any errors.
non-zero (1-255 values) exit status means command was failure.

*Pipeline: In Unix-like computer operating systems, a pipeline is a mechanism for inter-process communication using message passing. A pipeline is a set of processes chained together by their standard streams, so that the output text of each process (stdout) is passed directly as input (stdin) to the next one.

2 - $_
The underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list.
i.e: if you create a shell script with just echo $_ it will spit out the path of that script.

Subsequently, it expands to the last argument to the previous command.
e.g:
$ mkdir /home/xbot/foo/bar/dir
$ cd $_
$ echo $?
0
$ pwd
/home/xbot/foo/dir

Friday, April 19, 2019

killall command and signals.

killall sends a SIGTERM signal (if signal name is not specified explicitly) to all processes running any of the specified commands. 

[Syntax]
killall [options] command

killall supports regular expression matching of process names, via the -r option.

If the specified command contains slash (/) characters, the command is interpreted as a file name and processes running that particular file will be selected as signal recipients.

To make the killall operation interactive you can provide -i option which makes killall prompt for confirmation before sending signal.


Signals are software interrupts sent to a program to indicate that an important event has occurred.

You can use the options -s, --signal, and -SIGNAL options to send explicit signals.

[Example]
killall -s 9 chrome
killall -SIGKILL python

Some common signals :
SIGINT 2 - Issued if the user sends an interrupt signal (Ctrl + C).
SIGQUIT 3 - Issued if the user sends a quit signal (Ctrl + D).
SIGKILL 9 - If a process gets this signal it must quit immediately and will not perform any clean-up operations.

Thursday, March 14, 2019

prettify json file in vim.

:%!python -m json.tool

Thursday, December 6, 2018

read constantly updating file

tail -F foo.txt

[options]
-F: tracks changes to the file by filename and try to open the file if it's not present.

Thursday, November 15, 2018

delete column of csv

File: tmp.csv

Content:
a,b,c
1,2,3
10,20,30

Task: Remove 1st column.

Expected Output:
b,c
2,3
20,30

Command:
cut -d, -f1 --complement tmp.csv > new_tmp.csv

Options:
-d : delimeter
-f : fields to select
--complement: complement the set of selected bytes, characters or fields

Summary:
Above command is selecting the 1st column with a delimiter , and complementing the result set and saving output to new_tmp.csv.

Sunday, October 21, 2018

less command

less is the linux utility which can be used to read contents of text page by page.

[Syntax]
less [OPTIONS] [FILENAME]

[Options]
-N : Show number of lines.

[Navigation]
gg - Top
G - Bottom
f - Forward One Window
b - Backward One Window
q - Exit

[Search]
\ - Forward Search
? - Backward Search
n - Next Occurrence
N - Previous Occurrence

[Example]
~ less foo.txt
~ ls | less -N
   1. foo.txt
   2. bar.txt

Tuesday, September 11, 2018

Redirect output of echo to root owned files

sudo and echo command does not work together if you have to redirect output of echo to root owned files you can execute

sudo sh -c "echo 'bar' > /etc/foo.conf"

Wednesday, August 1, 2018

resume wget download

[Command]
wget -c https://foo[.]com/bar.mp4

To resume interrupted download from wget you can use -c flag which will continue from where it stopped last time.

[Options]
-c -- continue: continue getting a partially-downloaded file. This is useful when you want to finish up a download started by a previous instance of Wget, or by another program.
If there is a file named bar.mp4 in the current directory, Wget will assume that it is the first portion of the remote file, and will ask the server to continue the retrieval from an offset equal to the length of the local file.