1

how to perform by sed in order to print the line until ";" character

    echo "NUMBER   =   3247  FULL DUPLEX ; speed=343 434 " | sed .....

need the following output

NUMBER = 3247 FULL DUPLEX

2 Answers 2

1

Why don't you use awk for this? i.e.

echo "Number=3247 ; speed=343 434 " | awk {'print $1'}

And if you really need the " in front, use

echo "Number=3247 ; speed=343 434 " | awk {'print "\"" $1'}

EDIT: After feedback (see comments below), here the correct version with sed:

echo "NUMBER = 3247 FULL DUPLEX ; speed=343 434 " | sed 's/\;.*//g'

7
  • not good because Number = 3247 (i have apaces)
    – jennifer
    Nov 3, 2010 at 13:47
  • see please my update in the question
    – jennifer
    Nov 3, 2010 at 13:49
  • OK you're right. How about sed 's\; .*//g' then?
    – pgruetter
    Nov 3, 2010 at 13:54
  • echo "NUMBER = 3247 FULL DUPLEX ; speed=343 434 " | sed 's/\; .*//g' (need to add "/" after "s"
    – jennifer
    Nov 3, 2010 at 14:04
  • you can fix your answer according to your right remark so I will vote for you
    – jennifer
    Nov 3, 2010 at 14:05
1

Use awk with -F to specify the field delimiter:

echo "NUMBER   =   3247  FULL DUPLEX ; speed=343 434 " | awk -F\; '{print $1;}'

Note that the semicolon is excaped with a backslash so that your shell won't misinterpret the semicolon as the end of the command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .