CyberKeeda In Social Media
Showing posts with label Shell Scripting. Show all posts
Showing posts with label Shell Scripting. Show all posts

Linux - Count no of files in a folder by day.

 


One Liner to count the number of files in a directory by date.

We often got this requirement where we have to deal with file counts, we have a huge list of files within a folder and we want to count the number of files created by date.

One Liner Linux CLI command

# find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c

 
Output for above command will look something like below.

1 2019-07-03 1 2019-08-08 6 2019-08-13 1 2019-08-15 1 2019-09-10 2 2019-09-11 1 2019-09-23 1 2019-10-22 1 2019-10-25 1 2019-10-29 1 2019-12-05 1 2020-03-04 2 2020-03-30 1 2020-04-07 11 2020-04-08 2 2020-04-09 1 2020-04-21 1 2020-04-26 2 2020-04-30 430 2020-05-06 1 2020-05-20 4 2020-05-26 951 2020-07-01 434 2020-07-02 1 2020-07-05 2 2020-07-06 100 2020-07-15 1 2020-07-28 6 2020-07-29 1 2020-08-01 2 2020-09-03


Let's break out the command and understand one by one highlighted in pale Yellow.

find . -type f

# find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c

Find . -type f 

Find command will fetch only the files ( -type f ) within present directory ( . )


-printf '%TY-%Tm-%Td\n'

# find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c

-printf '%TY-%Tm-%Td\n' will prints the modification time of files in e.g. 2020-04-26 format

sort | uniq -c
# find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c

sort : It will sorts the output 
uniq -c : It will count the sorted output by date.


Feel free to use it,  thank me later ;)

Read more ...

Unix/Linux Shell Scripting : How to search using AWK




AWK


Every unix/linux guy knows awk as a powerful tool, while writing a shell script, so today we will know 
  • How AWK can be used to search string from a file and return desired value as output.
  • How AWK can be used to search for a specific pattern
  • How can we use AWK to print based upon line number. 

We will start with our file, here is our file named demo.txt and it's content

cat demo.txt

FName|LName|Age|City|Country
Jill|Smith|50|NewYork|USA
Emi|Jackson|94|London|Britain
Raj|Dhinga|35|Delhi|India
Yan|Yun|21|Bejing|China
Yusuf|Khan|44|Mulheim|Germany
Will|Smith|40|NewYork|USA
Shail|Raj|21|Delhi|India
Vikram|Rajnand|34|Pune|India
HansRaj|Kedia|18|Mumbai|India
Devraj|Shukla|55|Dhanbad|India

AWK Basic Syntax :

  # awk options 'selection criteria { actions }' inputFile



How to use AWK with delimiter.

So we will start by printing the first field that is Fname and we will know, how we will separate fields when we have common delimiter.
Our file demo.txt has | as delimiter, so use the below one liner to separate fields.

  # awk -F '|' '{print $1}' demo.txt


-F followed by delimiter is used to process our requirement.

output
FName
Jill
Emi
Raj
Yan
Yusuf
Will
Shail
Vikram
HansRaj
Devraj
How to Use AWK to search and match for a string and print the entire field.

In our example file ( demo.txt), we will try to find a string "Raj" that can be part of the entire file, in case it founds the string it will print the entire line that contain the field with value "Raj"

  # awk -F "|" '/Raj/' demo.txt

-F followed by delimiter(|) and search string ( Raj ) placed within inverted comma and two backward slashes is used to process our requirement.

output
Raj|Dhinga|35|Delhi|India
Shail|Raj|21|Delhi|India
Vikram|Rajnand|34|Pune|India
HansRaj|Kedia|18|Mumbai|India
Output Analysis:

Field doesn't matter :
it's matching the word "Raj" irrespective of field,  thus printing the value of the entire line wherever it matches the search criteria.

Sub-Strings are also True matches: It doesn't matters if search string is a part of any sub-string.
Our input file has the below line that proves the statement.
Vikram|Rajnand|34|Pune|India
HansRaj|Kedia|18|Mumbai|India
Strict Matching : It strictly differentiate between upper case and lower case, our demo.txt file contains "raj" also as a part of substring but it ignores it as we have passed "Raj"
Devraj|Shukla|55|Dhanbad|India

