免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2787 | 回复: 5

请教打印文件时在第一列前添加文件的owner [复制链接]

论坛徽章:
0
发表于 2010-06-10 10:42 |显示全部楼层
find . -type f -name "*"|xargs ls -l
-rw-------  1 root   root       580  4月 27 15:02 ./root/10.1.1.97.hist.20100427_145039
-rw-------  1 root   root        88  4月 27 18:03 ./root/10.1.1.97.hist.20100427_173928
-rw-------  1 root   root      1151  1月 31 00:13 ./root/10.1.1.98.hist.20100130_212824
-rw-------  1 root   root        41  2月  6 18:50 ./root/10.1.1.98.hist.20100206_184129

请教如何逐行打印每个文件,并在第一列前添加文件的owner

论坛徽章:
0
发表于 2010-06-10 10:54 |显示全部楼层
[root@commserver: /tmp]#awk '{print $3" "$0}' test118.txt
root -rw-------  1 root   root       580  4Ô 27 15:02 ./root/10.1.1.97.hist.20100427_145039
root -rw-------  1 root   root        88  4Ô 27 18:03 ./root/10.1.1.97.hist.20100427_173928
root -rw-------  1 root   root      1151  1Ô 31 00:13 ./root/10.1.1.98.hist.20100130_212824
root -rw-------  1 root   root        41  2Ô  6 18:50 ./root/10.1.1.98.hist.20100206_184129

这个意思吗?还是?

论坛徽章:
0
发表于 2010-06-10 11:04 |显示全部楼层
[root@localhost root]# ls -l 10.1.1.177.hist.20100609_094032
-rw-------  1 root root 1080  6月  9 16:51 10.1.1.177.hist.20100609_094032
当前目录下的文件内容格式如下
[root@localhost root]# cat 10.1.1.177.hist.20100609_094032

1276047643 cd
1276047643 ls
1276053491 cd
1276053493 cd
1276053500 ps

我希望打印当前目录下每个文件,并在每行文件前添加文件的owner
root 1276047643 cd
root 1276047643 ls
root 1276053491 cd
root 1276053493 cd
root 1276053500 ps

论坛徽章:
0
发表于 2010-06-10 11:22 |显示全部楼层
回复 3# jiajuzh


    [root@commserver: /tmp]#sh sh1.sh
pan 1276053491 cd
pan 1276053493 cd
pan 1276053500 ps
root 1276047643 cd
root 1276047643 ls
root 1276053491 cd
root 1276053493 cd
root 1276053500 ps
[root@commserver: /tmp]#more sh1.sh
#!/bin/bash

for file in test/*
do
    owner=`ls -l "$file" | awk '{print $3}'`
    awk -v owner=$owner '{print owner" "$0}' $file
done


写的不太好~不过应该是你的需求了。假设你的文件在test目录下

论坛徽章:
0
发表于 2010-06-10 12:18 |显示全部楼层
谢谢呆呆的热心回复,上面的输出是正确的.我将你写的脚本保存为pthist.sh,我想按照第二列的输出进行排序,并将系统时间转换成日期格式,我这样执行可以得到我想要的结果
sh pthis.sh |sort -nk 2|awk '{$2=strftime("%Y-%m-%d %H:%M:%S",$2)}1'

请问如何将排序和转换时间格式的脚本都合并到pthis.sh中?请注意是将所有的输出结果进行排序,最好不要先输出到文件中再进行排序

[root@localhost .hist]# cat pthis.sh
for file in `find . -type f -name "*"|grep -v *.sh`
do
    owner=`ls -l "$file" | awk '{print $3}'`
    awk -v  owner=$owner  -v  RS="#" '{if(NF==3)print owner,$1,$2}' $file
done

论坛徽章:
0
发表于 2010-06-10 15:23 |显示全部楼层
回复 5# jiajuzh
  1. for ...
  2. do
  3.         .
  4.         .
  5. done >file.bak
  6. sort -nk 2 file.bak|awk '{$2=strftime("%Y-%m-%d %H:%M:%S",$2)}1'
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP