Results 1 to 5 of 5

Thread: Combining sed and head/tail commands in bash script.

  1. #1
    Join Date
    Jul 2012
    Beans
    31

    Combining sed and head/tail commands in bash script.

    Hello great folks,

    As the title says i want to join 2 or more sed and head/tail command in a script. How should i do it ?
    I have
    Code:
    #!/bin/sh
    
    for file in ./*.htm
        do echo $file
        sed -r 's_<font color="#800000">&nbsp;<a name="([1-9]|[1-4][0-9]|50)"></a>|\[([1-9]|[1-4][0-9]|50)\] \[<a href="#top">[A-Za-z. ]+</a>\]__g' "$file" >"$file"_new
        done
    
    exit
    Want to add
    Code:
    #!/bin/sh
    
    for file in ./*.htm
        do echo $file
        tail -n +35 "$file" | head -n -11 > "$file"_new
        done
    
    exit
    I may have to add more so looking forward to grab the base idea.

    Thanks in advance for any kind of help and suggestions.
    Last edited by Peterinall; May 1st, 2013 at 08:39 PM.

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Combining sed and head/tail commands in bash script.

    something like this?
    Code:
    tail  -n +35 "$file" | head -n -11 | sed -r 's_...__g' > "$file"_new
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Jul 2012
    Beans
    31

    Re: Combining sed and head/tail commands in bash script.

    Sorry for the silly question but can i add more sed commands to the line like
    Code:
    tail  -n +35 "$file" | head -n -11 | sed -r 's_...__g' -re 's_..._g' and so on..... > "$file"_new
    Thanks you very much.

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Combining sed and head/tail commands in bash script.

    you can pipe as many commands as you wish
    cmd1 | cmd2 | sed | sed | sed
    though in case of sed you can indeed run multiple expressions at once
    eg
    cmd1 | cmd2 | sed 's/.../.../g; s/.../.../g; s/.../.../g'
    -e does the same i think (i don't know if there is a functional difference between these 2 in some special cases, i usually go with the ; version)

    example:
    Code:
    $ echo $'abc def ghi\naabcc ddeff gghii'
    abc def ghi
    aabcc ddeff gghii
    $ echo $'abc def ghi\naabcc ddeff gghii' | sed -r 's/abc/#/g; s/def/@/g; s/ghi/%/g'
    # @ %
    a#c d@f g%i
    $ echo $'abc def ghi\naabcc ddeff gghii' | sed -re 's/abc/#/g' -e 's/def/@/g' -e 's/ghi/%/g'
    # @ %
    a#c d@f g%i
    Last edited by Vaphell; May 1st, 2013 at 03:19 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Jul 2012
    Beans
    31

    Re: Combining sed and head/tail commands in bash script.

    Thank you very much for your time n help.

    Regards

Tags for this Thread

Bookmarks

Posting Permissions

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