Search code examples
bashsedkshhp-ux

getting error while using sed function on hp unix box


I'm trying to retrieve nth column from "busfile" file by substituting values in "i"

the below code works fine on redhat linux, when tried on hp unix i'm getting error

"sed: Function {i}{p} cannot be parsed."

here is my code

acList=/z/temp/busfile
i=1
temp1=`sed -n "{i}{p}" $acList`
echo $temp1

Update:

Even when I add the $ as suggested in some of the answers, I still have the same problem.

temp1=`sed -n "${i}{p}" $acList`

Solution

  • If you're trying to use the i variable to print a line, you need to precede it with $:

    temp1=`sed -n "${i}p" $acList`
    

    as per the following transcript:

    pax> i=3
    pax> echo 'a
    ...> b
    ...> c
    ...> d
    ...> e
    ...> f
    ...> g' | sed -n "${i}p"
    c
    

    In situations like this, I tend to first try the simplest solution then gradually add complexity until it fails.

    The first step would be to create a four-line file (called myfile) with the words one through four:

    one
    two
    three
    four
    

    then try various commands with it, in ever increasing complexity:

    sed -n "p" myfile                          # Print all lines.
    sed -n "3p" myfile                         # Print hard-coded line.
    i=3 ; sed -n "${i}p" myfile                # Print line with parameter.
    i=3 ; x=`sed -n "${i}p" myfile` ; echo $x  # Capture line with parameter.
    

    At some point, it will hopefully "break" and you can then target your investigations in a more concentrated manner.


    However, I suspect it's unnecessary here since your purported use of that command to extract a column is incorrect. If you're trying to print a column rather than a line, then awk may be a better tool for the job:

    pax> i=5
    pax> echo 'pax is a really great guy' | awk -vf=$i '{print $f}'
    great