1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Sed to insert pipe separator

Discussion in 'Linux and Unix' started by grinder182533, Jan 17, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. grinder182533

    grinder182533 Thread Starter

    Joined:
    Jun 1, 2009
    Messages:
    72
    Hi
    My txt file consists of records with 6 numbers followed by 3 characters.
    Is there a simple “sed” which will insert a | separator between the 6th and 7th position ?
    Many thanks
     
  2. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    Hi grinder182533,

    Sed is not very amenable to position character replacement in a line, but the best way to do what you need is to combine the use of a bash shell script with the cut command for each line and use wc -l to count the lines along with nawk to assign them from a pipe (backquoted) to a variable name, then use $varname to dereference with expr in a count down loop with both head and tail to modify a temporary output file that at the end replaces the original file.

    Work on it.

    -- Tom
     
  3. TerryNet

    TerryNet Moderator

    Joined:
    Mar 23, 2005
    Messages:
    78,256
    First Name:
    Terry
    Do you mean 7 fields as in 1 2 3 4 5 6 abc?

    Or do you mean one 9 character field as in 123456abc?
     
  4. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    Hi grinder182533,

    Here is an example shell script and file that demonstrates a solution to your problem. To use it, you will have to edit the input file in the script (assumed to be named - file) to the name of your input file. Needless to say, I consider this a quick hack and not a polished solution to your problem - however, it works!

    The following shell script, named test.sh, assumes an input file (coincidentally named file) with the contents:
    abcdefghijklmnopqrstuvwxyz
    zyxwvutsrqponmlkjihgfedcba
    abcdefghijklmnopqrstuvwxyz
    zyxwvutsrqponmlkjihgfedcba

    The code for the script test.sh is:
    Code:
    #!/bin/bash -x
    #test file content lines:
    #abcdefghijklmnopqrstuvwxyz
    #zyxwvutsrqponmlkjihgfedcba
    #abcdefghijklmnopqrstuvwxyz
    #zyxwvutsrqponmlkjihgfedcba
    cp file testfile
    nl=`wc -l testfile | nawk '{ print $1}'`
    nlm1=`expr $nl - 1`
    i=$nl
    while [ $i -gt 0 ] ; do
      # get next line in file
      tmp0=`cat testfile | head -1`
      echo $tmp0 > tmp
      cat testfile | tail -$nlm1 > testfile0
      cat testfile0
      left=`cut -c 1-6 tmp`
      right=`cut -c 7- tmp`
      new=$left"|"$right
      echo $new >> newtmp
      if [ $nlm1 -gt 0 ]
      then test0=`wc -l testfile0 | nawk '{ print $1 }'`
           cp testfile0 testfile
           nlm1=`expr $nlm1 - 1`
           i =`expr $i - 1`
      else rm testfile testfile0 tmp
           mv newtmp file 
           exit 1
      fi
    done
    
    Note: the first line uses the -x for debugging the script. The script will rewrite the input file named file with the desired result - i.e. a '|' character between the 6th and 7th column position in the file. Also, note: the script was not coded to deal with blank lines.

    -- Tom
     
  5. grinder182533

    grinder182533 Thread Starter

    Joined:
    Jun 1, 2009
    Messages:
    72
    Hi Ted and Terry.
    Sorry for the delay in response : I was ill then on holiday.
    I have solved my problem and am getting to grips with sed, but it isn't easy !
    Thanks again.
     
  6. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    Hi grinder182533,

    What was your sed solution?

    -- Tom
     
  7. grinder182533

    grinder182533 Thread Starter

    Joined:
    Jun 1, 2009
    Messages:
    72
    Hi Tom

    sed 's/......./&|/'
     
  8. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    Hi grinder182533,

    Your example puts the character '|' between the 7th and 8th characters - not the 6th and 7th as per your specification -
    IOW sed 's/....../&|/' is what you meant, eh?

    -- Tom
     
  9. grinder182533

    grinder182533 Thread Starter

    Joined:
    Jun 1, 2009
    Messages:
    72
    Indeed it is : I was looking at something else I had to do which was similar to the original post.
    Thanks again for your help Tom.
     
  10. lotuseclat79

    lotuseclat79

    Joined:
    Sep 12, 2003
    Messages:
    20,583
    And thank you grinder for your solution!

    -- Tom
     
  11. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/975219

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice