1

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?

4

2 回答 2

1

不确定这是否适用于 OSX(至少适用于 ubuntu。):

sed -n -E '1h;2,$H;${g;s/.{34}/& custom text here\n/gp}' input.txt

基本上,它会一直读取到文件末尾,然后在每 34 个字符后插入“custom text here\n”。

于 2013-06-27T08:47:23.893 回答
1

在 Bash 中,您可以使用while read -r -N 34重复读取 34 个字符。未经测试的例子:

while IFS= read -r -N 34 || [[ -n "$REPLY" ]]
do
    printf '%s' "$REPLY"
    printf '%s\n' " custom text here"
done < input-file

更新:看起来 OP 想要在每行 #34 之后丢弃字符:

while IFS= read -r || [[ -n "$REPLY" ]]
do
    printf '%s' "${REPLY:0:34}"
    printf '%s\n' " custom text here"
done < input-file

更新:在这里,我已经根据您的精彩知识弄清楚了我需要做的一切!

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
于 2013-06-27T07:35:20.440 回答