Find the answer to your Linux question:
Results 1 to 4 of 4
sample data sample.txt Code: ^Be|12345|inv123|DOC|a|01| ë^Be|12345|inv124|DOC|b|01| |^Be|12345|inv125|DOC|c|01| expected output Code: 12345|inv123|DOC|a|01| 12345|inv124|DOC|b|01| 12345|inv125|DOC|c|01| I tried as follows; cut -d "|" -f 2- sample.txt but I can get only Code: 12345|inv123|DOC|a|01| ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2012
    Posts
    24

    try to cut to determine what information you want


    sample data sample.txt
    Code:
    ^Be|12345|inv123|DOC|a|01|
    ë^Be|12345|inv124|DOC|b|01|
    |^Be|12345|inv125|DOC|c|01|
    expected output
    Code:
    12345|inv123|DOC|a|01|
    12345|inv124|DOC|b|01|
    12345|inv125|DOC|c|01|
    I tried as follows;
    cut -d "|" -f 2- sample.txt
    but I can get only
    Code:
    12345|inv123|DOC|a|01|
    12345|inv124|DOC|b|01|
    ^Be|12345|inv125|DOC|c|01|
    Can i search 12345 then cut the information I want;
    I tried below it does not work would you help me how to fix my script;
    export param = 12345
    cut -d "$param -f 1- sample.txt

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hi,

    sure, you can use sed to search and replace, e.g.:

    Code:
    sed -i.bak 's/^.*|12345/|12345/' sample.txt
    that matches everything from the beginning of the line (^.*) to "|12345" and replaces it with just "|12345".

  3. #3
    Just Joined!
    Join Date
    Oct 2012
    Posts
    24

    Thumbs up

    Quote Originally Posted by atreyu View Post
    hi,

    sure, you can use sed to search and replace, e.g.:

    Code:
    sed -i.bak 's/^.*|12345/|12345/' sample.txt
    that matches everything from the beginning of the line (^.*) to "|12345" and replaces it with just "|12345".
    You are smart!!!! I always think "cut"
    Thank You!!!

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    You could have used cut, and just redirected the output to a new file...but yeah, that is pretty much why sed exists!

    i'll go ahead and mark this thread as Solved for you. Note that you can do this yourself using the Thread Tools link at the top of the page.

Posting Permissions

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