0

i tried to split a file in parts of n matches each. The file is just one line and the seperator is '<br>'

foo<br>bar<br>.....<br>

I just want to split the file in parts, where each file has 100 datasets (text plus <br>)( normaly 100 datasets, but at the end maybe less)

I already played around with this ... split-file-in-2-with-sed and this split-one-file-into-multiple-files-based-on-pattern

sed.exe -e "^.*.<br>{0,100}/g" < original.txt > first_half.txt

The split do not work an the result is only 1 file instead of many.

2 Answers 2

0
awk -v n=100 -v RS="<br>" -v ORS="<br>" '++i % n == 0 {printf "\n"} 1' file

Test

$ printf "%d<br>" $(seq 100)
1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>21<br>22<br>23<br>24<br>25<br>26<br>27<br>28<br>29<br>30<br>31<br>32<br>33<br>34<br>35<br>36<br>37<br>38<br>39<br>40<br>41<br>42<br>43<br>44<br>45<br>46<br>47<br>48<br>49<br>50<br>51<br>52<br>53<br>54<br>55<br>56<br>57<br>58<br>59<br>60<br>61<br>62<br>63<br>64<br>65<br>66<br>67<br>68<br>69<br>70<br>71<br>72<br>73<br>74<br>75<br>76<br>77<br>78<br>79<br>80<br>81<br>82<br>83<br>84<br>85<br>86<br>87<br>88<br>89<br>90<br>91<br>92<br>93<br>94<br>95<br>96<br>97<br>98<br>99<br>100<br>

$ printf "%d<br>" $(seq 100) |
  awk -v n=10 -v RS="<br>" -v ORS="<br>" '++i % n == 0 {printf "\n"} 1'
1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>
10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>
20<br>21<br>22<br>23<br>24<br>25<br>26<br>27<br>28<br>29<br>
30<br>31<br>32<br>33<br>34<br>35<br>36<br>37<br>38<br>39<br>
40<br>41<br>42<br>43<br>44<br>45<br>46<br>47<br>48<br>49<br>
50<br>51<br>52<br>53<br>54<br>55<br>56<br>57<br>58<br>59<br>
60<br>61<br>62<br>63<br>64<br>65<br>66<br>67<br>68<br>69<br>
70<br>71<br>72<br>73<br>74<br>75<br>76<br>77<br>78<br>79<br>
80<br>81<br>82<br>83<br>84<br>85<br>86<br>87<br>88<br>89<br>
90<br>91<br>92<br>93<br>94<br>95<br>96<br>97<br>98<br>99<br>
100<br>
2
0

I get unknown command with sed on my RHEL machine. Probably because you are using Windows and sed.exe takes ^.*.<br>{0,100} as pattern for g command.

echo "foo<br>bar<br>...baz<br>" |  sed -e "^.*.<br>{0,100}/g"
sed: -e expression #1, char 1: unknown command: `^'

Are you trying to put each of the match to pattern space. I think that approach won't work because a matching like sed "/pattern/command" will be executed only once per line, so in total once in your case. And even if did separate the text differently you are redirecting the output to a single file.

Here is a simpler approach to replace all occurrences of
:

echo "foo<br>bar<br>...baz<br>" |  sed -e "s/<br>\{1,100\}/\n/g"
foo
bar
...baz

You can then use split to have each line in a different file

You must log in to answer this question.

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