extract lines from txt file

Discussion in 'Mac Programming' started by vloryan, Aug 11, 2014.

  1. vloryan macrumors member

    Joined:
    Jan 11, 2014
    #1
    hi guys, using automator i would like to have my txt-file splitted in several txt files... this is what the original txt-file could look like:

    Code:
    this is line 1
    this is line 2
    this is line 3
    and finally line 4
    now, i would like to run an applescript that saves "this is line 1" to "example.txt" in the same folder the original txt-file is located, save "this is line 2" to "line2.txt" in the same folder... and so on.

    i hope somebody out there can help me... thanks!
     
  2. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    You should be able to do something like this with the do shell script action. The argument passed, should then be the selected file. This example is just the shell script that copies the first four lines into four text files.

    Code:
    #/bin/bash
    
    if [ $# != 1 ]
    then
        echo Missing Argument
        exit 1
    fi
    
    function get_line() {
        head -n "$1" "$2" | tail -n 1
    }
    
    for i in {1..4}
    do
        get_line $i "$1" > example"$i".txt
    done
    
    Keep in mind that any files called "example1-4.txt" will be overwritten by the most current files representing the first four lines in the selected file.
     
  3. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #3
    Please see my Automator workflow attached to this post.

    Runs without any errors but doesn't save each line in a seperate file?! Am i doing something wrong?

    Thanks!
     

    Attached Files:

  4. subsonix, Aug 11, 2014
    Last edited: Aug 11, 2014

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    You can test it by just picking selected Finder items, so no other actions than the 'do shell script' action. It should then create the file in the directory where you have selected the file. I'm not sure exactly what you are trying to achieve with the TextEdit and search option.

    There is also no need for the test for arguments (the first if/fi block) here since the script explicitly passes in arguments, that is, it can not run without a supplied argument.

    Edit: It seems like current working directory here defaults to ~/ which is your home directory. So your example1-4.txt files should be there. Not quite sure how to pass in the directory where the selection is done for now.
     
  5. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #5
    i have deleted the "OPEN FINDER ITEM" action and ran the workflow again but that doesn't work. sorry ;(
     
  6. subsonix, Aug 11, 2014
    Last edited: Aug 11, 2014

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    Remove both actions, only leave the shell script action, save as "service", it will be available in your context menu when you select a file. The result appears in your home directory. As I said, it seems to default to ~/ which means that the script doesn't run in the directory the selection is made, not sure how to fix this right now.

    Ok, I got an idea, we can get the dirname from the argument passed in, so the modified script looks like this:

    Code:
    DIR=$(dirname "$1")
    
    function get_line() {
        head -n "$1" "$2" | tail -n 1
    }
    
    for i in {1..4}
    do
        get_line $i "$1" > "$DIR"/example"$i".txt
    done
    
     
  7. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #7
    Got it now, sorry. Both scripts extract the lines seperately, but:
    not with the content ;( Looks like the hidden file header is extracted...
     

    Attached Files:

  8. subsonix, Aug 11, 2014
    Last edited: Aug 11, 2014

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #8
    Not sure what you are saying, the file DOES have content. Specifically:

    Code:
    example1.txt: {\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf210
    example2.txt: {\fonttbl\f0\fmodern\fcharset0 Courier;}
    example3.txt: {\colortbl;\red255\green255\blue255;\red234\green234\blue234;}
    example4.txt: \paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
    
    Edit: To clarify, this is what you requested in the first post:

    A .txt file has no formatting, it's pure text, so it has no header at all! Are you sure that this is in fact .txt files or is it something else. If it's something else, you need to specify exactly what it is you want to achieve.
     
  9. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #9
    that was a rtf-file, sorry. works now :)

    now, there is a big chance that number of lines vary from file to file. might be four, can be eight. any chance to edit the lines and check the number of lines first?

    that would be great! thanks so much!
     

Share This Page