-2

I am writing a Bash script and, I need to remove all lines in between TER, including 'TER's

Input File :

ATOM    186  O3'   U     6       7.297   6.145  -5.250  1.00  0.00           O  
ATOM    187 HO3'   U     6       7.342   5.410  -5.865  1.00  0.00           H  
TER
ATOM      1 HO5'   A     1       3.429  -7.861   3.641  1.00  0.00           H  
ATOM      2  O5'   A     1       4.232  -7.360   3.480  1.00  0.00           O  
ATOM      3  C5'   A     1       5.480  -8.064   3.350  1.00  0.00           C  
ATOM      4  H5'   A     1       5.429  -8.766   2.518  1.00  0.00           H  
TER

Expected output:

ATOM    186  O3'   U     6       7.297   6.145  -5.250  1.00  0.00           O  
ATOM    187 HO3'   U     6       7.342   5.410  -5.865  1.00  0.00           H  

I found

sed '/TER/,$d' ${myArray[j]}.txt >> ${MyArray[j]}.txt  ### ${MyArray[j]} file name through an array 

But this does not work, I think awk will work with Bash Script. help Thanks

Digvijay S
  • 2,665
  • 1
  • 9
  • 21
Hutch
  • 167
  • 2
  • 8
  • What is your expected output? – anubhava Mar 06 '20 at 17:37
  • same file no change – Hutch Mar 06 '20 at 17:38
  • 3
    Appending to the same file you're reading is not the best idea. – Shawn Mar 06 '20 at 17:45
  • expeced output is first two line, without line between TER ...... TER – Hutch Mar 06 '20 at 17:45
  • The `sed` script `'/TER/,$d` by itself generates the correct output (even though it would delete also the lines following the second `TER`). But as @Shawn suggested, the problem in you original command is another. – Enlico Mar 06 '20 at 18:02
  • Does this answer your question? [Using sed to delete all lines between two matching patterns](https://stackoverflow.com/questions/6287755/using-sed-to-delete-all-lines-between-two-matching-patterns) – Sorin Mar 06 '20 at 19:05
  • @shawn you are right, changing the file name solved the problem, also append, only ">" I should use, I would do it like this ${myArray[j]}.txt >> a.txt , in next command, mv a.txt > ${myArray[j]}.txt Thanks – Hutch Mar 06 '20 at 19:16

4 Answers4

0

You can just use sed like this:

sed -i.bak '/^TER/,/^TER/d' "${myArray[j]}.txt"
cat "${myArray[j]}.txt"

ATOM    186  O3'   U     6       7.297   6.145  -5.250  1.00  0.00           O
ATOM    187 HO3'   U     6       7.342   5.410  -5.865  1.00  0.00           H
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

It can be done like this

sed '/TER/,$d' ${myArray[j]}.txt > tmp.txt #note only one " > "
mv tmp.txt ${myArray[j]}.txt
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Hutch
  • 167
  • 2
  • 8
0
sed '/TER/,/TER/d' 
   echo 
   "ATOM    186  O3'   U     6       7.297   6.145  -5.250  1.00  0.00           O  
    ATOM    187 HO3'   U     6       7.342   5.410  -5.865  1.00  0.00           H  
    TER
    ATOM      1 HO5'   A     1       3.429  -7.861   3.641  1.00  0.00           H  
    ATOM      2  O5'   A     1       4.232  -7.360   3.480  1.00  0.00           O  
    ATOM      3  C5'   A     1       5.480  -8.064   3.350  1.00  0.00           C  
    ATOM      4  H5'   A     1       5.429  -8.766   2.518  1.00  0.00           H  
    TER"  |sed '/TER/,/TER/d' 


######################################################################################

ATOM    186  O3'   U     6       7.297   6.145  -5.250  1.00  0.00           O  
ATOM    187 HO3'   U     6       7.342   5.410  -5.865  1.00  0.00           H 

sed '/Start Pattern/,/End Pattern/d'
Digvijay S
  • 2,665
  • 1
  • 9
  • 21
  • Better anchored as `sed '/^\s*TER$/,/^\s*TER$/d'` to require `TER` as the first non-whitespace word and disambiguating from `TERMINAL`, etc.. – David C. Rankin Mar 08 '20 at 01:38
0

awk also provides a simple solution using a flag to control printing. Below the skip variable is used as a flag. If 1 the lines are skipped, on the transition from 1 to 0, the script exits.

awk -v skip=0 '$1=="TER"{skip=skip?1:0; if (!skip)exit}1' file

Above $1=="TER" is used to match lines (records) where the first field is TER (this disambiguates between "TER" and "TERMINAL", etc...) Within the rule, the ternary skip=skip?1:0 sets skip=1 the first time "TER" is encountered and to 0 on the next. If skip==0 the script exits. The 1 at the end is just shorthand for print.

Example Use/Output

Using your data in file, you would get:

$ awk -v skip=0 '$1=="TER"{skip=skip?1:0; if (!skip)exit}1' file
ATOM    186  O3'   U     6       7.297   6.145  -5.250  1.00  0.00           O
ATOM    187 HO3'   U     6       7.342   5.410  -5.865  1.00  0.00           H
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85