Monday, 12 August 2019

Bash: Use for loop to iterate over file without using cat

Introduction:

In this very brief post I just wanted to demonstrate a way to use the for loop to iterate over a file without using the cat command.

Here is a file containing 10 numbers, 1 number on each line.

cat seq.txt
1
2
3
4
5
6
7
8
9
10

If I wanted to loop over it using for loop in bash I could type:

for i in `cat seq.txt`; do echo $i ;done

But what if I don't want to read in the file using cat command?
I could use input redirection to read in the file contents as shown in the below example:

for i in $(<seq.txt); do echo $i ; done
1
2
3
4
5
6
7
8
9
10
                            
One more way of avoiding the cat command would be to read the file into an array and then loop over the array as shown below.

readarray -t ARR < seq.txt

for i in "${ARR[@]}"; do echo $i; done
1
2
3
4
5
6
7
8
9
10


Conclusion

There is nothing wrong with using the cat command to read in file contents while iterating over it in a for loop. I just found redirecting directly from STDIN to be more elegant and I would try to use it more often.

Friday, 2 August 2019

Git cheat sheet

Introduction

This post contains a git cheat sheet that I've created for my use. The content is very succinct. I'll be writing more elaborate posts on installing and using git in the future. Also, this will be an ongoing post and will be periodically updated.


Here's the cheat sheet:


Initialize repository:

[sahil@lab-node:~/rep] $ git init .

Check for anything for ready to be staged or committed:
[sahil@lab-node:~/rep] $ git status



Staging changes:


[sahil@lab-node:~/rep] $ git add 1.bash
[sahil@lab-node:~/rep] $ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   1.bash
#


Commit the changes to the repo:


[sahil@lab-node:~/rep] $ git commit -m "version 1"
[master (root-commit) 252f733] version 1
 1 file changed, 10 insertions(+)
 create mode 100644 1.bash


View log of commits:


[sahil@lab-node:~/rep] $ git log
commit 252f733ded22f7634725961bfd95ecdc0826a69c
Author: Sahil Suri <sahil.suri@emerson.com>
Date:   Fri Aug 2 05:30:02 2019 +0000

    version 1


View one line logs:

[sahil@lab-node:~/rep] $ git log --oneline
3ee84d7 version 2
252f733 version 1


View the difference between commits:

[sahil@lab-node:~/rep] $ git log --graph -p
* commit 3ee84d747f009d7cbbe6f12b3eb71bd39da14220
| Author: Sahil Suri <sahil.suri@emerson.com>
| Date:   Fri Aug 2 05:32:29 2019 +0000
|
|     version 2
|
| diff --git a/1.bash b/1.bash
| index e193461..2ab8d4e 100644
| --- a/1.bash
| +++ b/1.bash
| @@ -7,4 +7,4 @@
|  #version:
|  ##############################################################
|
| -echo "This is testing version 1"
| +echo "This is testing version 2"
|
* commit 252f733ded22f7634725961bfd95ecdc0826a69c
  Author: Sahil Suri <sahil.suri@emerson.com>
  Date:   Fri Aug 2 05:30:02 2019 +0000

      version 1

  diff --git a/1.bash b/1.bash
  new file mode 100644
  index 0000000..e193461
  --- /dev/null
  +++ b/1.bash
  @@ -0,0 +1,10 @@
  +#!/bin/bash
  +
  +##############################################################
  +#Author: Sahil Suri
  +#Date:
  +#Purpose:
  +#version:
  +##############################################################
  +
  +echo "This is testing version 1"


View differences b/w current version and a particular version:


#git diff <commit hash>

[sahil@lab-node:~/rep] $ git log --oneline
c9ef444 this is version 3'
3ee84d7 version 2
252f733 version 1

[sahil@lab-node:~/rep] $ git diff 3ee84d7
diff --git a/1.bash b/1.bash
index 2ab8d4e..c2b325e 100644
--- a/1.bash
+++ b/1.bash
@@ -7,4 +7,4 @@
 #version:
 ##############################################################

-echo "This is testing version 2"
+echo "This is testing version 3"

[sahil@lab-node:~/rep] $ git diff 252f733
diff --git a/1.bash b/1.bash
index e193461..c2b325e 100644
--- a/1.bash
+++ b/1.bash
@@ -7,4 +7,4 @@
 #version:
 ##############################################################

-echo "This is testing version 1"
+echo "This is testing version 3"
[sahil@lab-node:~/rep] $


Retrieving previous versions of files:

This has a couple of steps involved:

Retrieve version of 1.bash two commits before:

[sahil@lab-node:~/rep] $ git checkout HEAD~2 1.bash

 To save this as the current version of a file, use git commit.

To go back to the updated version follow the below steps:

[sahil@lab-node:~/rep] $ git reset HEAD 1.bash
[sahil@lab-node:~/rep] $ git checkout  1.bash


Add a remote to git repository:

git remote add origin https://github.com/sahilsuri008/creating_shell_scripts_linux


To push updates to GitHub:

git push origin master


Conclusion

That's it for this post for now. Thank you for taking the time to read it.

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...