1

Can you help me with a unix script to get result like this in file:


apple1
apple2
apple3

etc.. till 100

from a file apple.txt which contains only one word - apple.

Thank you in advance!

aschuler
  • 175
  • 3
  • 12
ampersand8
  • 13
  • 2

5 Answers5

3

Google has ample resources for Bash Scripting.

Anyways here goes:

for i in {1..100}; do echo "`cat apple.txt`$i"; done
Shyam Sundar C S
  • 1,063
  • 8
  • 12
2

Here's a solution in AWK:

awk '{ while (count++ < 100) print $0 count }' apple.txt > output.txt

Bonus seq-golf solution (29 characters):

seq -f"$(pg apple.txt)%g" 100

seq starts at 1 by default, so there is no need to specify the FIRST parameter. The example output does not pad the numbers, so the format can be reduced. From the looks of the markup, the blank line in the example output is probably a formatting error.

Edit (25 characters): seq -f$(<apple.txt)%g 100

Matt Eckert
  • 156
  • 4
2

While we're all piling in with ideas, here's mine, with my long-time favourite under-used command, seq:

echo "" ; apple=`cat /tmp/apple.txt` ; seq 1 100 | sed -e "s/^/$apple/"

The echo at the beginning provides your intial blank line, which I fear some posters may be forgetting about (though their solutions are perfectly good, and could all be be easily fixed, hint hint).

MadHatter
  • 78,442
  • 20
  • 178
  • 229
  • 2
    `seq` is not Unix, UUoC, sed use for no good reason. This is pretty much the WORST of all possible answers. – adaptr Mar 28 '12 at 08:29
  • Forgive me, but in what sense is `seq` not UNIX, if `bash` is? It's part of the GNU coreutils set of tools, which includes other things that I personally regard as absolutely core UNIX tools (`rm`? `mv`?). I agree that I could have done without `sed` if I'd known about `seq`'s format option (see ThorstenS's excellent answer), but I didn't, so I'm learning from this whole thing, too. I can't quite see how I could have loaded the file into the variable without cat, so may I just add that I'm interested to see how you're going to do this without using `bash`, `sh`, or `seq`? – MadHatter Mar 28 '12 at 08:30
  • Bash isn't POSIX or SUS either :) – adaptr Mar 28 '12 at 08:38
  • True, but the OP didn't ask for POSIX compliancy, only for a UNIX solution. If you'd like to ask a new question that requires POSIX, we'll see what we can do! – MadHatter Mar 28 '12 at 08:41
  • See my answer for the most efficient bash solution. – adaptr Mar 28 '12 at 08:44
1
seq --format "$(cat apple.txt)%02g" 1 100
Lucas Kauffman
  • 16,818
  • 9
  • 57
  • 92
ThorstenS
  • 3,084
  • 18
  • 21
0

If bash is available, the following uses no external commands:

( read -r t; for i in {1..100}; do echo "$t$i" >> outfile; done ) < apple.txt
adaptr
  • 16,479
  • 21
  • 33