-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiralinha
executable file
·59 lines (52 loc) · 1.1 KB
/
tiralinha
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/zsh
usage() {
echo "*** Tiralinha 1.1"
echo "-----------------"
echo "Usage: $0 <line number1> [<line number 2> <line number 3>...] <file1> [<file2> <file3>...]"
}
if [[ $# -eq 0 ]]
then
usage
exit 2
fi
if [[ $# -eq 1 ]]
then
if [[ $1 =~ ^[0-9][0-9]*$ ]]
then
linha=$1
files=~/.ssh/known_hosts
else
usage
echo "First argument (line number) must be a number!"
exit 3
fi
else
[[ $1 =~ ^[0-9][0-9]*$ ]] || {
usage
echo "First argument (line number) must be a number!"
exit 3
}
linha=$1
shift
while [[ $1 =~ ^[0-9][0-9]*$ ]]
do
linha="$linha $1"
shift
done
files="$@"
fi
initexpr="$(echo $linha | sed 's/\(\w\w*\)/a[\1]=y\;/g')"
for arq in $files
do
[[ -r "$arq" && -w "$arq" ]] || {
echo "File '$file' is not readable or writable. Aborting."
exit 4
}
done
for arq in $files
do
newfile=$(/bin/mktemp)
# /bin/sed -n ${linha}'!p' < "$arq" > "$newfile" && /bin/cat "$newfile" > "$arq"
/usr/bin/awk 'BEGIN {'"$initexpr"'} (!(FNR in a)) {print $0}' < "$arq" > "$newfile" && /bin/cat "$newfile" > "$arq"
/bin/rm "$newfile"
done