-1

I am writing a c-shell script, where I am grepping two different directories in two strings. I wanted to remove the name of the directories which are the same. I only want the unique directory among the two by leaving out the duplicate ones. I am little confused about how to do this.

There are some common directories present in sta_views and pnr_views strings. I am grepped both of them using ls -l command as seen above stored them. Now what I want to do is "first we can loop over sta_view and check if that exist in the pnr_view list and if no , then put them in a separate list , if yes , then do not do anything" Hope this helps to understand you my question. Thank You!

Please let me know the approach to do it,

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 
 

Here sta_views contains the directories that are present in pnr_views. How shall I remove the pnr_views directories from sta_views?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    [Why you shouldn't parse the output of ls(1)](https://mywiki.wooledge.org/ParsingLs) – ceving Sep 21 '21 at 10:50
  • I'm a bit confused as to what you're trying to do. Are you seeking to remove common prefixes of filenames? A very small example would help; it can be with made-up data. (Also, why tag with `tcl`? What language do you really want the answers in?) – Donal Fellows Sep 21 '21 at 10:51
  • Hi There are some common directories present in sta_views and pnr_views strings. I am grepped both of them using ls -l command as seen above stored them. Now what I want to do is "first we can loop over sta_view and check if that exist in the pnr_view list and if no , then put them in a separate list , if yes , then do not do anything" Hope this helps to understand you my question. Thank You! – Coding Enthusiast Sep 21 '21 at 12:42

2 Answers2

0

Use grep -v -f lines-to-subtract.txt all-lines.txt.

Example:

#! /bin/bash

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

grep -v -f <(printf '%s\n' "${subtrahend[@]}") <(printf '%s\n' "${minuend[@]}")
ceving
  • 21,900
  • 13
  • 104
  • 178
  • Hi There are some common directories present in sta_views and pnr_views strings. I am grepped both of them using ls -l command as seen above stored them. Now what I want to do is "first we can loop over sta_view and check if that exist in the pnr_view list and if no , then put them in a separate list , if yes , then do not do anything" Hope this helps to understand you my question. Thank You! – Coding Enthusiast Sep 21 '21 at 12:41
  • @CodingEnthusiast It would be better to write the information into the question instead of a comment. – ceving Sep 22 '21 at 06:44
0

Since you tagged with , I'll give you a Tcl answer. It has the advantage of working correctly even with weirdly named directories (especially including ones with spaces in the name).

# 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]" 

There's a few ways to make the code shorter, especially in modern Tcl, but this is good enough.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215