0

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`
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
ShravanM
  • 323
  • 1
  • 7
  • 21
  • Shouldn't you be using `$i` ? Try `sed -n "${i}p"` –  Sep 23 '14 at 07:41
  • @Jidder I'm trying to retrieve nth column from "busfile" file by substituting values in "i" – ShravanM Sep 23 '14 at 07:43
  • @user215827, that will print a specific _line._ If you want to print a column, `awk` is probably better - see my answer. – paxdiablo Sep 23 '14 at 07:54
  • The idea behind SO is to match questions with answers. When you _change_ the question in such a way as to render all answers irrelevant (such as adding the `$` you were missing), both the question and answers become far less useful. By all means add an addendum to your question stating that you still get an error with the solutions, just _don't_ change the original question itself. I'll show you what I mean. – paxdiablo Sep 23 '14 at 08:06

2 Answers2

0

You can use:

acList=/z/temp/busfile
i=1
temp1=`sed -n $i'p' $acList`
echo "$temp1"
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

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
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • i tried that one too, still error sed: Function 1{p} cannot be parsed. note: i'm using hp-unix – ShravanM Sep 23 '14 at 07:54
  • @user215827, if it can't parse `1p`, it's bizarrely deficient. But, in any case, the command you want for columns is `awk` rather than `sed`. – paxdiablo Sep 23 '14 at 08:02
  • @user215827 not sure it would make any difference but why are you putting `p` in brackets `{p}` ? –  Sep 23 '14 at 08:07
  • @paxdiablo "sed -n "${i}p" myfile" worked, also is there any way i can store them in varaible? Value of temp1 = "56565665 55 5555" i want them to be stored in three different variables like $work1 $work2 $work3. $work1 should contain 56565665, $work2 55, $work3 5555 – ShravanM Sep 23 '14 at 08:57
  • @user215827, that would be the subject of a _different_ question, once you'd searched SO to ensure it hadn't already been asked, such as http://stackoverflow.com/questions/11799666/how-do-i-split-abcd-efgh-into-abcd-and-efgh :-) – paxdiablo Sep 23 '14 at 09:01