Skip to content

Commit 2b84ac0

Browse files
authored
feat: add bash solutions to lc problems: No.0193,0194,0195 (#1216)
* No.0193.Valid Phone Numbers * No.0194.Transpose File * No.0195.Tenth Line
1 parent 5598ce5 commit 2b84ac0

File tree

9 files changed

+94
-37
lines changed

9 files changed

+94
-37
lines changed

solution/0100-0199/0193.Valid Phone Numbers/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,18 @@
4242
<!-- 这里可写当前语言的特殊实现逻辑 -->
4343

4444
```sh
45+
# Read from the file file.txt and output all valid phone numbers to stdout.
46+
grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt
47+
```
4548

49+
```sh
50+
# Read from the file file.txt and output all valid phone numbers to stdout.
51+
sed -n -E '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/p' file.txt
52+
```
53+
54+
```sh
55+
# Read from the file file.txt and output all valid phone numbers to stdout.
56+
awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt
4657
```
4758

4859
<!-- tabs:end -->

solution/0100-0199/0193.Valid Phone Numbers/README_EN.md

+11
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,18 @@
3434
### **Bash**
3535

3636
```sh
37+
# Read from the file file.txt and output all valid phone numbers to stdout.
38+
grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt
39+
```
3740

41+
```sh
42+
# Read from the file file.txt and output all valid phone numbers to stdout.
43+
sed -n -E '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/p' file.txt
44+
```
45+
46+
```sh
47+
# Read from the file file.txt and output all valid phone numbers to stdout.
48+
awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt
3849
```
3950

4051
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
#!/usr/bin/env bash
2-
3-
# -P : --perl-regexp
4-
grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt
5-
6-
# or use sed
7-
sed -n -E '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/p' file.txt
8-
9-
# or use awk
1+
# Read from the file file.txt and output all valid phone numbers to stdout.
102
awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt

solution/0100-0199/0194.Transpose File/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,22 @@ age 21 30
4040
<!-- 这里可写当前语言的特殊实现逻辑 -->
4141

4242
```sh
43-
43+
# Read from the file file.txt and print its transposed content to stdout.
44+
awk '
45+
{
46+
for (i=1; i<=NF; i++) {
47+
if(NR == 1) {
48+
res[i] = re$i
49+
} else {
50+
res[i] = res[i]" "$i
51+
}
52+
}
53+
}END {
54+
for (i=1;i<=NF;i++) {
55+
print res[i]
56+
}
57+
}
58+
' file.txt
4459
```
4560

4661
<!-- tabs:end -->

solution/0100-0199/0194.Transpose File/README_EN.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,22 @@ age 21 30
3232
### **Bash**
3333

3434
```sh
35-
35+
# Read from the file file.txt and print its transposed content to stdout.
36+
awk '
37+
{
38+
for (i=1; i<=NF; i++) {
39+
if(NR == 1) {
40+
res[i] = re$i
41+
} else {
42+
res[i] = res[i]" "$i
43+
}
44+
}
45+
}END {
46+
for (i=1;i<=NF;i++) {
47+
print res[i]
48+
}
49+
}
50+
' file.txt
3651
```
3752

3853
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
#!/usr/bin/env bash
2-
3-
awk '
4-
{
5-
for (i=1; i<=NF; i++) {
6-
if(NR == 1) {
7-
res[i] = re$i
8-
} else {
9-
res[i] = res[i]" "$i
10-
}
11-
}
12-
}END {
13-
for (i=1;i<=NF;i++) {
14-
print res[i]
15-
}
16-
}
1+
# Read from the file file.txt and print its transposed content to stdout.
2+
awk '
3+
{
4+
for (i=1; i<=NF; i++) {
5+
if(NR == 1) {
6+
res[i] = re$i
7+
} else {
8+
res[i] = res[i]" "$i
9+
}
10+
}
11+
}END {
12+
for (i=1;i<=NF;i++) {
13+
print res[i]
14+
}
15+
}
1716
' file.txt

solution/0100-0199/0195.Tenth Line/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,18 @@ Line 10
4444
<!-- 这里可写当前语言的特殊实现逻辑 -->
4545

4646
```sh
47+
# Read from the file file.txt and output the tenth line to stdout.
48+
tail -n +10 file.txt | head -1
49+
```
4750

51+
```sh
52+
# Read from the file file.txt and output the tenth line to stdout.
53+
awk 'NR == 10' file.txt
54+
```
55+
56+
```sh
57+
# Read from the file file.txt and output the tenth line to stdout.
58+
sed -n 10p file.txt
4859
```
4960

5061
<!-- tabs:end -->

solution/0100-0199/0195.Tenth Line/README_EN.md

+11
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@ Line 10
4040
### **Bash**
4141

4242
```sh
43+
# Read from the file file.txt and output the tenth line to stdout.
44+
tail -n +10 file.txt | head -1
45+
```
4346

47+
```sh
48+
# Read from the file file.txt and output the tenth line to stdout.
49+
awk 'NR == 10' file.txt
50+
```
51+
52+
```sh
53+
# Read from the file file.txt and output the tenth line to stdout.
54+
sed -n 10p file.txt
4455
```
4556

4657
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
#!/usr/bin/env bash
2-
3-
# tail -n: use -n +K to output starting with the Kth
4-
tail -n +10 file.txt | head -1
5-
6-
# awk:
7-
awk 'NR == 10' file.txt
8-
9-
# sed:
10-
sed -n 10p file.txt
1+
# Read from the file file.txt and output the tenth line to stdout.
2+
sed -n 10p file.txt

0 commit comments

Comments
 (0)