How to Use AWK to search and match for a string and print only selected fields as an output.

So from our input file, we will match the same string "Raj" and try to print it's selected fields that is Fname($1), Age($3) and City($4)

  # awk -F "|" '/Raj/ {print $1, $3, $4}' demo.txt

-F followed by search string and  print statement with comma for space to create space between fields.

 
output
Raj 35 Delhi
Shail 21 Delhi
Vikram 34 Pune
HansRaj 18 Mumbai
How to Use AWK to search and match for a string and print only selected fields as an output with Line numbers.

So for all above search string matched lines, along with the output in order to print matched line numbers we can use NR combined with print statement, here is the one liner.

  # awk -F "|" '/Raj/ {print NR, $1, $3, $4}' demo.txt

-F followed by search string and  print statement starting with NR



output
4 Raj 35 Delhi
8 Shail 21 Delhi
9 Vikram 34 Pune
10 HansRaj 18 Mumbai

AWK Search pattern to ignore difference between upper and lower case.

From our input file, we want to add lines that contain "Raj" and "raj" both


  # awk -F "|" '/[Rr]aj/' demo.txt

-F followed by pattern [Rr] to add both the values.

output
Raj|Dhinga|35|Delhi|India
Shail|Raj|21|Delhi|India
Vikram|Rajnand|34|Pune|India
HansRaj|Kedia|18|Mumbai|India
Devraj|Shukla|55|Dhanbad|India

AWK to print by line number only.


From our input file,if we just want to print only 3rd line only, below is the one liner and it's output.

 # awk -F "|" 'NR==3'  demo.txt

-F followed by delimiter and "NR==3" where 3 is the line number,

output
Emi|Jackson|94|London|Britain
AWK to print between two line number.


From our input file,if we just want to print between line number 2 and 5, below is the one liner and it's output.

 # awk -F "|" ' NR==2, NR==5 {print NR,  $1, $3, $4}' demo.txt

-F followed by delimiter and "NR==2" a comma(,) and "NR==5"

output
2 Jill 50 NewYork
3 Emi 94 London
4 Raj 35 Delhi
5 Yan 21 Bejing


AWK to print between two line number and it's output value to a file.


From our input file,if we just want to print between line number 2 and 5, below is the one liner and it's output.

 # awk -F "|" ' NR==2, NR==5 {print NR,  $1, $3, $4}' demo.txt >> /tmp/newfile.txt




Read more ...

BASH: How to transform Vertical list into Horizontal list



This one liner will definitely help you, as i too believe this is one of the common requirement in order to solve complex requirements of various shell scripts.

While developing a script , i got a requirement to transform all vertical strings appended one after another into a single horizontal line.

Here is the example to make you understand my requirement and the end result as STDOUT.

[root@cyberkeeda.com ~]# cat myfile.txt
Hello World !
This
is 
my
blog
cyberkeeda.com
So i want it to look like  the below one

Hello World ! This is my blog cyberkeeda.com

There are multiple ways and tools to accomplish the same task, but i found  tr  as the best and short one.

[root@cyberkeeda.com ~]# cat myfile.txt |  tr '\n' ' '

I will be posting multiple ways later into the same post , to share more ways to do that.


Read more ...

BASH : Redirection STDOUT and STDERR into a file using 2>&1





How to redirects script output or error outputs
Let take three files named as  file1, file2, file3

Every file has the same content as " Hello World "
cybeerkeeda@Linux-Maniac:~ cat file1 file2 file3 
Hello World
Hello World
Hello World
Now lets assume fourt file named as file4 , which doesn't  exists 
cybeerkeeda@Linux-Maniac:~ cat file1 file2 file3 file4
Hello World
Hello World
Hello World
cat: file4: No such file or directory
Now compare with some of your shell script, you might want to log the errors or whole output into a file, in that case redirection comes into picture.

Case 1 : If you want to log every STDOUT irrespective of expected output or Errors, you can use the below parameter along with your shell script.
cybeerkeeda@Linux-Maniac:~ cat file1 file2 file3 file4   > mylog.txt  2>&1
Case 2 : If you want to log only STDERR ( Errors ), you can use the below parameter along with your shell script.

