Skip to content

Commit 20de30e

Browse files
committedJul 25, 2016
add four shell scripts
1 parent 98af918 commit 20de30e

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
 

‎shell/file.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name age
2+
alice 21
3+
ryan 30

‎shell/filter_number.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
cat file.txt | egrep "^(\([0-9]{3}\)\s|[0-9]{3}-)[0-9]{3}-[0-9]{4}$"
4+
5+
# Tips : cat file.txt | egrep "(\([0-9]{3}\)\s|[0-9]{3}-)[0-9]{3}-[0-9]{4}"
6+
# [[:anything:]] is in shell regex

‎shell/print_10th_line.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
count=0
4+
5+
while read line
6+
do
7+
count=`expr $count + 1`
8+
if [ $count -eq 10 ]
9+
then
10+
echo $line
11+
fi
12+
done < ./file.txt
13+
14+
# Tips:
15+
# use while loop, because for loop ifs will be "space"",\n,...
16+
# airthmetic in shell use expr, in if condition ues -eq not ==
17+
# read file in shell

‎shell/transpose_file.sh

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
COL=$(cat file.txt | head -n 1 | wc -w)
4+
LINE=$(cat file.txt | wc -l)
5+
6+
#echo $COL
7+
#echo $LINE
8+
cat file.txt | awk -v line=$LINE \
9+
'BEGIN{ for(i=1;i<=2;i++)
10+
{
11+
printf $i;
12+
}
13+
}'
14+
15+
16+
awk '
17+
{
18+
19+
for(i=1;i<=NF;i++){
20+
21+
if(NR==1){
22+
23+
s[i]=$i;
24+
}else{
25+
26+
s[i]=s[i]" "$i;
27+
}
28+
}
29+
}
30+
END{
31+
32+
for(i=1;s[i]!="";i++)
33+
print s[i];
34+
}
35+
' file.txt'

0 commit comments

Comments
 (0)