sed或awk抓住多种图案并打印

时间:2017-09-21 13:56:37

标签: bash awk sed

我需要从bash结果文件中提取用户的路径和短名称,但我真的不知道如何用sed或awk解析它

这是一个安排1line结果的例子。

"drwxr-xr-x+","2","adm","com.apple.local.ard_admin","68","Sep","7","23:17","/Volumes/raid-partages/POLE LTD/Plannings mensuels et hebdos LTD/Z Archive Planning CS LTD/Planning 2017 ds dossier CS","0: user:d_zeiger inherited allow list,search,readattr,readextattr,readsecurity,file_inherit,directory_inherit,"1: user:f_mure inherited allow list,search,readattr,readextattr,readsecurity,file_inherit,directory_inherit,"2: group:projet_gestion_temps_plannings_ls inherited allow list,search,readattr,readextattr,readsecurity,file_inherit,directory_inherit,"3: group:css_et_cs_pole45_bsi_drh inherited allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit,"4: user:us9-2_cadre inherited allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit,"5: user:css_ltd inherited allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit,"6: user:c_naegel inherited allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit,"7: user:d_zeiger inherited allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit,"8: user:us3_soins inherited allow list,search,readattr,readextattr,readsecurity,file_inherit,directory_inherit,"9: user:css_ltd inherited allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit,"10: user:extrahosp_s4_cadre inherited allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit,"11: user:p_triantafyllidis inherited allow list,search,readattr,readextattr,readsecurity,file_inherit,directory_inherit,"12: user:us_3 inherited allow list,search,readattr,readextattr,readsecurity,file_inherit,directory_inherit,"13: user:a_burglen inherited allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit,"14: user:d_spiess inherited allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit,"15: user:s_kling inherited allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit,"16: user:hopjour_cadre_s4 inherited allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,file_inherit,directory_inherit,"17: user:mc_broggini inherited allow list,search,readattr,readextattr,readsecurity,file_inherit,directory_inherit,"18: user:c_faffa inherited allow list,search,readattr,readextattr,readsecurity,file_inherit,directory_inherit"

我需要提取/Volumes/raid-partages/POLE LTD/Plannings mensuels et hebdos LTD/Z Archive Planning CS LTD/Planning 2017 ds dossier CS(路径始终是9个数据),例如user:us_3(谁可以在路径后面的任何位置)

3 个答案:

答案 0 :(得分:0)

查找最后user:something

$ sed -E 's/^("[^"]*",){8}"([^"]*)".*(user:[^ ]+).*/\2|\3/' file
/Volumes/raid-partages/POLE LTD/Plannings mensuels et hebdos LTD/Z Archive Planning CS LTD/Planning 2017 ds dossier CS|user:c_faffa

查找user:ussomething

$ sed -E 's/^("[^"]*",){8}"([^"]*)".*(user:us[^ ]+).*/\2|\3/' file
/Volumes/raid-partages/POLE LTD/Plannings mensuels et hebdos LTD/Z Archive Planning CS LTD/Planning 2017 ds dossier CS|user:us_3

查找user:us_3

$ sed -E 's/^("[^"]*",){8}"([^"]*)".*(user:us_3) .*/\2|\3/' file
/Volumes/raid-partages/POLE LTD/Plannings mensuels et hebdos LTD/Z Archive Planning CS LTD/Planning 2017 ds dossier CS|user:us_3

\1包含八个单元格,\2第九个。 \3包含用户。

答案 1 :(得分:0)

为CSV数据使用合适的CSV解析器。

如果我修复了CSV,我可以使用ruby来执行:

ruby -rcsv -e '
  $stdin.each_line do |line|
    CSV.parse(line) do |row|
      result = row[9..-1].inject([row[8]]) do |res, elem|
        if m = elem.match(/\buser:(\w+)/)
          res << m[1]
        end
        res
      end
      puts CSV.generate_line result, :force_quotes => true
    end
  end
' < file.csv
"/Volumes/raid-partages/POLE LTD/Plannings mensuels et hebdos LTD/Z Archive Planning CS LTD/Planning 2017 ds dossier CS","d_zeiger","f_mure","us9","css_ltd","c_naegel","d_zeiger","us3_soins","css_ltd","extrahosp_s4_cadre","p_triantafyllidis","us_3","a_burglen","d_spiess","s_kling","hopjour_cadre_s4","mc_broggini","c_faffa"

答案 2 :(得分:0)

现在您的问题很明确:如果在行中某处定义了用户us_3,请从第9列获取目录。

解决方案 GNU awk

$ awk -F, -v usr=us_3 '$0 ~ "user:"usr{print gensub(/"/,"","g",$9)}' input.txt
/Volumes/raid-partages/POLE LTD/Plannings mensuels et hebdos LTD/Z Archive Planning CS LTD/Planning 2017 ds dossier CS

OR解决方案 mawk

$ awk -F, -v usr=us_3 '$0 ~ "user:"usr{gsub(/"/, "");print $9}' input.txt
/Volumes/raid-partages/POLE LTD/Plannings mensuels et hebdos LTD/Z Archive Planning CS LTD/Planning 2017 ds dossier CS