如何在<之间提取字符串和>与sed或awk

时间:2013-12-22 23:15:20

标签: bash sed awk

我想在<之间提取所有头文件和>。我有一个名为configure.ac的文件来自git-repository。我想知道这个文件中存在哪些头文件。我想生成一个只包含头文件的列表文件。例如:

# _NL_MEASUREMENT_MEASUREMENT is an enum and not a define
AC_MSG_CHECKING([for _NL_MEASUREMENT_MEASUREMENT])
AC_LINK_IFELSE(
  [AC_LANG_PROGRAM(
    [[#include <langinfo.h>]],
    [[char c = *((unsigned char *)  nl_langinfo(_NL_MEASUREMENT_MEASUREMENT));]])],
  [nl_ok=yes],
  [nl_ok=no])
AC_MSG_RESULT($nl_ok)
if test "$nl_ok" = "yes"; then
  AC_DEFINE(HAVE__NL_MEASUREMENT_MEASUREMENT, 1,
        [Define to 1 if _NL_MEASUREMENT_MEASUREMENT is available])
fi

  if test "$ac_cv_header_sys_shm_h" = "yes"; then
    AC_MSG_CHECKING(whether shmctl IPC_RMID allowes subsequent attaches)
    AC_RUN_IFELSE(
      [AC_LANG_SOURCE([[
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    int main()
    {
      int id;
      char *shmaddr;
    id = shmget (IPC_PRIVATE, 4, IPC_CREAT | 0600);
    if (id == -1)
      exit (2);
      shmaddr = shmat (id, 0, 0);
      shmctl (id, IPC_RMID, 0);
      if ((char*) shmat (id, 0, 0) == (char*) -1)
      {
        shmdt (shmaddr);
        exit (1);
      }
      shmdt (shmaddr);
      shmdt (shmaddr);
      exit (0);
    }
      ]])],
      [AC_DEFINE([IPC_RMID_DEFERRED_RELEASE],[1],
                 [Define to 1 if shared memory segments are released deferred.])
       AC_MSG_RESULT(yes)],
      [AC_MSG_RESULT(no)],
      [AC_MSG_RESULT(assuming no)])

    AC_DEFINE(USE_SYSV_SHM, 1, [Define to 1 to use SYSV shared memory])
  else
    shmtype=none
  fi



输出文件必须包含:

langinfo.h
types.h中
ipc.h
shm.h


我试过了:

echo "#include <stdio.h>" | sed -n 's/.*<\(.*\)\>.*/\1/p'
---> stdio.h

cat configure.ac | sed -n 's/.*<\(.*\)\>.*/\1/p' | sort -u > list.txt
---> It doesn't work



我找不到错误。

2 个答案:

答案 0 :(得分:1)

这取决于您的sed版本。在Mac OS X 10.9.1 Mavericks(BSD sed)上,这可行:

$ sed -n 's/.*\<\(.*\)\>.*/\1/p' data
langinfo.h
sys/types.h
sys/ipc.h
sys/shm.h
$

(其中data是您在问题中引用的configure.ac的片段)。 OTOH,GNU sed(版本4.2.2)给出(...被省略的行):

$  /usr/gnu/bin/sed -n 's/.*\<\(.*\)\>.*/\1/p' data
a
_NL_MEASUREMENT_MEASUREMENT
AC_LINK_IFELSE
AC_LANG_PROGRAM
h
_NL_MEASUREMENT_MEASUREMENT
yes
...
AC_LANG_SOURCE
h
h
h
main
id
shmaddr
...
else
shmtype
fi
$

将正则表达式更改为:

$ /usr/gnu/bin/sed -n 's/.*<\(.*\)>.*/\1/p' data
langinfo.h
sys/types.h
sys/ipc.h
sys/shm.h
$

与BSD sed相同的输出。

道德:默认情况下,尖括号<>不是元字符,也不需要反斜杠转义。

当它们被转义时,它们具有特定的含义(单词或其后的结尾)。

答案 1 :(得分:0)

perl -lne 'print $1 if(/\<(.*?)\>/)' your_file