0

I have a long list of urls, and I'm trying to make a shell script to split each paragraph, separated by newline, into its own file. The files would need to be numbered

001 002 003 ...

and so on. Here's what I tried

    #!/bin/zsh

    # Jakaa pitkä tiedoston kappaleet erilisiin tiedostoihin
    # Ei toimi.

    i=1
    while read p; do
        if [[ "$p" == "k" ]]; then
            i=$((i + 1))
            continue
        fi
        if (( $i < 10 )); then
            printf '%s\n' "$p" | tee -a kuvat_vol00"$i"
        elif (( $i > 9 )) && (( $i < 100 )); then
            printf '%s\n' "$p" | tee -a kuvat_vol0"$i"
        elif (( $i > 99 )); then
            printf '%s\n' "$p" | tee -a kuvat_vol"$i"
        fi
    done

I didn't know how to use the newline as delimiter, so that's why I use the letter 'k' instead. Stupid propably, I know...

The script works for a test file I made, something like:

123
k
123
k
sdfsdf
k
k
something

This gives me kuvat_001 and kuvat_002 with "123" and kuvat_003 with "sdfsdf" and so on. But when I try it with the urls, it just prints everything to a single file kuvat_001. I figured it has something to do with the slashes and other funny characters in the urls. How do I make it so that the shell doesn't interpret the special characters as special? Or should I use awk or script it in vim directly or something? I just don't know how to do either of those.

I'm a little bit ashamed to ask such a stupid question, but I've struggeled with this for like three days and I can't figure it out. I completely hands down suck at shell scripting, I know, but for some reason I'm excited to learn to use the computer properly. So yeah, I appreciate any help you can give me!

EDIT: The beginning of my input file looks like this. Can it be a problem with that?

https://2.bp.blogspot.com/l3Sk4TIKuKgEji-IJbxA7LetcDqWGbosjx-wSH8omLyNj4b1hq-Cs1wtFxd88XASw-FiJjjFSNbO=s1600
https://2.bp.blogspot.com/9GUUQ1HaC6m-4LbMKOZ7JLj55SqMfam9SHD6J48ezjGNY_hy8YUYYybhip_LKgYr9pKEI140Ewug=s1600
https://2.bp.blogspot.com/LYVJuq-I5kmAQUfLd_Kk28rJu1sOdo3md0ANHgS5w_wIVSIHSrwJAgO_MNm9DFg7GahrSYo4MFIp=s1600
https://2.bp.blogspot.com/qBITOIAnC09jmA-KDrFRyD3yGK_-2kNRyfr-AAJlitKTR8R7qzy8Q6v_1ukwZVBmIT8hBSRyinJE=s1600

https://2.bp.blogspot.com/9dwLT4h1pDRoOf0Mmp76zy04UGzPVEVTChNdTHAfqDPsb5fSSB2Tf4hFlFivNqPgplwKvdoEYNR0=s1600
https://2.bp.blogspot.com/D-9yBu7Zzg65mnfO2DuyU4aBcF9kAgllCJPpFuRKDrN5qUiuwJ9U-ReMhBg8b0Grg4PdcHlcmh2Y=s1600
https://2.bp.blogspot.com/RAUJka1fyc5yQAwh_O9bmqKj0vXgFvM9QvuIcKkItkxQiNFGgU1WUSBNqUQIGnnNpZaSzPHCyd8N=s1600
3

This can be done with lots of tools. I would reach for awk as it has the concept of paragraphs.

awk '
   BEGIN { RS="" }
   { F=sprintf("kuvat_%03d", NR) ; print > F ; close(F) }' input_file_name

set the RS variable to the null string to active paragraph mode. For each record (paragraph) print it to a file of the right name and close the file.

  • Thanks, that's way simpler than my solution. It still prints everything to the first file though. It must be a problem with my input file since it seems to work with any other file of similar form, so I couldn't have made a typo. – haarhuur Nov 9 '19 at 1:02
  • Maybe the "blank" lines are not blank? If it comes from a microsoft windows environment there may be CR characters at the end of each line, or maybe there are spaces? If you have the hexdump -C program then 0d is the CR and 0a is the LF which is the unix end of line character (assuming ASCII of course). – icarus Nov 9 '19 at 1:08
  • Aah yes, you're right! There's a CR in front of every LF. But if I type '/\r' in Vim, it doesn't find anything. Why is that? – haarhuur Nov 9 '19 at 1:35
  • Ah, found the solution here. Yes, now everything makes sense, thanks! – haarhuur Nov 9 '19 at 1:40
0

With GNU csplit utility:

  $ csplit \
      --suppress-matched -s \
       -f kuvat_vol_ -b %03d \
        file '/^$/' '{*}'  \
  ;

Using Perl:

$ perl -l -00pe '
    close F if $i;
    open F, ">", sprintf "kuvat_vol_%03d", $i++;
    select F;
 ' file 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.