Reputation: 519
I'm stuck. I need a script that will read my text document and insert custom text on the 34 character. This needs to happen for every line in my text document.
Example:
INPUT TEXT
`12345678912345678912345678912345 ABCD`
`12345678912345678912345678912345 EFGH`
`12345678912345678912345678912345 IJKL`
`12345678912345678912345678912345 MNOP`
OUTPUT TEXT
`12345678912345678912345678912345 custom text hereABCD`
`12345678912345678912345678912345 custom text hereEFGH`
`12345678912345678912345678912345 custom text hereIJKL`
`12345678912345678912345678912345 custom text hereMNOP`
I've provided below the script I can get working within Terminal. However, if there is a way to get it to work in Applescript so when I drop my file on the application it will prompt and ask what text I would like to insert (this would be "custom text here") and then upon hitting OK it would run.
If it isn't possible to do in Applescript, Automator would work too however, I cannot seem to get Automator to work with the script. Process for Automator is:
1. Get Specified Finder Items
2. Run Shell Script
This runs fine, but it does not change the document. Help?
Upvotes: 1
Views: 195
Reputation: 58928
In Bash you can use while read -r -N 34
to read 34 characters repeatedly. Untested example:
while IFS= read -r -N 34 || [[ -n "$REPLY" ]]
do
printf '%s' "$REPLY"
printf '%s\n' " custom text here"
done < input-file
Update: Looks like OP wants to discard characters after #34 on each line:
while IFS= read -r || [[ -n "$REPLY" ]]
do
printf '%s' "${REPLY:0:34}"
printf '%s\n' " custom text here"
done < input-file
UPDATE: OP here and I've figured out based on your wonderful knowledge everything I need to do!
while IFS= read -r || [[ -n "$REPLY" ]]
do
printf '%s' "${REPLY:0:34}"
printf '%s' " Faux_Folder/"
printf '%s\n' "${REPLY:36}"
done < input-file > output-file
Upvotes: 1
Reputation: 20980
Not sure if this work on OSX (works on ubuntu at least.):
sed -n -E '1h;2,$H;${g;s/.{34}/& custom text here\n/gp}' input.txt
Basically, it reads till the end of file & then inserts " custom text here\n" after every 34 characters.
Upvotes: 1