adding string to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding string to a file
# 1  
Old 04-16-2007
adding string to a file

I 've to add the two statements to some java files:
setValid(false); // the call should be after the openeing curly brackets
return true; // the "return true" before the closing curly brackets

Input:

boolean isValid() {
. . .
}

Output:
boolean isValid() {
setValid(false); // the call should be after the openeing curly brackets

return true; // the "return true" before the closing curly brackets
}


How can I do that please!!!

Thank you
# 2  
Old 04-16-2007
Code:
sed -e "/{/a\\
setValid(false);" -e "/}/i\\
return true;" file

# 3  
Old 04-16-2007
anbu23.. it works fine!! But how to handle "new line"

boolean isValid()
{
. . .
}

Any ideas???
# 4  
Old 04-16-2007
Quote:
Originally Posted by andy2000
I 've to add the two statements to some java files:
setValid(false); // the call should be after the openeing curly brackets
return true; // the "return true" before the closing curly brackets

Input:

boolean isValid() {
. . .
}

Output:
boolean isValid() {
setValid(false); // the call should be after the openeing curly brackets

return true; // the "return true" before the closing curly brackets
}

Code:
awk '/boolean  isValid() {/ { print; print "  setValid(false);"; next }
                       /^}/ { print "return true;"; print; next }
                            {print}'

# 5  
Old 04-16-2007
if you have Python, here's an alternative:
Code:
#!/usr/bin/python
output = []
data = open("javafile").readlines()
for item in data:
     if "}" in item: output.append("return true;")
     output.append(item)     
     if "{" in item: output.append("setValid(false);") 
print ''.join(output )

output:
Code:
# ./test.py
boolean isValid() {
setValid(false);
return true;}

boolean isValid()
{
setValid(false);
return true;}

# 6  
Old 04-17-2007
Quote:
Originally Posted by andy2000
anbu23.. it works fine!! But how to handle "new line"

boolean isValid()
{
. . .
}

Any ideas???
Code:
$ cat file
boolean isValid()
{
. . .
}
$ sed -e "/{/a\\
> setValid(false);" -e "/}/i\\
> return true;" file
boolean isValid()
{
setValid(false);
. . .
return true;
}

What do you mean by new line?
# 7  
Old 04-17-2007
Code:
sed -e '$ i return true;' -e '1 a setValid(false);' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding to an array in an external file, and adding elements to it.

I have an array in an external file, "array.txt", which contains: char *testarray={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};I want to be able to add an element to this array, and have that element display, whenever I call it, without having to recompile... (29 Replies)
Discussion started by: ignatius
29 Replies

2. Shell Programming and Scripting

Adding gaps to a string in bash

I have the following string, and want to introduce additional spaces between the two %s. This will be done by specifying the gap between the %s. Example having gap=8 will put 8 spaces between the two %s. frmt_k1d1_test="%s %s\n" I am doing the script in bash. ---------- Post updated at... (4 Replies)
Discussion started by: kristinu
4 Replies

3. Shell Programming and Scripting

Adding the code after searching the string

Hi All, please suggest me.. How to add the text from one file to another file and need to add the code after 3 lines of below searched line . sample code in standard file: <corecom:Description xml:id="id_2607"> <xsl:value-of... (6 Replies)
Discussion started by: vijayko
6 Replies

4. Shell Programming and Scripting

Adding a String after a text in a Line (using nawk)

Hello I need to add a String after a text in a line. The Unix file is huge and I think nawk would be quick. Current: -name FILTER -node 60265 -cob 31/01/2013 -risktype + -change 1 -filter ALL_NODES -toponly -warnings OFF -delimiter "|" -noheader -select... (4 Replies)
Discussion started by: filter
4 Replies

5. Shell Programming and Scripting

Adding numbers in a string

I am writing a bash script on ubuntu11.10 I have some string having numbers and letter and want to add all the numbers together For example 1s2d23f I want to perform 1 + 2 + 23 and store it in a variable (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

[string processing]Adding new line in file

I have a report file which somewhere has a lines starting with word ".sub" I have a new variable named $new. What i am trying to do is insert the content of $new just before the occurrence of ".sub" in the report file. How can I do that? (11 Replies)
Discussion started by: animesharma
11 Replies

7. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

8. UNIX for Dummies Questions & Answers

Adding one string at the beginning of each line in a file

Hi, I have file a.txt as below. I want to add one string root beginning of each line. Sample file a.txt aaa bbb ccc Sample output Root aaa Root bbb Root ccc Can any one help me on this? (6 Replies)
Discussion started by: siba.s.nayak
6 Replies

9. Programming

Editing or Adding to ELF string tables

Is it possible to add - or edit - data (the strings) contained within the string table of an ELF executable? I know I can access the string table with the following code; while ((scn = elf_nextscn(m_elf, scn)) != 0) { char *name = 0; gelf_getshdr(scn, &shdr); ... (9 Replies)
Discussion started by: Dhodder
9 Replies

10. Shell Programming and Scripting

Adding a sequence string to a file

I have a pipe delimited file I need to add a sequence number to in the third field. The record fields will be variable length, so I have to parse for the second pipe. Another requirement is that the sequence number must be unique to all records in the file and subsequent files created, so the... (5 Replies)
Discussion started by: MrPeabody
5 Replies
Login or Register to Ask a Question