cybeerkeeda@Linux-Maniac:~ cat file1 file2 file3 file4   2> outfile

The above set of example will be used in log custom shell scripts outputs ( STDOUT / STDERR ) and one more example of it's usage in cron too.

Here is an example, usage of redirection with 2 and 1 to stop cron job sending mail for each execution. 




Read more ...

SED : How to delete N numbers of starting lines from a file using SED




Lets take an example of simple nagios host config file.

Example file name : localhost.cfg
# for the local machine
# A host for the local machine
# Monitoring template
# Define a host for the local machine

define host{
        use                     linux-server                    
        host_name               localhost
        alias                   localhost
        address                 127.0.0.1
        }
Here I want to delete starting 3 lines of my file localhost.cfg
to completely look like as
# Define a host for the local machine
define host{
        use                     linux-server                    
        host_name               localhost
        alias                   localhost
        address                 127.0.0.1
        }

Using sed to delete starting 3 lines of a file
cybeerkeeda@Linux-Maniac:~ sed -i -e "1,3d"   localhost.cfg
Change number 3 with your desired line number you want to delete


Note :

sed  -i  : Here -i    parameter will directly write the changes into the file.

I suggest to take a automatically take up backup during this comand.
cybeerkeeda@Linux-Maniac:~ sed -i.bak -e "1,3d"   localhost.cfg

 Search more SED tutorials on the go here.


Read more ...

SED: How to comment or nullify N number of starting lines of a file using SED




Lets take an example of simple nagios host config file.

Example file name : localhost.cfg
Lets take an example of simple nagios host config file.

Example file name : localhost.cfg

# Define a host for the local machine
define host{
        use                     linux-server                    
        host_name               localhost
        alias                   localhost
        address                 127.0.0.1
        contacts                nagiosadmin
        }
        
define host{
        use                     windows-server                    
        host_name               windows
        alias                   windows-srv
        address                 10.0.0.1
        contacts                nagiosadmin
        }
Here I want to comment starting 8 lines of my file to make it invalid

to nullify the string values my localhost.cfg should  look like below.
## Define a host for the local machine
#define host{
#        use                     linux-server                    
#        host_name               localhost
#        alias                   localhost
#        address                 127.0.0.1
#        contacts                nagiosadmin
#        }
        
