1

Manual

-p pattern
         The file is split whenever an input line matches pattern,
         which is interpreted as an extended regular expression.  The
         matching line will be the first line of the next output file.
         This option is incompatible with the -b and -l options.

Code

seq -w 1 1 10 | gsed ':a;N;$!ba;s/\n//g' | split -p '060'

which makes a sequence from 1 to 10, removes empty lines to have a megastring and then splits at 060 into two files unsuccessfully giving split: invalid option -- 'p'.

Command seq -w 1 1 10 | awk -F'060' '{print "field1: "$1 "\nfield2: 060"$2}' in comment gives

field1: 01 field2: 060 field1: 02 field2: 060 field1: 03 field2: 060 field1: 04 field2: 060 field1: 05 field2: 060 field1: 06 field2: 060 field1: 07 field2: 060 field1: 08 field2: 060 field1: 09 field2: 060 field1: 10 field2: 060

which is not what I want. I want to split the file at the mark 060 to two files. You can include 060 to either file.

How can you use the flag -p in BSD Split in OSX?

  • 1
    The fact that you're getting 'invalid option' suggests that you might not be calling the correct split. What does which split return? – mjturner Jul 6 '15 at 8:56
  • @mjturner I added the output of your command. Yes, it seems that something wrong in my system. My manual of man split is about BSD, while my actual split is about GNU. – Léo Léopold Hertz 준영 Jul 6 '15 at 9:05
  • Why are you not using both the seq and gsed commands while substituting awk for split? The following commands output is two lines, one is field1: 0102030405 and the other is field2: 0607080910. The command is: seq -w 1 1 10 | gsed ':a;N;$!ba;s/\n//g' | awk -F'060' '{print "field1: "$1 "\nfield2: 060"$2}' – user3439894 Jul 6 '15 at 11:48
2

The issue is caused by BSD split being later in your path than the GNU split (which doesn't support the -p option). If you do an echo $PATH you should see this - /usr/local/opt/coreutils/libexec/gnubin will be before /usr/bin in the output.

To call BSD split directly, use the full path - pipe to /usr/bin/split instead of split.

Your original command will then become:

seq -w 1 1 10 | gsed ':a;N;$!ba;s/\n//g' | split -p '060'
  • How can set PATH and MANPATH consistent such that the command man split is about split command? – Léo Léopold Hertz 준영 Jul 6 '15 at 9:12
  • If you'd like the GNU commands installed with a prefix, you'll need to reinstall them. Once that's done, amend your PATH and MANPATH in your shell configuration files accordingly. How did you install the GNU commands? I have a feeling by compiling by hand - if so, rather use a package manager like pkgsrc, MacPorts or Homebrew. Much better than compiling from source. – mjturner Jul 6 '15 at 9:14
  • 1
    @Masi That's by design. split splits a multiple line file into multiple files, using the -p pattern. As you're only using a single line of input, that's what's output. split doesn't split a matching line, it uses it as the start of the next file. Hopefully that makes sense! – mjturner Jul 6 '15 at 9:22
  • 1
    @mjturner, Your proposed awk command splits the string, so to speak, however its output discards "060" (as it's set as the field separator) from the string and I believe Masi doesn't want data discarded. Would the following be appropriate? awk -F'060' '{print "field1: "$1 "\nfield2: 060"$2}' – user3439894 Jul 6 '15 at 10:07
  • 1
    @Masi, I don't know how you're getting all those fields however the following commands output is two lines, one is field1: 0102030405 and the other is field2: 0607080910. The command is: seq -w 1 1 10 | gsed ':a;N;$!ba;s/\n//g' | awk -F'060' '{print "field1: "$1 "\nfield2: 060"$2}' – user3439894 Jul 6 '15 at 10:53
1

split operates on multiple line files not on a single line file or string. In the example below I'm using jot because seq in not on my system. Also I'm using BSD sed with an addition that changes the marker 060 to a colon. This produces the string 0102030405:7080910. We add the 060 back into the second file.

    jot -w "%02d" 10 1                                     |
    sed -e ':a' -e 'N' -e '$!ba' -e 's/\n//g' -e 's/060/:/'|
    IFS=: read -r one two
    echo "$one" > file1
    echo "060${two}" > file2
  • This jot -w "%02d" 10 1 | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n//g' -e 's/060/:/' | IFS=: read -r one two gives me nothing as an output. What is your output at that stage? The problem is with IFS=... My currently in Debian 8.1 here read is some Linux variant. – Léo Léopold Hertz 준영 Jul 17 '15 at 20:11
  • The first two commands give me correctly 0102030405:7080910. I try to get OSX soon in my fingers. What is this flag -r doing? I try to do it with this Debian 8.1. – Léo Léopold Hertz 준영 Jul 17 '15 at 20:20
  • 1
    I'm assuming that you are using bash. Just type help read for an explanation. – fd0 Jul 17 '15 at 21:14
  • Thank you! I did not know that help. I get -r do not allow backslashes to escape any characters. – Léo Léopold Hertz 준영 Jul 17 '15 at 21:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .