Posts

Showing posts from 2022

command line / bash variable in awk command

 This is how we use a command line / bash variable (named num) in a awk command. We define an awk  variable 'var' with -v option. This also shows use of BEGIN block. awk -F, -v var=$num 'BEGIN{srand(var)} {print $1","$2","rand()}' file_name 

Change nameserver entries in resolve.conf

 We can directly change the contents of the file but they dont seem to be in effect. So I added Google's nameserver in the file as follows :  $ sudo cat /etc/resolvconf/resolv.conf.d/base  nameserver 8.8.8.8 nameserver 8.8.4.4 And then updated the file as follows :  $ sudo resolvconf -u   we can also just update /etc/resolve.conf and it will be in effect immediately. $ sudo sed -i '1 i\nameserver 8.8.8.8\nnameserver 8.8.4.4' /etc/resolv.conf  To remove the changes : $ sudo sed -i '1,2d' /etc/resolv.conf 

renaming all the files in a folder

 IF we want to rename all the files with a part of the file we can do something following: for file in $(ls); do nn=$(echo $file | awk -F'_' '{print($2)}'); mv $file $nn; done

shell loop through dates

 Easy command to loop through the dates :  for i in 2022-01-{01..31} 2022-02-{01..15}; do echo $i; done More comprehensive command : d=2015-01-01 while [ " $d " != 2015-02-20 ]; do echo $d d=$(date -I -d " $d + 1 day" ) done