Shell grammar exercises

table of Contents

1. Count the number of lines in the file

2. Print the last 5 lines of the file

3. Output multiples of 7

4. Output the content of line 5

5. Print the line number of the blank line

6. Remove blank lines

7. Print words with less than 8 letters

8. Count the sum of memory occupied by all processes

9. Count the number of occurrences of each word

10. Is there any duplication in the second column?

11. The contents of the transposed file

12. Print the number of numbers that appear in each line

13. Remove all sentences containing this

14. Find the average

15. Remove unnecessary words


1. Count the number of lines in the file

cat nowcoder.txt | wc -l
或者
awk 'END{print NR}' nowcoder.txt

2. Print the last 5 lines of the file

tail -5 nowcoder.txt

 

3. Output multiples of 7

Write a bash script to output commands that are multiples of 7 (0 7 14 21...) from 0 to 500

seq is used to generate all integers from one number to another.

Usage: seq [option]... mantissa
or: seq [option]... first digit and mantissa
or: seq [option]... first digit and increment mantissa

seq 0 7 500
或者
seq 0 500|awk '$0%7==0 {print $0}' 

 

4. Output the content of line 5

head -n 5 nowcoder.txt | tail -n 1
或者
awk '{if(NR==5){print $0}}' nowcoder.txt

5. Print the line number of the blank line

awk '{if($0==""){print NR}}' nowcoder.txt

 

6. Remove blank lines

awk '{if($0 !=""){print $0}}' nowcoder.txt

7. Print words with less than 8 letters

awk '{for(i=0;i<NF;i++){if(length($i)<8){print $i}}}' nowcoder.txt 

8. Count the sum of memory occupied by all processes

Suppose the content of nowcoder.txt is as follows:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0? S September 25 0:00 [kthreadd]
root 4 0.0 0.0 0 0? I< September 25 0 :00 [kworker/0:0H]
web 1638 1.8 1.8 6311352 612400? Sl October 16 21:52 test
web 1639 2.0 1.8 6311352 612401? Sl October 16 21:52 test
tangmiao-pc 5336 0.0 1.4 9100240 238544 ?? S 3:09 PM 0:31.70 /Applications The

above content is output to the nowcoder.txt file through the ps aux | grep -v'RSS TTY' command.
Please write a script to calculate the sum of the memory occupied by all processes:

a Display all programs under the current terminal, including programs of other users.
u Display the program status in a user-oriented format.
x Display all programs, not distinguished by terminal.

The above explanations:

The owner of the USER process;

ID of the PID process;

PPID parent process;

%CPU The percentage of CPU occupied by the process;

The percentage of memory occupied by %MEM;

The NICE value of the NI process, the value is large, which means that less CPU time is used;

The amount of virtual memory used by the VSZ process (KB);

RSS The fixed amount of memory (KB) occupied by the process (the number of resident pages);

TTY The process is running on that terminal (the terminal location of the loginer). If it has nothing to do with the terminal, it will display (?).
If it is pts/0, etc., it means that the host process is connected by the network

Whether the current process of WCHAN is in progress, if it is-means in progress;

START The time the process was triggered to start;

TIME The time the process actually uses the CPU to run;

The name and parameters of the COMMAND command;

Common status characters in the STAT status bit

D Sleep state that cannot be interrupted (usually IO process);

R is running and can be passed in the queue;

S is in a dormant state;

T stop or be tracked;

W enters memory exchange (it is invalid since kernel 2.6);

X dead process (basically rare);

Z zombie process;

<High priority process

N lower priority process

L Some pages are locked into memory;

s the leader of the process (there are child processes under it);

l Multi-process (using CLONE_THREAD, similar to NPTL pthreads);

+ Process group in the background;

cat nowcoder.txt | awk '{sum+=$6}END{print sum}'

 

9. Count the number of occurrences of each word

nowcoder.txt only includes lowercase letters and spaces, each word consists of lowercase letters only, and the words are separated by one or more space characters.
tr is used to convert characters from standard input by replacing or deleting. tr is mainly used to delete control characters in a file or perform character conversion.

