Regular Expressions Write a regular expression that matches: all strings that en
ID: 3890654 • Letter: R
Question
Regular Expressions
Write a regular expression that matches:
all strings that end with a dot character ".", without the quotes.
all strings that begin with a "#" character, without the quotes.
all floating-point numbers using standard notation (e.g., 12.345 or –12.345). Note that matching numbers may contain any number of digits before or after the decimal point.
all floating-point numbers using scientific notation (e.g., 1.234e+5 or –1.234E–5). Again, matching numbers may contain any number of digits before or after the decimal point.
Using gawk
Consider the following file called coords.txt:
x1 y1 x2 y2
0.0 0.0 0.4 1.0
0.5 0.1 -0.25 0.9
0.75 0.4 -2.5 2.2
1.0 1.3 -1.0 4.2
Each field in this file is separated by a tab and each record is separated by a newline character. gawk is a very powerful utility; it can make calculations as well as support branching statements and function calls. Now write a one-line gawk script that computes the distance between each pair of points. Hint: gawk supports the sqrt function! Obviously, you need to skip the first line (i.e., the header) and only print out four values, the first of which is 1.07703.
Consider the following file called elems.txt:
1,H,Hydrogen
2,He,Helium
3,Li,Lithium
4,Be,Beryllium
. . .
115,Mc,Moscovium
116,Lv,Livermorium
117,Ts,Tennessine
118,Og,Oganesson
Since the shown file is incomplete, the elems.txt file is provided with this assignment. This file contains a list of the elements in the periodic table. Each field in this file is separated by a comma and each record is separated by a newline character. First, write a one-line gawk script that prints out just the record number and the element symbol (e.g., He) of all the elements in the list that contain the pattern "ium", without the quotes. Then, write another one-line gawk script that prints just the total number of lines that contain the pattern "ium”, without the quotes.
Using sed
Write a one-line sed command that will replace all multiple spaces in a file called names.txt (this is an arbitrary file that you may create yourself) by only one space.
Consider the following file called system.txt:
1 2 3 4
5 6 7 8
9 He 11 Hg
Ts H 15 Be
17 Mc Li 20
Write a one-line sed command that replaces the second number in every line with the string “system”, without the quotes. For example, after running the appropriate sed command, the following would be output to the terminal:
1 system 3 4
5 system 7 8
9 He system Hg
Ts H 15 Be
17 Mc Li system
Consider the following file called swap.txt:
349 230 481
800 456 129
043 942 584
901 627 874
Each three-digit number is separated by a space. Write a one-line sed command that performs in-place substitution to swap the first digit with the third digit of the first number in every line. For example, after running the appropriate sed command, the swap.txt file would contain:
943 230 481
008 456 129
340 942 584
109 627 874
For bonus, can you swap the first digit with the third digit of the second number in every line? Do not worry about performing in-place substitution for this part. For example, after running the appropriate sed command, the following would be output to the terminal:
349 032 481
800 654 129
043 249 584
901 726 874
Note:Test the result on a linux server and copy and paste the answers.
Explanation / Answer
Sample File
cat 3.sh
ls -lrth|awk '{print $10}'|grep "^f" > List.txt
#while read line.
#do
#if
#done < List.txt
for file in `cat List.txt`
do
mv $file $file.bac
done
1)all strings that begin with a "#" character, without the quotes.
grep [.]$ 3.sh //We are escaping the . with the backslash as it is a special character, $ signifies the end of the line.
2)all strings that begin with a "#" character, without the quotes
grep "^#" 3.sh //^ indicates beginning of the line, So it will search all the lines beginning with #
3)all floating-point numbers using standard notation (e.g., 12.345 or –12.345). Note that matching numbers may contain any number of digits before or after the decimal point.
[-+]?[0-9]*.?[0-9]+
//[-+] is used to signify that floating point can be negative or postive, ? Matches between zero and onetimes, as many times as possible, giving back as needed
4)Match numbers with exponents, you can use:
[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)?
Hi,
I apologize for not answering others, according to the Chegg Forum rules, We can answer only 4 sub part questions not more than that.
Thanks for asking the question, Please let me know if you have any queries will be glad to help