Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

vloryan

macrumors member
Original poster
Jan 11, 2014
57
0
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!
 
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.
 
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!
 

Attachments

  • Schnappschuss (2014-08-11 15.51.17).png
    Schnappschuss (2014-08-11 15.51.17).png
    100.9 KB · Views: 193
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?

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.
 
Last edited:
i have deleted the "OPEN FINDER ITEM" action and ran the workflow again but that doesn't work. sorry ;(
 
i have deleted the "OPEN FINDER ITEM" action and ran the workflow again but that doesn't work. sorry ;(

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
 
Last edited:
Got it now, sorry. Both scripts extract the lines seperately, but:
not with the content ;( Looks like the hidden file header is extracted...
 

Attachments

  • example1.txt
    52 bytes · Views: 340
  • example2.txt
    41 bytes · Views: 340
  • example3.txt
    63 bytes · Views: 316
  • example4.txt
    76 bytes · Views: 317
Got it now, sorry. Both scripts extract the lines seperately, but:
not with the content ;( Looks like the hidden file header is extracted...

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:

hi guys, using automator i would like to have my txt-file splitted in several txt files...

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.
 
Last edited:
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.