And*_*res 5 pam perl text-processing
我需要更改文件中的文本行位置;从一行位置到另一行位置,在定义的文本下方。
例如,我有以下文本块(在 /etc/pam.d/system-auth 文件上)
account required pam_unix.so
account sufficient pam_localuser.so
account required pam_permit.so
account required pam_tally2.so
Run Code Online (Sandbox Code Playgroud)
我想将最后一行“移动”到第二行(在包含 pam_unix.so 的下面)
如何使用 Perl 命令完成此操作?
您可以使用将Tie::File文件的行与数组变量联系起来的模块来做到这一点:
perl -MTie::File -e '
tie @lines,"Tie::File","your_file_here";
$last_line = pop @lines;
splice @lines,1,0,$last_line
'
Run Code Online (Sandbox Code Playgroud)
绑定变量 ( @lines) 变得神奇,因为无论您对其执行什么数组操作都会影响它所绑定到的文件的行。
使用此解决方法来解决我的问题...
/bin/grep "pam_tally2.so" /etc/pam.d/system-auth
if [ "$?" -eq "0" ]; then
#looks for line existence (pam_tally2.so) and delete it (if present)
/usr/bin/perl -i -pe 'chomp,$_.="" if /account\s.*required\s.*pam_tally2.so/' /etc/pam.d/system-auth
/usr/bin/find /etc/pam.d/system-auth.bkp | /usr/bin/xargs perl -pi -e 's/account\s*required\s*pam_tally2.so//'
#inserts the -deleted- line underneath pam_unix.so line, as desired. Creating a 'moving line' result.
/usr/bin/perl -i -pe 'chomp,$_.="\n" if /account\s.*required\s.*pam_unix.so/' /etc/pam.d/system-auth
/usr/bin/perl -i -pe 'chomp,$_.="\naccount required pam_tally2.so\n" if /account\s.*required\s.*pam_unix.so/' /etc/pam.d/system-auth
fi
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1193 次 |
| 最近记录: |