Updating the files at specific location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Updating the files at specific location
# 1  
Old 12-28-2009
Updating the files at specific location

Hi All,

I have a requirement wherein I need to test if a line in file succeeding "IF" statement has "{" .If the line succeeding "IF" statement does not have "{" , then I need to insert the "{". Similarly , I need to check if a line in file preceding "ENDIF" statement has "}" .If not , then I need to insert the "{" as preceding line.

e.g. If a file file.txt has following content
Code:
$VAR1 = $VAR2 * $VAR3
IF( $VAR1 == 1) THEN
{
	IF ($VAR2 != 2) THEN
		IF($VAR3 = FLOOR($VAR)) THEN
			$VAR4 = $VAR1
				IF($VAR2 < 5) THEN
					PRINT($VAR2)
				}
				ENDIF
		ENDIF
	ENDIF
}
ENDIF

then I need to insert "{" after 2nd , 3rd and 4th "IF" Statement, also I need to insert "}" before 2nd and 3rd "ENDIF" Statement.

One more thing here is that , "{" and "}" to be inserted needs to same way indented as the preceding "IF" and succeeding "ENDIF".

e.g. The final file.txt that I need will be
Code:
$VAR1 = $VAR2 * $VAR3
IF( $VAR1 == 1) THEN
{
	IF ($VAR2 != 2) THEN
	{	
		IF($VAR3 = FLOOR($VAR)) THEN
		{
			$VAR4 = $VAR1
				IF($VAR2 < 5) THEN
				{
					PRINT($VAR2)
				}
				ENDIF
		}
		ENDIF
	}
	ENDIF
}
ENDIF

Can anyone please suggest a solution for this requirement ?

TIA
# 2  
Old 12-28-2009
$ cat braces.awk

Code:
function pr_brace(b,c,s) {print substr(s, 1, index(s, c)-1)b}
$1=="ENDIF" && old1!="}" {pr_brace("}", "E", $0)}
old1~/^IF\(?/ && $1!="{" {pr_brace("{", "I", old0)}
{old0=$0; old1=$1; print}

$ awk -f braces.awk data
# 3  
Old 12-29-2009
Thanks a lot alister. This works like charm Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy files based on specific word in a file name & its extension and putting it in required location

Hello All, Since i'm relatively new in shell script need your guidance. I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder. so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR... (13 Replies)
Discussion started by: prajaktaraut
13 Replies

2. UNIX for Beginners Questions & Answers

Searching the value of a specific attribute among xmls files from a particular directory location

Hi Folks , I have the different xml files at the following directory `/opt/app/rty/servers/tr/current/ops/config` Let's say there are three files named abc.xml bv.xml ert.xml Now inside these xml there can be many tags as like shown below <bean id="sdrt"... (6 Replies)
Discussion started by: unclesamm
6 Replies

3. Shell Programming and Scripting

Help with copying the list of files from one location to other location

A) I would like to achive following actions using shell script. can someone help me with writing the shell script 1) Go to some dir ( say /xyz/logs ) and then perform find operation in this dir and list of subdir using find . -name "*" -print | xargs grep -li 1367A49001CP0162 >... (1 Reply)
Discussion started by: GG2
1 Replies

4. Shell Programming and Scripting

Help on Moving files from one location to another location

Hi, I am new to unix and shell scripting. Please help me in resolving the below issue. In my shell script I have a variable which stores the different files with the path. Now I need to move all the files one by one to another location. ---- 1.... (4 Replies)
Discussion started by: kpagadala
4 Replies

5. Shell Programming and Scripting

Find and replace a string a specific value in specific location in AIX

Hi, I have following samp.txt file in unix. samp.txt 01Roy2D3M000000 02Rad2D3M222222 . . . . 10Mik0A2M343443 Desired Output 01Roy2A3M000000 02Rad2A3M222222 . . (5 Replies)
Discussion started by: techmoris
5 Replies

6. Shell Programming and Scripting

Shell Script for Copy files from one location to another location

Create a script that copies files from one specified directory to another specified directory, in the order they were created in the original directory between specified times. Copy the files at a specified interval. (2 Replies)
Discussion started by: allways4u21
2 Replies

7. UNIX for Dummies Questions & Answers

Updating specific fields with awk using conditions

Can someone help me again, I think with awk? I have a file with 4 columns (pipe-delimited): I just want to convert the last field so that e1 is now 'message 1', e2 is 'message 2', e0 is 'message 3', etc. I don't want to change any other columns because the e0-e10 code may appear as part of a... (4 Replies)
Discussion started by: giannicello
4 Replies

8. Shell Programming and Scripting

Transfer files from one location to another location

Hi, i have to transfer of files of User1 located in Location1 to user2 located in Location2 using shell script. Please suggest me. (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

9. UNIX for Advanced & Expert Users

copy files from one location to similar location

I need help in forming a script to copy files from one location which has a sub directory structure to another location with similar sub directory structure, say location 1, /home/rick/tmp_files/1-12/00-25/ here 1-12 are the number of sub directories under tmp_files and 00-25 are sub... (1 Reply)
Discussion started by: pharos467
1 Replies
Login or Register to Ask a Question