Refining if loops using sed/awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Refining if loops using sed/awk
# 1  
Old 11-19-2008
Refining if loops using sed/awk

hi All,

I have the following two requirements:
case 1:
In a file i have the below code:
Code:
 
if ((a>b)) a=b;
else a = c;

by using some means i need to convert the line to the following output:
Output required:
Quote:

if ((a>b))
{
a=b;
}
else
{
a=c;
}
case 2:
In a file i have the below code:

Code:
 
if (a>b) 
 a=b;
else 
 a = c;

Output required:
Quote:
if (a>b)
{
a=b;
}
else
{
a=c;
}

Last edited by engineer; 11-19-2008 at 03:34 AM..
# 2  
Old 11-19-2008
Hi,

try sed with:

Code:
sed "s/a\s*=\s*b;/\n{\n&\n}/;s/a\s*=\s*c;/\n{\n&\n}/" file

\n means newline, \s* means followed by none or more spaces, & stand for the matched string. The second case is basically the same.

HTH Chris
# 3  
Old 11-19-2008
Chris, I need to put the { by checking the presence of ")" .The content a,b can be anything..

Last edited by engineer; 11-19-2008 at 05:10 AM..
# 4  
Old 11-19-2008
Also the output is
Quote:
if ((a>b)) n{na=b;n}
else a = c;
not the required.
# 5  
Old 11-19-2008
Yeah, you didn't tell which flavour of unix you are unix.
Now we know, you sed doesn't know \n as newline.

To make the regexp more general, save this in a file "regexp":

Code:
s/^\(\s*if\s*(\+.*)\+\)\s*\(.*\)/\1\
{\
\2\
}/;s/\(^\s*else \)\(.*\)/\1\
{\
\2\
}/

\n is now represented as \ at the lineend. It matches case 1. To convert case2 to case1, try:

Code:
sed -n '$! N;s/\
//p' file >> file2

Now you can use sed with:

Code:
sed -f regexp file

If have tested it for both of you case on my Linux box.

But most probably what you are looking for is a indenter like "indent".
Trying to match all possible cases over multiple lines with regexp get slow pretty fast.

HTH Chris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with refining script

Happy new year all! I would like to ask for some assistance refining the script I have put together below. I am a noob with bash scripting and still getting used to all the options available originally coming from a windows background couch, couch... Now I am more than happy in NIX world :b::)... (13 Replies)
Discussion started by: Simplify
13 Replies

2. UNIX for Dummies Questions & Answers

Dealing with Empty files, AWK and Loops

I write this bit of code to calculate the mean and variance for all the files in one directory and print the mean and variance in a separate folder but with the same file name. FILES="data/*" for X in $FILES do name=$(basename $X) awk '{x=$0; s+=$0; n++} END{mean=s/n; for (i in x){ss... (20 Replies)
Discussion started by: A-V
20 Replies

3. UNIX Desktop Questions & Answers

awk using 2 input files instead of while loops

Hi Friends, I have two files as input with data that looks like this: file1.txt 1 2 3 4 file2.txt a,aa b,bb c,cc d,dd e,ee f,ff instead of me doing 2 while loops to get the combinations while read line_file1 (2 Replies)
Discussion started by: kokoro
2 Replies

4. UNIX for Dummies Questions & Answers

Bourne-sh (not bash) question about nested loops and sed

Here's the input: alpha, numeric or alphanumeric string ("line 1 string") numeric string ("line 2 string") numeric string ("line 3 string") numeric string ("line 4 string") ... where - each numeric string is in a pattern that can be matched with RE but - there can be any number of... (2 Replies)
Discussion started by: uiop44
2 Replies

5. Shell Programming and Scripting

SOLVED: Refining an awk command

I have a file (file1) with in the below format ST*820*212121 BPR*C*213212.20*C*212*CCD*01***01*071000013*DA*321321*101208 TRN*1*21321321*13213 N1*PR*3232. dff. SYS.*91*3232 ENT*1 N1*PE* 2132121321 RMR*TN*234456677888**192387.20*192387.20 REF*IV*234456677888*213213 3213 UNI... (0 Replies)
Discussion started by: Muthuraj K
0 Replies

6. Shell Programming and Scripting

awk command to avoid loops

Hi... I need a help in using the awk command or any other solution to avoid the usage of loops. My question is : I have a input like this : field1|field2|field3|field4|field5|field6|field7|field8|field9 ex : 4000|testing|scenario|14450|500|320|450|200|100 where the... (2 Replies)
Discussion started by: vijayarajvp
2 Replies

7. UNIX for Dummies Questions & Answers

Passing KSH variable to AWK with two loops

Hi, I need some help to figure out why an outer for loop KSH variable does not decode in AWK but inner for loop does. Below is my code, If I hard code variable 'SUBSEQ' in AWK it works but if I try to pass the SUBSEQ from KSH, it does not and when I pass the variable 'NAM' from KSH it works: I... (1 Reply)
Discussion started by: chowdhut
1 Replies

8. Shell Programming and Scripting

sed used in loops

Hello: This is my first post here, regarding my first attempt at shell scripting.I was given a large amount of word documents which I need to first make into text files, and then change the commercial names to generic names (they are lists of therapeutic drugs), as well as remove some unnecesary... (1 Reply)
Discussion started by: riayi
1 Replies

9. Shell Programming and Scripting

Awk formatting of a data file - nested for loops?

Hello - is there any way in awk I can do... 4861 x(1) y(1) z(1) 4959 x(1) y(1) z(1) 5007 x(1) y(1) z(1) 4861 x(2) y(2) z(2) 4959 x(2) y(2) z(2) 5007 x(2) y(2) z(2) 4861 x(3) y(3) z(3) 4959 x(3) y(3) z(3) 5007 x(3) y(3) z(3) to become... 4861 x(1) y(1) z(1) 4861 x(2) y(2) z(2)... (3 Replies)
Discussion started by: catwoman
3 Replies

10. Shell Programming and Scripting

While loops and awk

I am trying to make a script that will replace backslashes in a file, but only if the occurance is a pathname. In the file, there are a lot of regular expressions as well, so I'm trying to preserve the integrity of those regular expressions, but convert Windows relative paths. I'm using bash and... (1 Reply)
Discussion started by: Loriel
1 Replies
Login or Register to Ask a Question