From 9db0806744dc15eeb87ef0a3abe3fa7e583c11d7 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 15 Jul 2023 16:28:59 +0800 Subject: [PATCH] feat: add bash solutions to lc problems: No.0193,0194,0195 * No.0193.Valid Phone Numbers * No.0194.Transpose File * No.0195.Tenth Line --- .../0193.Valid Phone Numbers/README.md | 11 +++++++ .../0193.Valid Phone Numbers/README_EN.md | 11 +++++++ .../0193.Valid Phone Numbers/Solution.sh | 10 +----- .../0100-0199/0194.Transpose File/README.md | 17 +++++++++- .../0194.Transpose File/README_EN.md | 17 +++++++++- .../0100-0199/0194.Transpose File/Solution.sh | 31 +++++++++---------- solution/0100-0199/0195.Tenth Line/README.md | 11 +++++++ .../0100-0199/0195.Tenth Line/README_EN.md | 11 +++++++ .../0100-0199/0195.Tenth Line/Solution.sh | 12 ++----- 9 files changed, 94 insertions(+), 37 deletions(-) diff --git a/solution/0100-0199/0193.Valid Phone Numbers/README.md b/solution/0100-0199/0193.Valid Phone Numbers/README.md index 8f97e417acecf..7671b3f0e53c0 100644 --- a/solution/0100-0199/0193.Valid Phone Numbers/README.md +++ b/solution/0100-0199/0193.Valid Phone Numbers/README.md @@ -42,7 +42,18 @@ ```sh +# Read from the file file.txt and output all valid phone numbers to stdout. +grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt +``` +```sh +# Read from the file file.txt and output all valid phone numbers to stdout. +sed -n -E '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/p' file.txt +``` + +```sh +# Read from the file file.txt and output all valid phone numbers to stdout. +awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt ``` diff --git a/solution/0100-0199/0193.Valid Phone Numbers/README_EN.md b/solution/0100-0199/0193.Valid Phone Numbers/README_EN.md index e31ff775b9710..ec9fdfa464156 100644 --- a/solution/0100-0199/0193.Valid Phone Numbers/README_EN.md +++ b/solution/0100-0199/0193.Valid Phone Numbers/README_EN.md @@ -34,7 +34,18 @@ ### **Bash** ```sh +# Read from the file file.txt and output all valid phone numbers to stdout. +grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt +``` +```sh +# Read from the file file.txt and output all valid phone numbers to stdout. +sed -n -E '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/p' file.txt +``` + +```sh +# Read from the file file.txt and output all valid phone numbers to stdout. +awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt ``` diff --git a/solution/0100-0199/0193.Valid Phone Numbers/Solution.sh b/solution/0100-0199/0193.Valid Phone Numbers/Solution.sh index 72ca105be09e0..6ab8e15177ecc 100644 --- a/solution/0100-0199/0193.Valid Phone Numbers/Solution.sh +++ b/solution/0100-0199/0193.Valid Phone Numbers/Solution.sh @@ -1,10 +1,2 @@ -#!/usr/bin/env bash - -# -P : --perl-regexp -grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt - -# or use sed -sed -n -E '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/p' file.txt - -# or use awk +# Read from the file file.txt and output all valid phone numbers to stdout. awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt \ No newline at end of file diff --git a/solution/0100-0199/0194.Transpose File/README.md b/solution/0100-0199/0194.Transpose File/README.md index cc305c1066b01..23e450f02fe5a 100644 --- a/solution/0100-0199/0194.Transpose File/README.md +++ b/solution/0100-0199/0194.Transpose File/README.md @@ -40,7 +40,22 @@ age 21 30 ```sh - +# Read from the file file.txt and print its transposed content to stdout. +awk ' +{ + for (i=1; i<=NF; i++) { + if(NR == 1) { + res[i] = re$i + } else { + res[i] = res[i]" "$i + } + } +}END { + for (i=1;i<=NF;i++) { + print res[i] + } +} +' file.txt ``` diff --git a/solution/0100-0199/0194.Transpose File/README_EN.md b/solution/0100-0199/0194.Transpose File/README_EN.md index c090d0da95962..c907e2e942438 100644 --- a/solution/0100-0199/0194.Transpose File/README_EN.md +++ b/solution/0100-0199/0194.Transpose File/README_EN.md @@ -32,7 +32,22 @@ age 21 30 ### **Bash** ```sh - +# Read from the file file.txt and print its transposed content to stdout. +awk ' +{ + for (i=1; i<=NF; i++) { + if(NR == 1) { + res[i] = re$i + } else { + res[i] = res[i]" "$i + } + } +}END { + for (i=1;i<=NF;i++) { + print res[i] + } +} +' file.txt ``` diff --git a/solution/0100-0199/0194.Transpose File/Solution.sh b/solution/0100-0199/0194.Transpose File/Solution.sh index 8c3b8696eae66..172a9c8227fa5 100644 --- a/solution/0100-0199/0194.Transpose File/Solution.sh +++ b/solution/0100-0199/0194.Transpose File/Solution.sh @@ -1,17 +1,16 @@ -#!/usr/bin/env bash - -awk ' -{ - for (i=1; i<=NF; i++) { - if(NR == 1) { - res[i] = re$i - } else { - res[i] = res[i]" "$i - } - } -}END { - for (i=1;i<=NF;i++) { - print res[i] - } -} +# Read from the file file.txt and print its transposed content to stdout. +awk ' +{ + for (i=1; i<=NF; i++) { + if(NR == 1) { + res[i] = re$i + } else { + res[i] = res[i]" "$i + } + } +}END { + for (i=1;i<=NF;i++) { + print res[i] + } +} ' file.txt \ No newline at end of file diff --git a/solution/0100-0199/0195.Tenth Line/README.md b/solution/0100-0199/0195.Tenth Line/README.md index 153f2549d5f78..12b957042e61a 100644 --- a/solution/0100-0199/0195.Tenth Line/README.md +++ b/solution/0100-0199/0195.Tenth Line/README.md @@ -44,7 +44,18 @@ Line 10 ```sh +# Read from the file file.txt and output the tenth line to stdout. +tail -n +10 file.txt | head -1 +``` +```sh +# Read from the file file.txt and output the tenth line to stdout. +awk 'NR == 10' file.txt +``` + +```sh +# Read from the file file.txt and output the tenth line to stdout. +sed -n 10p file.txt ``` diff --git a/solution/0100-0199/0195.Tenth Line/README_EN.md b/solution/0100-0199/0195.Tenth Line/README_EN.md index d5f395168ab88..67c2637df5b2f 100644 --- a/solution/0100-0199/0195.Tenth Line/README_EN.md +++ b/solution/0100-0199/0195.Tenth Line/README_EN.md @@ -40,7 +40,18 @@ Line 10 ### **Bash** ```sh +# Read from the file file.txt and output the tenth line to stdout. +tail -n +10 file.txt | head -1 +``` +```sh +# Read from the file file.txt and output the tenth line to stdout. +awk 'NR == 10' file.txt +``` + +```sh +# Read from the file file.txt and output the tenth line to stdout. +sed -n 10p file.txt ``` diff --git a/solution/0100-0199/0195.Tenth Line/Solution.sh b/solution/0100-0199/0195.Tenth Line/Solution.sh index daffebdd1346b..9710beff912c7 100644 --- a/solution/0100-0199/0195.Tenth Line/Solution.sh +++ b/solution/0100-0199/0195.Tenth Line/Solution.sh @@ -1,10 +1,2 @@ -#!/usr/bin/env bash - -# tail -n: use -n +K to output starting with the Kth -tail -n +10 file.txt | head -1 - -# awk: -awk 'NR == 10' file.txt - -# sed: -sed -n 10p file.txt +# Read from the file file.txt and output the tenth line to stdout. +sed -n 10p file.txt \ No newline at end of file