Removing specific lines from script files.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing specific lines from script files.
# 8  
Old 04-10-2013
Set a scope(=block) from /find/ to the next /list/, like RudiC did.
Code:
for i in *.tmp
do
    if grep 'find[[:blank:]].*[[:blank:]]with[[:blank:]]' "$i" >/dev/null
    then
      # file backup
      cp "$i" "$i".bak
   
      sed -e '/find[[:blank:]].*[[:blank:]]with[[:blank:]]/,/list/ {' \
          -e 's/find\([[:blank:]].*\)[[:blank:]]with[[:blank:]][[:blank:]]*/where\1:/' \
          -e 's/^[[:blank:]]*;[[:blank:]]*$//' \
          -e '}' "$i".bak > "$i"

    fi
done

Deletion of "run" (word or line) is left as an exercise.

---------- Post updated at 11:21 AM ---------- Previous update was at 09:45 AM ----------

The sed scope is not safe.
Perl regular expressions are not only better readable, one can also make use of state variables. So the scope is most safely set.
Code:
perl -pe 'if (s/find(\s.*)\swith\s+/where$1:/) { $insert=1; next }
          if ($insert>=1) { if (/^\s*(;|run)\s*$/) { $buf=$buf . $_; $_=""; $insert=2; next}
          if ($insert==2) { if (/list/) {print "\n" } else {print $buf} $insert=0 }
          }'


Last edited by MadeInGermany; 04-10-2013 at 01:28 PM.. Reason: removed \ at the end, gives syntax error in perl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

How to find files containing two specific lines and delate those lines?

Hi I need to find files in a specified folder where are two specified lines of text and delate that lines. It looks like this" 35. ?>NL 36. <iframe>.......</iframe>NLThe problem is that "?>" is in the other lines and id should not be removed if the next line is not like "<iframe>....." So... (4 Replies)
Discussion started by: androwida
4 Replies

3. Shell Programming and Scripting

Removing 3 consecutive lines from script

Hi I need to remove three consecutive lines of code which appear multiple times during a script. Two of the lines also appear in other parts of the scripts and need to stay so I can't strip out the code per se - It needs to be the exact three lines. Hope that makes sense ! Any help much... (5 Replies)
Discussion started by: Grueben
5 Replies

4. Shell Programming and Scripting

Removing specific records from files when duplicate key

Hello I have been trying to remove a row from a file which has the same first three columns as another row - I have tried lots of different combinations of suggestion on this forum but can't get it exactly right. what I have is 900 - 1000 = 0 900 - 1000 = 2562 1000 - 1100 = 0 1000 - 1100... (7 Replies)
Discussion started by: tinytimmay
7 Replies

5. UNIX for Dummies Questions & Answers

Removing Lines Shared by Multiple Files

Hey everyone, I have a question about comparing two files. I have two lists of files. The first list, todo.csv, lists a series of compounds my supervisor wants me to perform calculations on. The second list, done.csv, lists a series of compounds that I have already performed calculations on.... (2 Replies)
Discussion started by: Stuart Ness
2 Replies

6. Shell Programming and Scripting

Removing specific lines using Unix

Hello everyone, I have a fasta file in the following format: >Sic_7657.x01 bhg|7675859 info:546474 ATGCTAGATGCTAGCTAGCTAGCTGCT CGTAGCTAGTCGTAGCTGATGCTAGGC CGATG >Sic_7657.x1 bhg|76675 info:546474 CGATGCTGATGCTGATCGTGATCTGTC CAGTCGAGCTGATGTCGTATGCGGGTG GCTAGCTA >Sic_7658.y1 bhg|76675... (3 Replies)
Discussion started by: ad23
3 Replies

7. Shell Programming and Scripting

PERL: removing blank lines from multiple files

Hi Guru's , I have a whole bunch of files in /var/tmp that i need to strip any blank lines from, so ive written the following script to identify the lines (which works perfectly).. but i wanted to know, how can I actually strip the identified lines from the actual source files ?? my... (11 Replies)
Discussion started by: hcclnoodles
11 Replies

8. Shell Programming and Scripting

Removing lines from large files.. quickest method?

Hi I have some files that contain be anything up to 100k lines - eg. file100k I have another file called file5k and I need to produce filec which will contain everything in file100k minus what matches in file 5k.. ie. File100k contains 1FP 2FP 3FP File5k contains 2FP I would... (2 Replies)
Discussion started by: frustrated1
2 Replies

9. Shell Programming and Scripting

Removing specific lines

Hi I have a .conf file having many location tags like <Location /main> AuthName main AuthUserFile /ppt/gaea/passwd_main Require user admin </Location> ...... ... <Location /wonder> AuthName gaea AuthUserFile /ppt/gaea/passwd_gaea Require... (3 Replies)
Discussion started by: catgovind
3 Replies

10. Shell Programming and Scripting

need help--script to filter specific lines from multiple txt files

Hi folks, - I have 800 txt files - those files are cisco router configs router1.txt router2.txt ... router800.txt I want to accomplish the following: - I want to have a seperate file with all the filenames that I want to process - I want a script that goes trough all those... (7 Replies)
Discussion started by: I-1
7 Replies
Login or Register to Ask a Question