Removing specific lines from script files.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing specific lines from script files.
# 1  
Old 04-10-2013
Removing specific lines from script files.

Hello,

Activity to perform:

1. Find all of the "*.tmp" files in a given user directory
2. Determine which ones have "find" in them.
3. Replace the "find sequence" of commands with a "list set" of commands.


Example:

Original file:
--------------
Code:
define lastn1 = "A"
	define lastn2 = "Z"
	find MPDB_CORE with MREC > 0
	and (LAST_DATE >= fdate and LAST_DATE <= ldate)
	;
	run
	list/nodef/pagelen=0/noblank/pagewid=255
	MPDB_CORE:MREC/wid=9/c=1
		  PAT_TEL/col=10/mask="9999999999"
	;
	run/sub
	rreplace mpimrph
	bye

Final file should be:
---------------------
Code:
define lastn1 = "A"
	define lastn2 = "Z"
	where MPDB_CORE:MREC > 0
	and (LAST_DATE >= fdate and LAST_DATE <= ldate)
							-- Remove ;
							-- Remove run
	list/nodef/pagelen=0/noblank/pagewid=255
	MPDB_CORE:MREC/wid=9/c=1
		  PAT_TEL/col=10/mask="9999999999"
	;
	run/sub
	rreplace mpimrph
	bye


I am trying following code:
-----------------------------------

Code:
for i in `ls *.tmp`
do 
    grep -i find $i 
	if [[ "$?" -eq "0" ]]
	then
	
	# for i in *.tmp; do cp "$i" "${i%.tmp}"_Orig.tmp; done 
	
	sed 's/find/where/g' $i
	sed 's/[[:blank:]]*with[[:blank:]]*/:/g' $i
		[[ --grep -B 2 -ni list $i {grep -B is not available in my Unix} ]]
		{I need to write a code here to remove lines with ";" and "run" from following code:
		;
		run
		list}
	
	fi
done


Please help.

Thank you,
Manish Kumar

---------- Post updated at 01:13 PM ---------- Previous update was at 11:59 AM ----------

In short, if I have following lines in my code :
Code:
;
	run
	list/nodef/pagelen=0/noblank/pagewid=255
	MPDB_CORE:MREC/wid=9/c=1
		  PAT_TEL/col=10/mask="9999999999"
	;
	run/sub

Then I want to remove the lines having ;" and/or run just before the line having word list in it.

I know how to remove a line matching pattern using sed but don't know to remove lines before matching pattern.

Please help. It's urgent for me.

Many thanks in advance.

Last edited by manishdivs; 04-10-2013 at 06:23 AM.. Reason: typo
# 2  
Old 04-10-2013
Try this:
Code:
grep -il find *.tmp | xargs  sed '/find/,/list/ {/;\|run/d; s/find/where/; s/ with /:/}'
define lastn1 = "A"
    define lastn2 = "Z"
    where MPDB_CORE:MREC > 0
    and (LAST_DATE >= fdate and LAST_DATE <= ldate)
    list/nodef/pagelen=0/noblank/pagewid=255
    MPDB_CORE:MREC/wid=9/c=1
          PAT_TEL/col=10/mask="9999999999"
    ;
    run/sub
    rreplace mpimrph
    bye

You may want to refine the "with" substitution part.
# 3  
Old 04-10-2013
Hello RudiC,

I am getting following error message while using your code:
Code:
sed: 0602-404 Function /find/,/list/ {/;\|run/d; s/find/where/; s/ with /:/} cannot be parsed.

Thank you.
# 4  
Old 04-10-2013
As you can see, it worked with my version. You now need to fiddle around with e.g. the /;\|run/d command, and others. Try /;/d;/run/d. Be creative.

Last edited by RudiC; 04-10-2013 at 05:21 AM.. Reason: typo
# 5  
Old 04-10-2013
Quote:
Originally Posted by RudiC
As you can see, it worked with my version. You now need to fiddle around with e.g. the /;\|run/d command, and others. Try /;/d;/run/d. Be creative.

Hello RudiC,

I did try several option but not getting success might be because I am not well versed with Unix.

I request you to please give me a separate command for reading the line beginning with list and removing the lines before that having ";: and/or "run".

I have on my machine:
Code:
uname -a
AIX AIX-sizing 1 6 00F6FFEE4C00


grep -B is not working on my machine otherwise that easy to achieve.

Please help.
# 6  
Old 04-10-2013
The loop
Quote:
for i in *.tmp; do cp "$i" "${i%.tmp}"_New.tmp; done
is nonsense, because the outer loop cycles through the .tmp files already.
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 's/find\([[:blank:]].*\)[[:blank:]]with[[:blank:]][[:blank:]]*/where\1:/' \
          -e 's/^[[:blank:]]*;[[:blank:]]*$//' "$i".bak > "$i"

    fi
done

Variables like $i should be quoted when used as command arguments.
You can also delete ";" and "run" with the sed command. The latter is left as an exercise (this smells like homework).

Last edited by MadeInGermany; 04-10-2013 at 05:53 AM.. Reason: "if grep"
# 7  
Old 04-10-2013
Hello MadeInGermany,

I kept inner for loop my mistake. I was initially planning to take backup of the files then forgot to remove it from there.
This is not a homework, I stuck in between writing a command sequence upon request received from superior on an urgent basis. Any help will be really appreciated.

As you can see in my initial post that I already covered the parts of replacing find and with with their corresponding changes. I am only stuck to remove ; and/or run only from the lines occurring above line starting with word list.

I know the command to remove the single line using PATTERN, following is the code:
Code:
sed '/;/ c\
	REMOVE_PATTERN' file_name

but I stuck for removing pattern only if occurs in above line where list keyword is found.

In your response, -e 's/^[[:blank:]]*;[[:blank:]]*$//' "$i".bak > "$i" code only replaces ; irrespective of checking that it should appear in above 1 or 2 lines where the code begins with list occurs.

Actually, I want to remove ; and/or run only from the lines occurring above line starting with word list.

I apologize if I am not clear in explaining the situation. Please let me know if I need to explain more.

Please help.
Thank you.

Last edited by manishdivs; 04-10-2013 at 06:39 AM.. Reason: typo
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