KSH - search for string and insert line above


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH - search for string and insert line above
# 1  
Old 05-25-2009
KSH - search for string and insert line above

Hi,, Please excuse my lack of knowledge here. I think I am tring to do something fairly advanced yet am a bit of a beginner.....

I want to use a korn shell (as is the norm with others in my company) to write a script to search a file for a string and insert some text above that line.

Sudo code as follows:
User inputs data item 1
User inputs data item 2
Search commands file for 'end of commands' marker
Insert new command above the line in the format 'command $data1 $data2 true'

I have seen notes on awk and sed but cannot seem to get any output when attempting to run these from the command line.

I already have the script to capture the user data.

Please help

Thanks in advance
Hugo
# 2  
Old 05-25-2009
what have you tried
# 3  
Old 05-25-2009
To be honest, not a lot, I tried simply substituting the text I am searching for from the command line as follows:

$ sed -f hugo01.vm1.commands 's/oldtext/newtext/'

but got the error

sed: file hugo01.vm1.commands line 1: unknown option to `s'

...just to test if I could sustitute the 'end of commands' marker with a new line and put the marker back in as a partial solution
# 4  
Old 05-25-2009
Here is an obfuscated awk based solution:
Code:
pc pbzznaqf_svyr /gzc/pbzznaqf_svyr
njx -i q1="$qngn1" -i q2="$qngn2" '/raq bs pbzznaqf/{cevag "pbzznaq",q1,q2,"gehr"}1' /gzc/pbzznaqf_svyr > pbzznaqf_svyr

rot13.com

Not tested.

It can be almost as easily done entirely whith ksh built in commands if you are inclined to do so.
# 5  
Old 05-25-2009
ok I have this inside my script now, which displays my output to the screen with 'test' in the right place:

awk '/#end of nero56 commands/ {print "test"} {print}' hugo01.vm1.commands > hugo01.vm1.commands

how would I write this to a file? I tried adding a '> newfile' on the end but I got permission denied
# 6  
Old 05-25-2009
process_the_file a_file > a_file

This is a common error, it cannot work since the shell evaluates > a_file before to start the command, therefore a_file is emptied before any work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert line based on found string

Hi All I'm trying to insert a new line at the before each comment line in a file. Comment lines start with '#-----' there are other comments with in lines but I don't want a new line there. Example file: blah blah #do not insert here #this is a comment blah #some more #another comment... (10 Replies)
Discussion started by: Mudshark
10 Replies

2. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

4. Shell Programming and Scripting

Awk, if line after string does not match insert

I have a large file with interface records. I need to check every record that has the string "encapsulation bridge1483" and if the next line after this does not have "ip description" then I need to insert a line to add "ip description blah_blah_blah. Sample file: interface atm 1/0.190158... (3 Replies)
Discussion started by: numele
3 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. Shell Programming and Scripting

how to insert a string as a first line of the file

Hi I need to update a number of existing files and insert #!/bin/ksh line a the first line of the file. Is there any awk or sed command which would help me to do that instead of doing it manually? Thanks a lot -A (10 Replies)
Discussion started by: aoussenko
10 Replies

7. Shell Programming and Scripting

search a line and insert string into specific at position

Hi, guys. I have one question: How can I search for a line with certain string in it and then insert a string into this line? For example: There is a file called shadow, the contents of it are below: ************************** ... yuanz:VIRADxMsadfDF/Q:0:0:50:7:::... (9 Replies)
Discussion started by: daikeyang
9 Replies

8. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

9. Shell Programming and Scripting

how to insert line break + string in vi (search & replace )

Hello all i have big test file that has allot of structure text something like this : <foo1 *.html> <blah action> somthing 1 somthing 2 </blah> </foo1 > now i will like to insert 2 more lines of text below the <blah action> so it will be like : <foo1... (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

Failed to insert string into file before line

Hi folks, I have the following program: #! /bin/ksh set -xv export grepDataSource="</data-sources>" export DB_HOST=tornado export SCHEMA_NAME=IAS10G export dataSource=data-sources.xml sourceString="<data-source class=\"oracle.jdbc.pool.OracleConnectionCacheImpl\" name=\"RiGHTvDS\"... (7 Replies)
Discussion started by: nir_s
7 Replies
Login or Register to Ask a Question