tr -c -d -s [“string1_to_translate_from”] [“string2_to_translate_to”] < input-file

  • -c Replace this character set with the complement of the character set in string 1, requiring the character set to be ASCII.
  • -d Delete all input characters in string 1.
  • -s Delete all repeated character sequences, only keep the first one; about to compress the repeated character string into one string.
  • input-file is the name of the converted file. Although other formats can be used for input, this format is most commonly used.
cat nowcoder.txt | tr -s ' ' '\n' |sort |uniq -c|sort |awk '{print $2" "$1}'

 

10. Is there any duplication in the second column?

Given a nowcoder.txt file with 3 columns of information, as in the following example, write a sheel script to check whether the second column of the file is duplicated, and there are several duplicates, and extract the second column information of the duplicate rows:
Example :
20201001 python 99
20201002 go 80
20201002 c++ 88
20201003 php 77
20201001 go 88
20201005 shell 89
20201006 java 70
20201008 c 100
20201007 java 88
20201006 go 97

Results:
2 java
3 go

awk '{print $2}' nowcoder.txt | sort | uniq -cd | sort -n

11. The contents of the transposed file

For the sake of simplicity, you can assume:
You can assume that the number of rows and columns is the same, and each field is separated by a space.

Example:
Assume the content of nowcoder.txt is as follows:
job salary
c++ 13
java 14
php 12

Your script should output (in ascending order of word frequency) ):
job c++ java php
salary 13 14 12

awk '{
   for (i = 1; i <= NF; i++) {
       if (NR == 1) {
           array[i] = $i
       } else {
           array[i] = array[i] $i
       }
   }
} END {
      for (j = 1; j <= NF; j++) {
          print array[j]
      }
}' nowcoder.txt

 

12. Print the number of numbers that appear in each line

Write a bash script to count the number of 1,2,3,4,5 numbers that appear in each line of a text file nowcoder.txt and calculate how many 1, 2, 3, 4, 5 appear in the entire document The total number of digits.
Example:
Assume the content of nowcoder.txt is as follows:
a12b8
10ccc
2521abc
9asf
Your script should output:
line1 number: 2
line2 number: 1
line3 number: 4
line4 number: 0
sum is 7

Note:
Don’t worry about the spaces and line breaks you output.

awk '{
   count = 0;
   len = length($0);
   for (i = 1; i <= len; i++) {
      s = substr($0, i,1);
      if (0 < s && s < 6) {
         total++;
         count++;
      }
   }
   printf("line%d number:%d\n", NR, count);
} END {
   printf("sum is %d\n", total);
}' nowcoder.txt

13. Remove all sentences containing this

Write a bash script to achieve a requirement, remove the sentence that contains this in the input, and output the sentence that does not contain this
Example:
Suppose the input is as follows:
this is your bag
is this your bag?
to the degree or extent indicated.
there was a court case resulting from this incident
welcome to nowcoder


Your script should output the above input:
to the degree or extent indicated.
welcome to nowcoder

Note:
You don’t need to care about the output format, including spaces and line breaks

grep -v 'this' nowcoder.txt

 

14. Find the average

Write a bash script to achieve a requirement. Find the average value of an input array. The

first line is the length of the input array
. The second to N lines are the elements of the array. For example, the following is: the
array length is 4, and the array elements are 1 2 9 8
Example:
4
1
2
9
8

Then the average value is: 5.000 (3 digits after the decimal point),
your script should output the above input:
5.000

awk '{
   if (NR == 1) {
      rows = $0 
   } else {
      sum = sum + $0 
   }
} END {
   printf("%.3f", sum/rows);
}' nowcoder.txt

 

15. Remove unnecessary words

Write a bash script to achieve a requirement, remove the words containing B and b in the input
Example:
Suppose the input is as follows:
big
nowcoder
Betty
basic
test

Your script should output the above input:
nowcoder
test
 

grep -v -i 'b' nowcoder.txt

 

 

Guess you like

Origin blog.csdn.net/xiao__jia__jia/article/details/113763559