Using Ex editor commands in a shell script - Help!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Ex editor commands in a shell script - Help!
# 1  
Old 07-04-2011
Using Ex editor commands in a shell script - Help!

Hi all,

I am trying to use the Ex editor and its commands in a script - more specifically within an if statement within a while loop.

Here are the basics of the loop:
Code:
cat $file1 | while read line
do
  grep $line $file2
  if [[ $? != 0 ]]
  then
    echo $line > $file2
  elseex $file2
    /ESI185
  fi
done

FYI:
$file1 is a text file, and $file2 is another text file.
I want the second line in the else section to search for the string "ESI185", and then i want it to go to the next blank line and that will be where i'd like it to begin entering more text.

The problem is, once "ex $file2"is read, it then goes into the ex editor, but will not execute the search string, and therefore stays in the editor.

Any help would be appreciated.

Cheers,
Luke.

Last edited by Franklin52; 07-04-2011 at 08:25 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 07-04-2011
you need use a HERE document. see Here Documents for an example
# 3  
Old 07-05-2011
Quote:
Originally Posted by frank_rizzo
you need use a HERE document. see for an example
Hi,

I've now got this:
Code:
cat $file1 | while read line
do
  grep $line $file2
  if [[ $? != 0 ]]
  then
    echo $line >> $file2
  else
    ex $file2
    /ESI185
    /^$
    i
    EOF
    echo $line >> $file2
  fi
done

But how would I get the echo command to echo the contents of $line to the line of $file2 that the ex commands have looked for above? (i.e. the next blank line after ESI185).

There has to be an easy way to do this, i'm probably just not thinking straight. Never the less, i'd appreciate a bit of help Smilie

Cheers,
Luke.

Moderator's Comments:
Mod Comment Please use [CODE] tags when posting source listings, console output, ...
# 4  
Old 07-05-2011
Do simple things before jumping into complex ones so get the here-document thing working and then focus on the ex part of the script...
# 5  
Old 07-05-2011
It is often simpler to use the ed editor for such tasks. Suppose your original file is;
Code:
$ cat file
AAA
BBB
ESI185
CCC
DDD

and you want to add the text line "sultan kudarat" after ESI185, here is one easy way of doing it:
Code:
$ line='sultan kudarat'
$ printf '%s\n' /^ESI185 a "$line" . w | ed -s file
ESI185
$ cat file
AAA
BBB
ESI185
sultan kudarat
CCC
DDD

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Two exec commands in one shell script?

Hi Folks - Is there a way to add two execs to one script? For instance, I need to redirect the stdout and stderr to two separate directories. I want to do this: #::-- Direct STDOUT and STDERROR to repositories --::# exec 2>"${_ERRORFILE}" > "${_LOGFILE}" exec 2>"/new/path/file.err" >... (7 Replies)
Discussion started by: SIMMS7400
7 Replies

2. Shell Programming and Scripting

Need to overwrite shell script using vi editor

I have an existing shell script that I am trying to modify. I have about 10 lines of info I want to overwrite using text someone emailed to me. I guess what I am trying to do basically is like a copy/paste, but it's not working for me. I am using Cygwin and vi editor. I open the script and... (4 Replies)
Discussion started by: kimberlyg2007
4 Replies

3. UNIX for Dummies Questions & Answers

Commands to run from shell script

Hi script> isumid 98765432 if i give above command in cmd prompt it is running the same thing if i give inside the shell script it is not working below is the code #!/bin/bash isumid 98765432 please give me a solution (16 Replies)
Discussion started by: Ramrangasamy
16 Replies

4. Shell Programming and Scripting

Linux commands in shell script?

I am trying to learn to write basic shell scripts. I have a little experience with perl but none with shell. I am trying to write a simple script to write the last 15 or so lines of log files for my web server to a temp file so I can view all at once. Here's what I have. Are you not able to use... (6 Replies)
Discussion started by: ktb231
6 Replies

5. UNIX for Dummies Questions & Answers

Modifying a shell script without using an editor

Hi all, I really wan't to know that how to edit a shell script with out using an editor.. Is there any command? (4 Replies)
Discussion started by: buddhi
4 Replies

6. Shell Programming and Scripting

writing a shell script of commands

Can anyone help me out in visualizing on what is the logic behind simple unix commands. For Eg: ls command lists files and directories, how it displays I need to know the source code for commands like this. (1 Reply)
Discussion started by: rahul_11d
1 Replies

7. Shell Programming and Scripting

Why can't embed commands like fg or bg in a shell script ?

Hi Can someone explain in an easy way that why can't embed commands like fg or bg in a shell script ? (4 Replies)
Discussion started by: qiulang
4 Replies

8. Shell Programming and Scripting

shell script to run a few commands help!

Hi friends this is first post i am very new to shell scripting so i require your expertise to do the following thank u I need to write a shell script which will run the following commands pg_dump bank > backup(Enter) Wait for bash prompt to appear coz it indicates that the command is... (23 Replies)
Discussion started by: perk_bud
23 Replies

9. UNIX for Dummies Questions & Answers

Vi editor basic commands

I would be thankful if anyone could show me commands to do the following tasks in vi:1) How can I undo or redo my last action in vi editor. 2) How can I copy only a word or a portion of line (not the whole line) in vi, like we can select text and press ctrl+c in notepad to copy any text. 3)... (4 Replies)
Discussion started by: nervous
4 Replies

10. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question