Thursday, July 4, 2019

How to loop through the content of a file in Bash [SOLVED]

I saw that many people face the problem to loop through the file content in Bash.
I have some solutions for this. There are many ways through you can achieve this.
You can choose the one which suits you.

Option No.1
while IFS="" read -r p || [ -n "$p" ]
do
  printf '%s\n' "$p"
done < peptides.txt
If your file has leading whitespace, interpretting backslash sequences, and you want to skip the trailing line then above solution is for you.


Option No. 2
#!/bin/bash
filename='peptides.txt'
echo Start
while read p; do 
    echo $p
done < $filename
above option is a while loop option. Single line at a time. Input redirection.


Option No. 3
#!/bin/bash
filename='peptides.txt'
exec 4<$filename
echo Start
while read -u4 p ; do
    echo $p
done
Above one is also in while loop: Single line at a time:
Open the file, read from a file descriptor (in this case file descriptor #4).


Option No. 4 
while IFS= read -r line; do
   echo "$line"
done <file
Above option is with simple while loop. You need to set the IFS properly, otherwise you will loose the indentation. Also always use the -r option with read. NEVER EVER use the for loop for reading lines. 


I hope these options are helpful for you. ðŸ˜ƒ

Let me know your feedback!

Thanks for reading!
Amit




Tags:
Read the content of file through bash, loop through content of file bash, bash read file



Thursday, April 25, 2019

How to symlink a file in Linux? [SOLVED]

How to create symlink for a file/directory ?

Today I will show you how to create a symlink to a file or folder.
In linux, a symbolic link, also known as a symlink or a soft link, is a special kind of file (entry) that points to the actual file or directory on a disk (like a shortcut in Windows)

So lets create one: 

in order to create a new symlink try below


ln -s /path/to/file /path/to/symlink


To create or update a symlink:
ln -sf /path/to/file /path/to/symlink


How to list all symlink in a directory ?
Now you have created a symlink. To see present symlink for current directory you can try below command:

ls -la /var/www/ | grep "\->"

This will list all the links present in the current directory.

Let me know if this resolves your problem 😃



Tags:

Friday, April 19, 2019

Search in all files for specific string on Linux [SOLVED]

If you want to search a word in all of your files on your linux you need to run grep command. This command has some parameters. All explained below:

grep -rnw '/path/to/somewhere/' -e 'pattern'
  • -r or -R is recursive,
  • -n is line number, and
  • -w stands for match the whole word.
  • -l (lower-case L) can be added to just give the file name of matching files.
Along with these, --exclude--include--exclude-dir flags could be used for efficient searching:
  • This will only search through those files which have .c or .h extensions:
    grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
    
  • This will exclude searching all the files ending with .o extension:
    grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"
    
  • For directories it's possible to exclude a particular directory(ies) through --exclude-dirparameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
    grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
    
This works very well for me, to achieve almost the same purpose like yours.
For more options check man grep

There appears to be trouble with your network connection. Retrying... [SOLVED]

So recently I was facing an issue while adding Yarn package to my application. Every time I was doing Yarn add it was showing me error like There appears to be trouble with your network connection. So problem is, this happens when your network is too slow or the package being installed is too large, and yarn just assumes it's a network problem. Lets try increasing Yarn network timeout!

To resolve this problem all you need to do is:

yarn add <yourPackage> --network-timeout 100000


Now if problem still exists then you need to add proxy.
To add proxy just do below.

$ yarn config set proxy http://your_company_proxy_url:port
$ yarn config set https-proxy http://localhost:3128

example $ yarn config set https-proxy http://proxy.example.com:8080

Add a comment below if problem still exists :) Will be happy to help you!