【问题标题】:How to remove the same directories/names from two strings /list? [duplicate]如何从两个字符串/列表中删除相同的目录/名称? [复制]
【发布时间】:2021-11-14 22:22:31
【问题描述】:

我正在编写一个 c-shell 脚本,其中我在两个字符串中 grepping 两个不同的目录。我想删除相同的目录名称。通过省略重复的目录,我只想要两者之间的唯一目录。我对如何做到这一点有点困惑。

在 sta_views 和 pnr_views 字符串中有一些常见的目录。如上所示,我使用 ls -l 命令对它们都进行了 grepped 存储。现在我想做的是“首先我们可以遍历 sta_view 并检查它是否存在于 pnr_view 列表中,如果不存在,则将它们放在单独的列表中,如果是,则不要做任何事情”希望这有助于理解你我的问题。谢谢!

请告诉我这样做的方法,

set pnr_views = `(ls -l pnr/rep/signoff_pnr/ | grep '^d' | awk '{print $9}'\n)`
set sta_views = `(ls -l sta/rep/ | grep '^d' | grep -v common | grep -v signoff.all.SAVEDESIGN | awk '{print $9}'\n)`

foreach i ($sta_view) 
if [$i == $pnr_view] #just want to remove the list pnr_view from sta_view
then
echo ...
else 
 

此处sta_views 包含pnr_views 中存在的目录。如何从sta_views 中删除pnr_views 目录?

【问题讨论】:

  • 我对你想要做什么感到有点困惑。您是否正在寻求删除文件名的常见前缀?一个非常小的例子会有所帮助;它可以是虚构的数据。 (另外,为什么要标记tcl?你真正想要什么语言的答案?)
  • 您好 sta_views 和 pnr_views 字符串中存在一些常见目录。如上所示,我使用 ls -l 命令对它们都进行了 grepped 存储。现在我想做的是“首先我们可以遍历 sta_view 并检查它是否存在于 pnr_view 列表中,如果不存在,则将它们放在单独的列表中,如果是,则不要做任何事情”希望这有助于理解你我的问题。谢谢!

标签: csh


【解决方案1】:

使用grep -v -f lines-to-subtract.txt all-lines.txt

例子:

#! /bin/bash

minuend=(a b c d e)
subtrahend=(c e)

grep -v -f <(printf '%s\n' "${subtrahend[@]}") <(printf '%s\n' "${minuend[@]}")

【讨论】:

  • 您好 sta_views 和 pnr_views 字符串中存在一些常见目录。如上所示,我使用 ls -l 命令对它们进行了 grepped 存储。现在我想做的是“首先我们可以遍历 sta_view 并检查它是否存在于 pnr_view 列表中,如果不存在,则将它们放在单独的列表中,如果是,则不要做任何事情”希望这有助于理解你我的问题。谢谢!
  • @CodingEnthusiast 最好将信息写入问题而不是评论。
【解决方案2】:

既然你用 标记,我会给你一个Tcl 答案。它的优点是即使使用奇怪命名的目录(尤其是名称中包含空格的目录)也能正常工作。

# How to list directories in a directory, but just get their last name
proc listDirNamesIn {directory} {
    glob -directory $directory -type d -tails *
}

# This will contain all *unwanted* directory names as keys
# The . is ARBITRARY and quoted just for highlighting here
set exclude(common) "."
set exclude(signoff.all.SAVEDESIGN) "."
foreach d [listDirNamesIn pnr/rep/signoff_pnr/] {
    set exclude($d) "."
}

# Now we get the list of directories we're interested in, applying the filter
set found {}
foreach d [listDirNamesIn sta/rep/] {
    if {![info exists exclude($d)]} {
        lappend found $d
    }
}

# Print the sorted result
puts "Name of all the views are [lsort $found]" 

有几种方法可以缩短代码,尤其是在现代 Tcl 中,但这已经足够了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2023-02-13
    • 2020-08-13
    • 2021-11-15
    相关资源
    最近更新 更多