美文网首页
filter/shell/perl/js 基础

filter/shell/perl/js 基础

作者: 33jubi | 来源:发表于2019-08-17 09:25 被阅读0次

filters

egrep
-v “content” 排除
-i ‘content’ 不区分大小写
-w ‘content’ to ignore white spa

[^] reg排除某字母
排除的逻辑:重复问题,前面不排除干净会导致后面的冲突
egrep -v . prints all the empty lines in its input.
The command egrep '^$' would also do this.
? to make the fractional part optional.

cut -d’单字符‘ -f 列
sort 默认升序
sort -n 按数字顺序排序(默认按字母顺序排)
sort<file按第一列排
sort -k2n按第二列排
sort -k2nr按第三列排
sort -r逆序排
sort -nr按数字顺序逆序排

uniq 筛重复
uniq -d找出重复行
-c 计数
uniq -c
rev逆置字符串,双次逆置可以与cut取倒数某列
wc -l统计总行数
tr 替换一系列字符 为 另一系列字符
tr -d ‘a’ 删除a
tr -c = 'complement' so it replaces everything NOT in string 1 with string 2
tr -s = 'squeeze' repeated characters, so it replaces any duplicate newlines with just one
tr -cs ‘aa’ ‘bb’ 在aa中插入bb
wc -w 单词数量
wc -c 多少byte

tr -cs 'a-zA-Z0-9' '\n' | wc -l  //统计单词数量

y=“Y Y”

echo "$y"
Y Y

| $y expands into a single argument |

echo '$y'
$y

| the single quotes prevent variable expansion |

date当前时间
cat打印文本
tac行逆序打印文本

shell #!/bin/sh

shell

return是exit 0和1

exit 0 正常运行退出
exit 1 非正常运行导致退出

数字处理

自增
i=$(($i+ 1))

参数

参数数量 $#
echo ${small[*]} 输出整个数组
参数数组 $@

| 0 | 当前脚本的文件名 | |n | 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是1,第二个参数是2。 |
| # | 传递给脚本或函数的参数个数。 | |* | 传递给脚本或函数的所有参数。 |

shell中计算

i=expr i +increment``
i=$((i + increment))

字符串处理

1.字符串衔接
smallfiles="$smallfiles $file"

判断

1.判断数字
if test “$#” -eq 2
then
do sth
fi

eq 等于
ne 不等于
gt 大于
lt 小于
ge 大于等于
le 小于等于
"3" ne "3.0" but "3" == "3.0"
注意变量➕双引号
shell中==和!=都可以用于判断数字

2.判段文件是否存在
if test -e $file

1/2>/dev/null

2>/dev/null忽略程序自己的错误提示(有时要求输出特定的提示)

if egrep -iw '</?blink>' $file >/dev/null 不加数字默认1,可以在筛选中避免输出

循环

1.数字循环
for i in $(seq 1 10)
do
do something
done

for varible1 in {1..5}
for((integer = 1; integer <= 5; integer++))
while ((i <= finish))

2.文件循环
for file in *

3.while循环
<1> 永真

 while true 
 do
     do sth
 done

文件处理

1.图片类型转换
convert $fileA $fileB
rm $fileA
2.图片展示
display $img
3.stdin
read message
4.mutt 用于邮件发送

echo "$message"| mutt -s 'image' -a "$png_file" -e 'set copy=no' -- "$emailaddress"

5.给文件改名
将文件a命名为b
mv a b
6.将文件a的时间设置为b
touch -r a b
7.罗列文件信息
ls -l
8.>/dev/null 不显示任何信息在控制台(跟在会有输出的操作之后取消那些输出)
9.basename 去除路径信息比如一个文件path:dira/dir2/c/a base这个下来就是a
10.复制文件
cp $file1 $file2

  • -p:除复制文件的内容外,还把修改时间和访问权限也复制到新文件中。
    cp -p file1file2
    11.删除文件
    rm -rf 文件名

