Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    189

    SED help with end of line


    I have a script that fixes my subtitles, right now it will fix a lot of the common errors I am getting. Is there a SED command to look at the end of a line? Here is my sed command to fix lower case L's that should be Upper case I's.

    Code:
    sed 's/ l / I /g'
    Here is the line that it is not fixing:
    Code:
    7
    00:00:40,207 --> 00:00:41,708
    Why aren't l
    using this every day?
    My sed command will not fix the l and the end of the second line because there is no space after it. I know to look at the beginning of a line you need to use "^" and I heard that the end of the line is "$" but I could not get it to work. Can someone help me please.

  2. #2
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    480
    is there a reason for the spaces in your original command "sed 's/ l / I /g'"?
    if you omit them, then your problem line is fixed
    the sun is new every day (heraclitus)

  3. #3
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    189
    yeah, I don't want it to replace every l in the file. That would change ever L (lowercase) to an I in every word. That would be a lot of spelling errors.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    480
    OK, I see. Let's switch utilities, two passes with "tr"

    tr " l " " I " <filename >newfile
    tr " l\n" " I\n" <newfile

    the nice thing about "tr" is, it can handle '\n' but "sed" can't
    the sun is new every day (heraclitus)

  6. #5
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,382
    Hi.

    I would use sed thusly:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate substitution of single character words.
    
    # Utility functions: print-as-echo, print-line-with-visual-space, debug.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && $C sed
    
    FILE=${1-data1}
    
    pl " Input data file $FILE:"
    cat $FILE
    
    pl " Results:"
    sed 's|\bl\b|I|g' $FILE
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
    bash GNU bash 3.2.39
    sed GNU sed version 4.1.5
    
    -----
     Input data file data1:
    7
    00:00:40,207 --> 00:00:41,708
    Why aren't l
    using this every day?
    When l come home.
    The little fox.
    Who is Belinda?
    l can see clearly now,
    lumps are in my mashed potatoes.
    
    -----
     Results:
    7
    00:00:40,207 --> 00:00:41,708
    Why aren't I
    using this every day?
    When I come home.
    The little fox.
    Who is Belinda?
    I can see clearly now,
    lumps are in my mashed potatoes.
    See man pages for details.

    Best wishes ... cheers, drl
    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 )

Posting Permissions

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