PS C:\PowerShell\test1> gc passwd s2002408030068:x:527:527::/home/dz02/s2002408030068:/bin/pw s2002408032819:x:528:528::/home/dz02/s2002408032819:/bin/pw s2002408032823:x:529:529::/home/dz02/s2002408032823:/bin/pw PS C:\PowerShell\test1> gc shadow s2002408030068:$1$d8NwFclG$v4ZTacfR2nsbC8BnVd3dn1:12676:0:99999:7::: s2002408032819:$1$UAvNbHza$481Arvk1FmixCP6ZBDWHh0:12676:0:99999:7::: s2002408032823:$1$U2eJ3oO1$bG.eKO8Zupe0TnyFhWX9Y.:12676:0:99999:7::: |
PS C:\PowerShell\test1> gc shadow | %{ $hash = @{} } { $key,$value = $_.trim().split(':')[0,1]; $hash.$key = $value } |
PS C:\PowerShell\test1> switch -f passwd { { $_ -notmatch '^\s*$' } { $ofs = ":"; $input = $_.split(':'); $input[1] = $hash[$input[0]]; "$input" } } s2002408030068:$1$d8NwFclG$v4ZTacfR2nsbC8BnVd3dn1:527:527::/home/dz02/s2002408030068:/bin/pw s2002408032819:$1$UAvNbHza$481Arvk1FmixCP6ZBDWHh0:528:528::/home/dz02/s2002408032819:/bin/pw s2002408032823:$1$U2eJ3oO1$bG.eKO8Zupe0TnyFhWX9Y.:529:529::/home/dz02/s2002408032823:/bin/pw |
好了, 虽然他还不能算得上好使, 但是一般工作都可以完成了. 下面我们测试一下他. 目前他只能支持从管道读取数据.复制代码
- filter awk ([scriptblock] $cmd = $args[0], [regex] $delimiter='\s+')
- {
- $line = @();
- $nf = 0;
- $line = @($delimiter.split($_.Trim()));
- $nf = $line.length;
- $0 = $_;
- for ($i = 1; $i -le $nf; $i++)
- {
- Invoke-Expression "`$i = '$($line[$i - 1])'"
- }
- & $cmd
- }
PS C:\PowerShell\test2> gc file1 0011AAA 200.00 20050321 0012BBB 300.00 20050621 0013DDD 400.00 20050622 0014FFF 500.00 20050401 PS C:\PowerShell\test2> gc file2 I0011 11111 I0012 22222 I0014 55555 I0013 66666 |
PS C:\PowerShell\test2> $hash = @{} PS C:\PowerShell\test2> gc file2 | awk -cmd {$hash[$1.substring(1,4)] = $2} PS C:\PowerShell\test2> gc file1 | awk -cmd { "$0 $($hash[$1.substring(0,4)])" } 0011AAA 200.00 20050321 11111 0012BBB 300.00 20050621 22222 0013DDD 400.00 20050622 66666 0014FFF 500.00 20050401 55555 |
欢迎光临 批处理之家 (http://bbs.bathome.net/) | Powered by Discuz! 7.2 |