Matt Matt - 2 months ago 7
Linux Question

Command produces different results in commandline versus script file

I have this line

grep -n -m7 '\$' myfile.asm | tail -n 1 | awk -F':' '{print $1}'


Which produces this output:

27


I wanted to use it in a script that is editing a file. So I did this in
myscript.ksh
:

fileName=$1
....
lastLine=`grep -n -m7 '\$' ${fileName}.asm | tail -n 1 | awk -F':' '{print $1}'`
....
echo ${lastLine}


When I do this the expected value of
27
in
lastLine
is only
7
. Here is first 30 lines of the file I'm working with.

$***********************************************************************
$ THE FOLLOWING BULK DATA ENTRIES RELATED TO EXT. SUPERELEMENT 5000
$ ARE FOR USE IN THE MAIN BULK DATA PORTION OF THE ASSEMBLY RUN
$***********************************************************************
$
SEBULK 5000EXTOP2 MANUAL 12
$
SECONCT 5000 0 NO
60582 60582 180021 180021 180023 180023 180057 180057
180941 180941 180942 180942 180943 180943 180944 180944
180946 180946 180947 180947 204499 204499 231050 231050
231060 231060 232050 232050 232060 232060 252300 252300
252301 252301 253072 253072 253073 253073 401101 401101
401102 401102 401103 401103 401104 401104 402101 402101
402102 402102 402103 402103 402104 402104 403101 403101
403102 403102 403103 403103 403104 403104 404101 404101
404102 404102 404103 404103 404104 404104 405101 405101
405102 405102 405103 405103 405104 405104 406101 406101
406102 406102 406103 406103 406104 406104 407101 407101
407102 407102 407103 407103 407104 407104 408101 408101
408102 408102 408103 408103 408104 408104 631050 631050
631060 631060 632050 632050 632060 632060 652300 652300
652301 652301 653072 653072 653073 653073 662050 662050
662060 662060 677500 677500 677600 677600 678516 678516
678622 678622 679514 679514 679620 679620 703077 703077
703078 703078 1184100 1184100 1184200 1184200 1184400 1184400
$
CORD2C* 220102 220104 0.000000000E+00 0.000000000E+00
* 0.000000000E+00 0.000000000E+00 0.000000000E+00 0.100000000E+01
* 0.100000000E+01 0.900000000E+02 0.100000000E+01


What I am trying to do is copy the SECONCT entry out of this file into another at this stage. I know it begins with
SECONCT
and ends with the 7th
$
. Like I said it works at the command line but not in the script. If I use the "`" operator at the command line I get this error:


-ksh: 7: not found [No such file or directory]


So this is related to that operator but I've never had this problem before. How do I fix it?

Update:
I ran it with
set -x
here is the output:

+ grep -n -m7 '$' sq93a3mkop2_4hz.asm
+ awk -F: '{print $1}'
+ tail -n 1
+ lastLine=7


The error is from the
\$
being read in as
$
. But how do I fix that?

Answer

I think you're overcomplicating things.

This is a much simpler way to output that section:

 sed -n '/SECONCT/,/^\$$/p' myfile.asm

I don't have ksh installed, but this works under csh.

Explanation:

  • "-n" - sed will only print what you are interested in.
  • "/SECONCT/" - Start when the SECONCT line appears.
  • "/^\$$/" - Stop when the next line with just "$" appears.
  • "p" - Print everything between the two patterns, inclusive.
Comments