Scripting a ksh script to read in a value and take action if that value is true.

Posted on 2004-10-11
Last Modified: 2013-11-17
I need a script that basically compares the value out of a text file and compares it to a value list.  If it meets the value, then it needs to replace the next value with the current value in the text file.  I have started the script as below, but I am having problems with it working right.  Please help.  Since I am fairly new at shell scripting, any suggestions would be greatly appreciated.  I do only prefer to use korn shell.

set -A array

if [ "${array[1]}" -eq "cut -c 81-89 gl265test" ] ; then
      sed 's/${array[1]},${array[2]}/'  gl265test > gl265.out
if [ "${array[2]}" -eq "cut -c 81-89 gl265test" ] ; then
      sed 's/${array[2]},${array[3]}/'  gl265test > gl265.out
if [ "${array[3]}" -eq "cut -c 81-89 gl265test" ] ; then
      sed 's/${array[3]},${array[4]}/'  gl265test > gl265.out
if [ "${array[4]}" -eq "cut -c 81-89 gl265test" ] ; then
      sed 's/${array[4]},${array[5]}/'  gl265test > gl265.out
if [ "${array[5]}" -eq "cut -c 81-89 gl265test" ] ; then
      sed 's/${array[5]},${array[1]}/'  gl265test > gl265.out
Question by:cdeelena
  • 2
LVL 20

Accepted Solution

tfewster earned 250 total points
ID: 12282727
The ","s in the `sed` command need to be "/"s, and the single quotes need to be double quotes, i.e.
sed "s/${search}/${replace}/"

The other potential issues I see are:
You're explicitly testing columns 81-89 for a match, but then the sed command will replace the _first_ matching pattern in the line;

The output, "> gl265.out"  means only the last match in the input file will appear in the output file; Did you mean to use ">>" ?
LVL 20

Expert Comment

ID: 12282749
Oh, and the test  "-eq"  should only be used for integers; For string comparisons, use "="

Author Comment

ID: 12291672
Thanks tfewster,

The error in the sed command was my fault.  I knew it should be a /.  The single quotes did help out.  As far as the columns I am testing, the file is only a single line and the value I was testing for would only be found in column 81-89, so I was ok there.

I didn't know there was a difference for eq, so that helped as well.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
removing nim resources 5 48
remove a combination of patterns from a file 15 60
Unix Question 19 45
Sed question 2 69
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now