0

I have a number of files that have the following format:

Num  name1  name2  value

So, for example, the file called in1.sp looks like this:

 C1    in1    out1        3.9e-12
 C2    foo    st1/in1     1.2e-14
 C3    foo2   in1         8.3e-14
 ...

and so on. In all lines, one of the name columns contains the filename. I want to remove the entire entry if it contains the filename, even if there is other text. So if above is the input, the desired output would be:

 C1    out1    3.9e-12
 C2    foo     1.2e-14
 C3    foo2    8.3e-14
 ...

Thanks!

1
  • Are the fields tab-delimited or delimited by multiple spaces?
    – Kusalananda
    Apr 8, 2020 at 6:43

3 Answers 3

0

If I also understand you correctly, try this

for f in *.sp; do 
  sed "s/[^ ]*${f%.*}[^ ]*//" "$f" | column -t
done

Output:

C1  out1    3.9e-12
C2  foo     1.2e-14
C3  foo2    8.3e-14
0
0

If I understand you correctly, you want to remove any field value that contains a match to the filename (minus its extension).

If so, you could do a field-wise grep in perl:

$ cat in1.sp 
 C1    in1    out1        3.9e-12
 C2    foo    st1/in1     1.2e-14
 C3    foo2   in1         8.3e-14

then

$ perl -alpe '$_ = join "\t", grep { $_ !~ (split(/\./,$ARGV))[0] } @F' in1.sp 
C1  out1  3.9e-12
C2  foo   1.2e-14
C3  foo2  8.3e-14
0

You could use awk to select the desired columns

$ awk '{print $1 "\t" $2 "\t" $4}' in1.sp 
C1  in1 3.9e-12
C2  foo 1.2e-14
C3  foo2    8.3e-14

This will work as long as the fields inside the columns are guaranteed to be non-empty and never contain whitespace.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .