Find the answer to your Linux question:
Results 1 to 4 of 4
I am trying to place a word at the beginning of each line using sed. It doesn't work on lines with only one word. . Both of these commands to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2006
    Posts
    6

    Red face Sed problem


    I am trying to place a word at the beginning of each line using sed. It doesn't work on lines with only one word. . Both of these commands to the same thing. They put global at the beginning of each line except empty and single-word lines. Please help.

    sed '/[^ ] /s/^[^ ]/global &/'g bla2
    sed '/[^^$] /s/^[^^$]/global &/'g bla2


  2. #2
    Banned CodeRoot's Avatar
    Join Date
    Sep 2005
    Posts
    567
    Try:

    Code:
    sed 's/^/global/'

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,283
    Hi, nc3b.
    I am trying to place a word at the beginning of each line using sed.
    As CodeRoot said:
    Code:
     Contents of bla2:
         1  not blank$
         2  $
         3  previous line is empty$
         4   space at beginning$
         5    two spaces at beginning$
         6  ^Itab at beginning$
         7  word$
         8  two words$
         9  last line$
    
    Command is "sed 's/^/global /'"
    
    global not blank
    global
    global previous line is empty
    global  space at beginning
    global   two spaces at beginning
    global  tab at beginning
    global word
    global two words
    global last line
    I used "global ", remove the space if you don't want it ... cheers, drl

    ( edit 1: extend example )
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Just Joined!
    Join Date
    Aug 2006
    Posts
    6
    Thank you. I don't want to match empty lines so

    sed '/^$/!s/^/global/g' /path/to/filename
    sed '/[^ ]\{1,\}/ s/^/global /' /path/to/filename

    work for me. Again thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •