Remove line containing string and renumber


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove line containing string and renumber
# 8  
Old 10-13-2014
With the awk suggestion:
Code:
$ cat rmlist.awk

Code:
NR==FNR{
  A[$1]
  next
}

FNR==1 { 
  c=1
  close(f)
  f=FILENAME ".new"
} 

$3 in A {
  next
}
 
FNR>14 {
  sub($1,c++)
}

{
  print>f
}

You could try this example of how files that are in a directory hierarchy could be processed:
Code:
 find . -name '*out.txt' -exec awk -f rmlist.awk rmlist.dat {} +


Last edited by Scrutinizer; 10-14-2014 at 12:08 AM..
# 9  
Old 10-14-2014
Quote:
Originally Posted by LMHmedchem
[...]
For the Perl script posted by Aia, these files are in a directory structure of 40 different directories. I don't know perl well enough to set up the looping to troll through all of that and test the script. It seems as if I would have to run my script and then call your script and pass $FILE_LIST along with the file with the patterns to remove. Is that right?
[...]
LMHmedchem
Don't know how you get your `wanted' files to process upon. I made it to accept any succession of paths, individually or glob. The only requirement is to pass it first a file containing the patterns to search.
# 10  
Old 10-22-2014
I have run into a problem with one of my patterns,
spiro[2,3-dihydrobenzothiazole-2,1'-cyclohexane]

The script doesn't like this because of the unmatched single quote. I have been putting all the patterns in single quotes to deal with special characters. I don't think I can escape this single quote because the strings are single quoted already.

This is the current list of patterns,
Code:
REMOVE_LIST=(
             '(2-iminochromen-3-yl)-N-(4-methoxyphenyl)carboxamide'
             '(4S,5R)-2,4,5-tri(4-pyridyl)-2-imidazoline'
             '[4-(tert-butyl)phenyl]-N-(4-piperidylphenyl)carboxamide'
             '{[(1E)-2-(4-phenylphenyl)-1-azaprop-1-enyl]amino}[(4-methylphenyl)amino]methane-1-thione'
             '1-(4-methylpentyl)-5-(pyrrol-2-ylmethylene)-1,3-dihydropyrimidine-2,4,6-trione'
             '1,2-bis(2,5-dimethyl-3-thienyl)ethane-1,2-dione'
             '1-methyl-2-(3-(2-quinolyl)(4-quinolyl))benzimidazole'
             '2-((1E,4E)-3,5-di(2-thienyl)-2,4-diazapenta-1,4-dienyl)thiophene'
             '2,3,3-trimethyl-3H-pyrrolo[3,2-h]quinoline'
             '2-[6-((4aS,9bR)-2,8-dimethylpiperidino[4,3-b]indolin-5-yl)-6-oxohexyl]benzo[c]azoline-1,3-dione'
             '2-piperidylphenol'
             '3-(2H,3H-benzo[3,4-e]1,4-dioxan-6-ylazamethylene)isoindolylamine'
             '3,4-diphenyl-2-(phenylazamethylene)-1,3-thiazoline'
             '3-[(2,4-dimethylphenyl)amino]-1-phenylpropan-1-one'
             '3-pyridyl_(2E)-3-phenylprop-2-enoate'
             '4-[(2S,6S)-2,6-bis(2-methylprop-2-enyl)-4-1,2,5,6-tetrahydropyridyl]hepta-1,6-dien-4-ylamine'
             '5-acetyl-1,3-diphenyl-2-pyrazoline'
             '5-dodecyl-1,3,5-triazaperhydroine-2-thione'
             '6,9-dimethyl-2,3,4,9-tetrahydro-4aH-carbazol-1-one'
             'bitolterol'
             'ethyl_2-[3-(ethoxycarbonyl)-2-pyridyl]pyridine-3-carboxylate'
             'methyl_3-[(3-imino-2H-benzo[c]azolidinylidene)azamethyl]benzoate'
             'N-[(1-butyl-2-oxobenzo[d]azolin-3-ylidene)azamethyl]-2-(phenylamino)acetamide'
             'spiro[2,3-dihydrobenzothiazole-2,1'-cyclohexane]'
            )

Any ideas about how to deal with this?

LMHmedchem
# 11  
Old 10-22-2014
Change the line:
Code:
             'spiro[2,3-dihydrobenzothiazole-2,1'-cyclohexane]'

to:
Code:
             "spiro[2,3-dihydrobenzothiazole-2,1'-cyclohexane]"

# 12  
Old 10-22-2014
I actually tried,

"spiro[2,3-dihydrobenzothiazole-2,1\'-cyclohexane]"

because I thought I would have to escape the single quote.

The escaped version did work, but is it generally better to use double quotes for a situation like this with allot of potential special characters?

LMHmedchem
# 13  
Old 10-22-2014
You don't have to escape single quotes inside double quotes and you don't have to escape double quotes inside single quotes.

Choosing whether to use single quotes or double quotes depends on what the strings you're quoting contain.

If you want parameter expansion, command substitution, or arithmetic expansion in your strings, or if you have single quotes in your strings; use double quotes. If your strings contain single quotes or contain dollar signs that could appear to the shell to be parameter expansions, command substitutions, or arithmetic expansions that you want to appear as literal (unexpanded, unsubstituted) character sequences; use single quotes. If your strings don't contain any quotes or dollar signs, single quoting may be slightly faster because the shell doesn't have to look for dollar signs as it evaluates the string.

If you have single quotes and double quotes in your strings, if you want to expand or substitute some, but not all, parameter expansions, command substitutions, and arithmetic expansions; concatenate various types of quoted strings. For instance if you want the string "'" you can use:
Code:
'"'"'"'"'

and to expand one variable and leave another unexpanded:
Code:
"$ExpandMe"'$DoNotExpandMe'

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Vi remove line range containing a string

In vi I would like to remove a line containing a string. I thought after reading this I could do this. https://www.unix.com/302297288-post3.html :'3560,3572/gcc/d' It keeps complaining vi mark not set. And sometimes it complains E488: Trailing characters. I don't understand what mark... (5 Replies)
Discussion started by: cokedude
5 Replies

2. Shell Programming and Scripting

Remove every line with specific string, and also the one above and below it

I would like to identify every line with a specific string, in this case: "Mamma". I would like to remove that line, and also the line above it and below it. So the below Where are all amazing Flats Look At The Great Big White Hey There Hot Mamma You Are So hot Baby I wish You were Mine... (5 Replies)
Discussion started by: phpchick
5 Replies

3. Shell Programming and Scripting

gawk to remove last character in a line or string

I am outputting a line like this print $2 "/" $4The last character though is a ":" and I want to remove it. Is there any neat way to remove it? Or am I forced to do something like this: print $2 "/" substr($4, 1, length($4) - 1)Thanks. (6 Replies)
Discussion started by: benalt
6 Replies

4. Shell Programming and Scripting

Remove last string from last line in a file

Hi, I have a file like : I want to remove last string in last line (here total string is "05550"~). And last line end with ~ character. Output should be : Please help me Thanks in advance (3 Replies)
Discussion started by: mnmonu
3 Replies

5. Shell Programming and Scripting

Remove last string from each line

I am trying to write a script that will allow me to recursively look at my directories, and output all filenames to a txt document. I am almost finished, however I am hitting one last snag. Here is my script so far: find . | grep .jpg | awk -F"/" '{print $NF}' > output.txtThis will give me an... (7 Replies)
Discussion started by: Davinator
7 Replies

6. Shell Programming and Scripting

Remove line based on string and put new line with parameter

Hi Folks, I am new to ksh, i have informatica parameter file that i need to update everyday with shell script. i need your help updating this file with new parameters. sample data $$TABLE1_DATE=04-27-2011 $$TABLE2_DATE=04-23-2011 $$TABLE3_DATE=03-19-2011 .......Highligned... (4 Replies)
Discussion started by: victor369
4 Replies

7. Shell Programming and Scripting

Remove Command-Line Option from String

I want to add a "-r <remote_host>" option to my ksh script, causing the script to run a script of the same name on the specified remote host. The remote invocation should itself include all the command-line options of the original invocation, less the -r option. For example, this invocation: ... (7 Replies)
Discussion started by: mattmiller
7 Replies

8. Shell Programming and Scripting

How to remove new line char from a string

Hi Can anyone tell me how can i remove new line character from a string. My requirement is to read a line from a file and store it to a string. read line string1=$line read line string2=$line echo $string1$string2 The result i am getting in different line. i want the output in the same... (1 Reply)
Discussion started by: sreedivia
1 Replies

9. Shell Programming and Scripting

Remove Line that contains specific string

I am looking for a way to remove any line in a text file that contains the string "Mac address". I guess you would grep and sed, but I am not sure how to do this. Thanks for you help. (3 Replies)
Discussion started by: CBarraford
3 Replies
Login or Register to Ask a Question