12.检测目录是否存在
if test -d dirname
ftype=file -b $f | sed 's/ //'
bytes=`wc -c <"$f" 检测文件多少byte

perl 格式:#!/usr/bin/perl -w

1.匹配
$line =~ s/[0-4]/</g;

while (<STDIN>) {
    tr/0-9/<<<<<5>>>>/;
    print;
}

参数

参数数组/参数个数:@ARGV
参数个数 $#ARGV+1

数组

push
给数组➕值: push @files,$file;
给数组➕多个值: push @names, @others;
pop 取出数组最后一个值(下标@arr-1)并删除$file = pop @files;
!!
shift 取出数组第一个值(下标0)并删除 $file = shift @files;
unshift 给数组开头传入一个或多个值 unshift @files,$file unshift @files,@puls_files

数组最后一位下标-----$#arr

直接打印数组第first到第#lines个

print @lines[$first..$#lines];

数组排序
1.sort @arr基于ascll码排序,升序
2.sort{$a<=>$b} @arr 基于数值比较,升序
3.sort { $a cmp $b } @words;按字母顺序比较,升序
sometime注意大小写

4.@fields = reverse @fields;数组逆置
5.$line_out = join ' ', @fields;数组转字符串

harsh

1.提取key的操作 exp: harsh{key} `@arr=keys%harsh` 2.harsh初始化 `%harsh=();` `%harsh;` 3.给harsh按值排序 和数组道理一样 **注意这里排序的数组不是harsh,理解一下其实是按harsh值给这个数组排序,然后你可以通过她keys 的数组来顺序逆序输出她harsh 的值** sort{harsh{a}<=>harsh{b}} @keys sort{harsh{a}<=>harsh{b}} keys%harsh ==>@sortedharsh_keys=sort{harsh{a}<=>harsh{b}} @keys ==>@sortedharsh_keys=sort{harsh{a}<=>harsh{b}} keys%harsh 4.harsh的遍历,其实就是遍历key的数组 foreachkey(keys%harsh){
harsh{key}do sth; } 5.`sort keys %tag_count` 6.`sort {tag_count{a} <=>tag_count{b} ||a cmp $b} keys %tag_count;`

!/usr/bin/perl -w
while ($line = <>) {
    $line =~ tr/A-Z/a-z/;
    foreach $word ($line =~ /[a-z]+/g) {
        $count{$word}++;
    }
}
@words = keys %count;
@sorted_words = sort {$count{$a} <=> $count{$b}} @words;
foreach $word (@sorted_words) {
    printf "%d %s\n", $count{$word}, $word;
}

return die “message”== or ==exit

exit 0 正常运行退出
exit 1 非正常运行导致退出

文件处理

1.打开文件

open F,’<‘,$file or die “$0: $file cannot open $!\n”;
@lines = <F>;
close F;

$0文件名

2.文件复制模块

use File::Copy;
copy( $original, $new_copy ) or die "Copy failed: $!";

注意目录不存在or目录下没文件可能会报错
➕处理避免

3.创建目录
mkdir($dirname) or die “cannot create $dirname\n”

4.取目录下文件(隐藏目录这个方法不行)
@files = glob( $dir/* ); 一定要+最后的*号

5.fetch url
open my $f, '-|', "wget -q -O- $url" or die;

字符串处理

eq 相等
ne 不相等

lc(line) 字符串转小写 uc(line) 字符串转大写

== 数字相等
!= 数字不相等
这里perl 和shell其实都是eq这种比较字符串,==这种比较数字,但是shell如果字符串是数字用eq也可以比较

  • 匹配拆分字符串 @words=($line =~ /[A-Za-z]+/g);(这里是以匹配部分为内容) 或者 @words=split /[^a-zA-Z]+/ ,$line;(这里匹配的是分隔标志)

  • $count++ if $word ne '';
    system "cut -c$ARGV[0]-$ARGV[1] $ARGV[2]";perl中使用filter

  • 格式化处理 printf “%${width}d”,$x

  • join split
    str1 = join(“<li>\n”,@list); //以参数1为分隔符连接数组 @str2 =split(“<li>\n”,str1); //以参数1为分割符拆分数组

从stdin读入文本 @lines = <>;

循环语句

1.循环
foreach(i=0;i<@ARGV;i++){} 2.循环中断 < 1 > break ---last < 2 > continue ---next `text .= <> foreach (1..$n);`

while ($n-- > 0){
text .= <>; } `forfile (@ARGV[1..$#ARGV])`

ascll

ord(c) 转ascll chr(n) 转字符

函数和方法

sub main{ 
   back_file($file);
}
sub back_file{
    $file = @_;               #传参数的格式
    my ($source, $destination) = @_;     #传参数的格式
}

example:

@a = (1..5);
@b = grep { $_ = $_ - 3; $_ > 0} @a;
print "@a\n";
print "@b\n";

It prints:

-2 -1 0 1 2
1 2

sub compare {
    my $a_length = length $a;
    my $b_length = length $b;
    if ($a_length == $b_length) {
        return $a cmp $b;
    } else {
        return $a_length <=> $b_length;
    }
}

chmop 去除行尾的\n

shell:

if diff "$file" "$directory2" >/dev/null 2>&1
then
      basename "$file"
fi
 

javascript

node js 参数 process.argv[2] //第一个参数
记不得filter map就用for
for中in是遍历下标of是遍历值

  • filter 可以理解为将for中if的内容写进函数中filter过滤
const startWithA=(user)=>user.name.startsWith(‘A');
const isMale=(user)=>user.gender==='male';

const maleA = users.filter(startsWithA).filter(isMale);


  • map map是处理一个数组生成一个新的数组,所以注意参数不可以是字典元素
    用方法筛选字典元素的值({keyname})=>keyname_do_sth;处理的是value不是key(keyname包裹在{}中挑选数据中的这个key作为参数值)
const match = ({matches})=>parseInt(matches);//这是一个function

const map_match = data.map(match);

  • reduce
const sum =(x,y)=>x+y;

const sum_matches = map_match.reduce(sum,0);
  • 间隔运行方法
    setInterval(()=>{do sth},10000);
  • js内置作为调整某个css class是否工作的方法
    toggle(’cssname’)
output.classList.toggle('cssname') //用作自动判断元素是否有该方法list,如果有则删除,如果没有则添加
content.style.display = (content.style.display == 'none') ? 'block' : 'none';
  • 事件委托 event.target
function hide(e){
  // e.target 引用着 <li> 元素
  // 不像 e.currentTarget 引用着其父级的 <ul> 元素.
  e.target.style.visibility = 'hidden';
}

// 添加监听事件到列表,当每个 <li> 被点击的时候都会触发。
ul.addEventListener('click', hide, false);
  • js中变量也可以用变量
document.getElementById(`tab-${i}`).classList.remove('active');
  • promise可以是一个数组,通过数组的操作同时处理多个promise . promise.all()
    eg1

 Promise.all([
    getJSON('https://jsonplaceholder.typicode.com/users'),
    getJSON('https://jsonplaceholder.typicode.com/posts'),
  ])
    .then(([users, posts]) => {
    do sth
    }

eg2

while(i < 5 ){
  promises.push(fetch(url));
}
promise.all(promises).then(responses=>{
   do sth
});

避免使用var ---hoisting it means it’s global, but we prefer to make it more reliable (let &const)

=> :a short hand to declare a quick function

import function from package ---- export function

Node.js is a serverside JavaScript environment that provides an interface with the operating system.

tut 6-t13

相关文章

网友评论

      本文标题:filter/shell/perl/js 基础

      本文链接:https://www.haomeiwen.com/subject/bojtrctx.html