• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

Need script to test text file

Hi,
I need a script to test a text file and based on the character it finds on line 1 column 60, run different programs.  The script can be awk, perl or whatever else that will run on IBM AIX.
 
If line 1 column 60 is "O" then run program qu
If line 1 column 60 is "d" then run program ak
if line 1 column 60 is "k" then run program pk
If line 1 column 60 is "a" then run program wt
If line 1 column 60 is anything else, run program other

Any help would be appreciated.

Thanks,
Mark
0
9thTee
Asked:
9thTee
  • 4
  • 2
  • 2
  • +1
1 Solution
 
Peter KwanAnalyst ProgrammerCommented:
awk < textfile.txt 'NR == 1 {
     switch (substr($0, 60, 1)) {
     case "O": qu; break;
     case "d": ak; break;
     case "k": pk; break;
     case "a": wt; break;
     default: other;
     }
}'
0
 
brettmjohnsonCommented:
switch (`head -1 textfile.txt | cut -c 60-60`)
case "O": qu; break;
case "d": ak; break;
case "k": pk; break;
case "a": wt; break;
default: other;
}
0
 
9thTeeAuthor Commented:
pkwan:

When I run this it says:

 Syntax Error The source line is 2.
 The error context is
                switch >>>  (` <<< 
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 2.
 Syntax Error The source line is 3.


Any ideas?

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
9thTeeAuthor Commented:
brettmjohnson:
I am not sure how to run your script.

Mark
0
 
brettmjohnsonCommented:
nevermind, brain burp on my part.  The only interesting part is

`head -1 textfile.txt | cut -c 60-60`

which returns the specific character of interest.
The switch statement is from the previous post
and is in awk/C syntax, not Bourne shell syntax.

0
 
Sys_ProgCommented:
case `head -1 textfile.txt | cut -c:60 -L:1` in
        "O")   qu
        ;;
        "d")    ak
        ;;
        "k")   pk
        ;;
        "a")    wt
        ;;
       *)      Other program
       ;;
esac



Amit
         
 
 
0
 
9thTeeAuthor Commented:
Hi SysProg,
What does the L:1 do?  I had to remove it for the script to run.

Thanks,
Mark
0
 
Sys_ProgCommented:
-L means how many characters to consider
By default, it takes upto the end of line
-c means the start position from where u want to cut the string


Amit
0
 
9thTeeAuthor Commented:
SysProg,
Well the script works fine without it on IBM AIX.  Thanks for the help.

Mark
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now