Skip to content

Commit 55a0e4b

Browse files
committedSep 1, 2019
add shell solutions
1 parent 15df183 commit 55a0e4b

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed
 
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
# NF是当前行的field字段数;NR是正在处理的当前行数。
4+
awk '{
5+
for(i=1;i<=NF;i++){
6+
res[$i]++
7+
}
8+
}
9+
END{
10+
for(k in res){
11+
print k,res[k]
12+
}
13+
}' words.txt | sort -nr -k2
14+
15+
#or:
16+
17+
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{print $2" "$1}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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
10+
awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}
17+
' file.txt

‎solution/0195.Tenth Line/Solution.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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

0 commit comments

Comments
 (0)