define host{
        use                     windows-server                    
        host_name               windows
        alias                   windows-srv
        address                 10.0.0.1
        contacts                nagiosadmin
 Here we go, we will use SED to add   #  at the beginning of line for starting 8 lines

Using sed to add string on seventh line.
cybeerkeeda@Linux-Maniac:~ sed -i '1,8 s:^:#:' localhost.cfg
Change number 8 with your desired line number and # with your desired word or string


Note :

sed  -i  : Here -i    parameter will directly write the changes into the file.

I suggest to take a automatically take up backup during this comand.
cybeerkeeda@Linux-Maniac:~ sed -i.bak '1,8 s:^:#:' localhost.cfg


Read more ...

SED : How to add specific string after N number of line in a file.




Lets take an example of simple nagios host config file.

Example file name : localhost.cfg
# Define a host for the local machine
define host{
        use                     linux-server                    
        host_name               localhost
        alias                   localhost
        address                 127.0.0.1
        }
 Here I want to add a line as  "  contacts                nagiosadmin " after address
contacts                nagiosadmin
to completely look like as
# Define a host for the local machine
define host{
        use                     linux-server                    
        host_name               localhost
        alias                   localhost
        address                 127.0.0.1
        contacts                nagiosadmin
        }
 Hence I have to add contact string at seventh ( 7 )line for the same file localhost.cfg

Using sed to add string on seventh line.


cybeerkeeda@Linux-Maniac:~ sed -i '7i\ contacts   nagiosadmin' localhost.cfg
Change number 7 with your desired line number


Note :

sed  -i  : Here -i    parameter will directly write the changes into the file.

I suggest to take a automatically take up backup during this comand.
cybeerkeeda@Linux-Maniac:~ sed -i.bak '7i\ contacts   nagiosadmin' localhost.cfg
White spaces after \ are intentional , you can remove it as per your requirement.

Without white space it would look like this.
cybeerkeeda@Linux-Maniac:~ sed -i.bak '7i\contacts nagiosadmin' localhost.cfg

 Search more SED tutorials on the go here.



Read more ...

BASH : While Loop with Multiple Variables from multiple file as input



This blog post will help you to use the WHILE loop with multiple variables fetched out from multiple files.
If you have two files and some content within it which you want in parallel to work together.

Lets assume , i have a two file named as  hostname.txt and serial_no.txt and here are the contents for it.

cybeerkeeda@Linux-Maniac:~ cat hostname.txt 
Myhost01
Myhost02
Myhost03

cybeerkeeda@Linux-Maniac:~ cat serial_no.txt 
SGH120X
SGH345U
SGH6YUI


Now what I want my output should look like 

Myhost01    SGH120X
Myhost02    SGH345U
Myhost03    SHH6YUI

We can do these in numbers of way but for now we will simply demonstarate, how we can use WHILE loop to achieve it.

while read -r x && read -r y <&3; do echo " $x  $y " ; done<hostname.txt 3<serial_no.txt

We can even write it to a file too

while read -r x && read -r y <&3; do echo " $x  $y " ; done<hostname.txt 3<serial_no.txt  >> /tmp/my_inventory.txt


Depending upon your need you can modify the commands , just one more example , i have used the same loop to modify some data with SED.

while read -r x && read -r y <&3; do   echo "sed -n '/6\/$x\/2016/,/6\/$y\/2016/p'"; done <old_date.txt 3<new_date.txt > sed_date.txt






















Read more ...

Important and Useful Linux / Unix Commands




Linux / Unix Commands

Commonly used UNIX Commands!!

1.How to display the 10th line of a file?

head -10 filename | tail -1

2. How to remove the header from a file?

sed -i '1 d' filename

3. How to remove the footer from a file?

sed -i '$ d' filename

4. Write a command to find the length of a line in a file?

The below command can be used to get a line from a file.

sed –n '<n> p' filename

We will see how to find the length of 10th line in a file

sed -n '10 p' filename|wc -c

5. How to get the nth word of a line in Unix?

cut –f<n> -d' '

6. How to reverse a string in unix?

echo "java" | rev

7. How to get the last word from a line in Unix file?

echo "unix is good" | rev | cut -f1 -d' ' | rev

8. How to replace the n-th line in a file with a new line in Unix?

sed -i'' '10 d' filename       # d stands for delete

sed -i'' '10 i new inserted line' filename     # i stands for insert

9. How to check if the last command was successful in Unix?

echo $?

10. Write command to list all the links from a directory?

ls -lrt | grep "^l"

11. How will you find which operating system your system is running on in UNIX?

uname -a

12. Create a read-only file in your home directory?

touch file; chmod 400 file

13. How do you see command line history in UNIX?

The 'history' command can be used to get the list of commands that we are executed.

14. How to display the first 20 lines of a file?

By default, the head command displays the first 10 lines from a file. If we change the option of head, then we can display as many lines as we want.

head -20 filename

An alternative solution is using the sed command

sed '21,$ d' filename

The d option here deletes the lines from 21 to the end of the file

15. Write a command to print the last line of a file?

The tail command can be used to display the last lines from a file.

tail -1 filename

Alternative solutions are:

sed -n '$ p' filename

awk 'END{print $0}' filename

16. How do you rename the files in a directory with _new as suffix?

ls -lrt|grep '^-'| awk '{print "mv "$9" "$9".new"}' | sh

17. Write a command to convert a string from lower case to upper case?

echo "apple" | tr [a-z] [A-Z]

18. Write a command to convert a string to Initcap.

echo apple | awk '{print toupper(substr($1,1,1)) tolower(substr($1,2))}'

19. Write a command to redirect the output of date command to multiple files?

The tee command writes the output to multiple files and also displays the output on the terminal.

date | tee -a file1 file2 file3

20. How do you list the hidden files in current directory?

ls -a | grep '^\.'


21. List out some of the Hot Keys available in bash shell?

Ctrl+l - Clears the Screen.

Ctrl+r - Does a search in previously given commands in shell.

Ctrl+u - Clears the typing before the hotkey.

Ctrl+a - Places cursor at the beginning of the command at shell.

Ctrl+e - Places cursor at the end of the command at shell.

Ctrl+d - Kills the shell.

Ctrl+z - Places the currently running process into background.

22. How do you make an existing file empty?

cat /dev/null >  filename

23. How do you remove the first number on 10th line in file?

sed '10 s/[0-9][0-9]*//' < filename

24. What is the difference between join -v and join -a?

join -v : outputs only matched lines between two files.

join -a : In addition to the matched lines, this will output unmatched lines also.

25. How do you display from the 5th character to the end of the line from a file?

cut -c 5- filename


26. Display all the files in current directory sorted by size?

ls -l | grep '^-' | awk '{print $5,$9}' |sort -n|awk '{print $2}'


27. Write a command to search for the file 'map' in the current directory?

find -name map -type f


28. How to display the first 10 characters from each line of a file?

cut -c -10 filename


29. Write a command to remove the first number on all lines that start with "@"?

sed '\,^@, s/[0-9][0-9]*//' < filename


30. How to print the file names in a directory that has the word "term"?

grep -l term *

The '-l' option make the grep command to print only the filename without printing the content of the file. As soon as the grep command finds the pattern in a file, it prints the pattern and stops searching other lines in the file.


31. How to run awk command specified in a file?

awk -f filename


32. How do you display the calendar for the month march in the year 1985?

The cal command can be used to display the current month calendar. You can pass the month and year as arguments to display the required year, month combination calendar.

cal 03 1985

This will display the calendar for the March month and year 1985.


33. Write a command to find the total number of lines in a file?

wc -l filename

Other ways to pring the total number of lines are

awk 'BEGIN {sum=0} {sum=sum+1} END {print sum}' filename

awk 'END{print NR}' filename


34. How to duplicate empty lines in a file?

sed '/^$/ p' < filename


35. Explain iostat, vmstat and netstat?

Iostat: reports on terminal, disk and tape I/O activity.

Vmstat: reports on virtual memory statistics for processes, disk, tape and CPU activity.

Netstat: reports on the contents of network data structures.

36. How do you write the contents of 3 files into a single file?

cat file1 file2 file3 > file

37. How to display the fields in a text file in reverse order?

awk 'BEGIN {ORS=""} { for(i=NF;i>0;i--) print $i," "; print "\n"}' filename

38. Write a command to find the sum of bytes (size of file) of all files in a directory.

ls -l | grep '^-'| awk 'BEGIN {sum=0} {sum = sum + $5} END {print sum}'

39. Write a command to print the lines which end with the word "end"?

grep 'end$' filename

The '$' symbol specifies the grep command to search for the pattern at the end of the line.

40. Write a command to select only those lines containing "july" as a whole word?

grep -w july filename

The '-w' option makes the grep command to search for exact whole words. If the specified pattern is found in a string, then it is not considered as a whole word. For example: In the string "mikejulymak", the pattern "july" is found. However "july" is not a whole word in that string.

41. How to remove the first 10 lines from a file?

sed '1,10 d' < filename

42. Write a command to duplicate each line in a file?

sed 'p' < filename

43. How to extract the username from 'who am i' comamnd?

who am i | cut -f1 -d' '

44. Write a command to list the files in '/usr' directory that start with 'ch' and then display the number of lines in each file?

wc -l /usr/ch*

Another way is

find /usr -name 'ch*' -type f -exec wc -l {} \;

45. How to remove blank lines in a file ?

grep -v ‘^$’ filename > new_filename


46. How to display the processes that were run by your user name ?

ps -aef | grep <user_name>

47. Write a command to display all the files recursively with path under current directory?

find . -depth -print

48. Display zero byte size files in the current directory?

find -size 0 -type f

49. Write a command to display the third and fifth character from each line of a file?

cut -c 3,5 filename

50. Write a command to print the fields from 10th to the end of the line. The fields in the line are delimited by a comma?

cut -d',' -f10- filename

51. How to replace the word "Gun" with "Pen" in the first 100 lines of a file?

sed '1,00 s/Gun/Pen/' < filename

52. Write a Unix command to display the lines in a file that do not contain the word "RAM"?

grep -v RAM filename

The '-v' option tells the grep to print the lines that do not contain the specified pattern.

53. How to print the squares of numbers from 1 to 10 using awk command

awk 'BEGIN { for(i=1;i<=10;i++) {print "square of",i,"is",i*i;}}'

54. Write a command to display the files in the directory by file size?

ls -l | grep '^-' |sort -nr -k 5

55. How to find out the usage of the CPU by the processes?

The top utility can be used to display the CPU usage by the processes.

56. Write a command to remove the prefix of the string ending with '/'.

The basename utility deletes any prefix ending in /. The usage is mentioned below:

basename /usr/local/bin/file

This will display only file

57. How to display zero byte size files?

ls -l | grep '^-' | awk '/^-/ {if ($5 !=0 ) print $9 }'

58. How to replace the second occurrence of the word "bat" with "ball" in a file?

sed 's/bat/ball/2' < filename

59. How to remove all the occurrences of the word "jhon" except the first one in a line with in the entire file?

sed 's/jhon//2g' < filename

60. How to replace the word "lite" with "light" from 100th line to last line in a file?

sed '100,$ s/lite/light/' < filename

61. How to list the files that are accessed 5 days ago in the current directory?

find -atime 5 -type f

62. How to list the files that were modified 5 days ago in the current directory?

find -mtime 5 -type f

63. How to list the files whose status is changed 5 days ago in the current directory?

find -ctime 5 -type f

64. How to replace the character '/' with ',' in a file?

sed 's/\//,/' < filename

sed 's|/|,|' < filename

65. Write a command to find the number of files in a directory.

ls -l|grep '^-'|wc -l

66. Write a command to display your name 100 times.

The Yes utility can be used to repeatedly output a line with the specified string or 'y'.

yes <your_name> | head -100

67. Write a command to display the first 10 characters from each line of a file?

cut -c -10 filename

68. The fields in each line are delimited by comma. Write a command to display third field from each line of a file?

cut -d',' -f2 filename

69. Write a command to print the fields from 10 to 20 from each line of a file?

cut -d',' -f10-20 filename

70. Write a command to print the first 5 fields from each line?

cut -d',' -f-5 filename

71. By default the cut command displays the entire line if there is no delimiter in it. Which cut option is used to supress these kind of lines?

The -s option is used to supress the lines that do not contain the delimiter.

72. Write a command to replace the word "bad" with "good" in file?

sed s/bad/good/ < filename

73. Write a command to replace the word "bad" with "good" globally in a file?

sed s/bad/good/g < filename

74. Write a command to replace the word "apple" with "(apple)" in a file?

sed s/apple/(&)/ < filename

75. Write a command to switch the two consecutive words "apple" and "mango" in a file?

sed 's/\(apple\) \(mango\)/\2 \1/' < filename

76. Write a command to display the characters from 10 to 20 from each line of a file?

cut -c 10-20 filename


77. Write a command to print the lines that has the the pattern "july" in all the files in a particular directory?

grep july *

This will print all the lines in all files that contain the word “july” along with the file name. If any of the files contain words like "JULY" or "July", the above command would not print those lines.

78. Write a command to print the lines that has the word "july" in all the files in a directory and also suppress the filename in the output.

grep -h july *

79. Write a command to print the lines that has the word "july" while ignoring the case.

grep -i july *

The option i make the grep command to treat the pattern as case insensitive.

80. When you use a single file as input to the grep command to search for a pattern, it won't print the filename in the output. Now write a grep command to print the filename in the output without using the '-H' option.

grep pattern filename /dev/null

The /dev/null or null device is special file that discards the data written to it. So, the /dev/null is always an empty file.

Another way to print the filename is using the '-H' option. The grep command for this is

grep -H pattern filename

81. Write a command to print the file names in a directory that does not contain the word "july"?

grep -L july *

The '-L' option makes the grep command to print the filenames that do not contain the specified pattern.

82. Write a command to print the line numbers along with the line that has the word "july"?

grep -n july filename

The '-n' option is used to print the line numbers in a file. The line numbers start from 1

83. Write a command to print the lines that starts with the word "start"?

grep '^start' filename

The '^' symbol specifies the grep command to search for the pattern at the start of the line.

84. In the text file, some lines are delimited by colon and some are delimited by space. Write a command to print the third field of each line.

awk '{ if( $0 ~ /:/ ) { FS=":"; } else { FS =" "; } print $3 }' filename

85. Write a command to print the line number before each line?

awk '{print NR, $0}' filename

86. Write a command to print the second and third line of a file without using NR.

awk 'BEGIN {RS="";FS="\n"} {print $2,$3}' filename

87. How to create an alias for the complex command and remove the alias?

The alias utility is used to create the alias for a command. The below command creates alias for ps -aef command.

alias pg='ps -aef'

If you use pg, it will work the same way as ps -aef.

To remove the alias simply use the unalias command as

unalias pg

88. Write a command to display todays date in the format of 'yyyy-mm-dd'?

The date command can be used to display todays date with time

date '+%Y-%m-%d'


89.For LOOP

1. Rename all ".old" files in the current directory to ".bak":

for i in *.old   do  j=`echo $i|sed 's/old/bak/'`  mv $i $j  done


2. Change all instances of "yes" to "no" in all ".txt" files in the current directory. Back up the original files to ".bak".

for i in *.txt do  j=`echo $i|sed 's/txt/bak/'`  mv $i $j   sed 's/yes/no/' $j > $i  done

3. Loop thru a text file containing possible file names. If the file is readable, print the first line, otherwise print an error message:

for i in `cat file_list.txt` do  if test -r $i

  then      

   echo "Here is the first line of file: $i"    

   sed 1q $i

else

echo "file $i cannot be open for reading."      fi  done


How to print/display the first line of a file?

$> head -1 file.txt

$> sed '2,$ d' file.txt

How to print/display the last line of a file?

$> tail -1 file.txt

$> sed -n '$ p' test

How to display n-th line of a file?

$> sed –n '<n> p' file.txt

$> sed –n '4 p' test

$> head -<n> file.txt | tail -1

$> head -4 file.txt | tail -1

How to remove the first line / header from a file?

$> sed '1 d' file.txt

$> sed '1 d' file.txt > new_file.txt

$> mv new_file.txt file.txt

$> sed –i '1 d' file.txt

How to remove the last line/ trailer from a file in Unix script?

$> sed –i '$ d' file.txt

How to remove certain lines from a file in Unix?

$> sed –i '5,7 d' file.txt

How to remove the last n-th line from a file?

$> sed –i '96,100 d' file.txt   # alternative to command [head -95 file.txt]

$> tt=`wc -l file.txt | cut -f1 -d' '`;sed –i "`expr $tt - 4`,$tt d" test


How to check the length of any line in a file?

$> sed –n '<n> p' file.txt

$> sed –n '35 p' file.txt | wc –c

How to check if a file is present in a particular directory in Unix?

$> ls –l file.txt; echo $?

How to check all the running processes in Unix?

$> ps –ef

$> ps aux

$>ps -e -o stime,user,pid,args,%mem,%cpu


Combine multiple Rows to a Column – Oracle

SELECT SUBSTR (SYS_CONNECT_BY_PATH (NAME , ','), 2) FRUITS_LIST

FROM (SELECT NAME , ROW_NUMBER () OVER (ORDER BY NAME ) RN,

COUNT (*) OVER () CNT

FROM FRUITS)

WHERE RN = CNT

START WITH RN = 1

CONNECT BY RN = PRIOR RN + 1;

What is command to check space in Unix

df -k


What is command to kill last background Job

kill $!

What is difference between diff and cmp command

cmp -It compares two files byte by byte and displays first mismatch.

diff -It displays all changes required to make files identical.


What does $# stands for


It will return the number of parameters passed as command line argument.
Read more ...
Designed By Jackuna