Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
    
shortest solution echo ; seq -f<apple.txt%g 100 –  ThorstenS Mar 28 '12 at 9:58
    
Is the leading blank line relevant? –  glenn jackman Mar 28 '12 at 13:12

5 Answers 5

up vote 2 down vote accepted

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).

share|improve this answer
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 8: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 8:30
    
Bash isn't POSIX or SUS either :) –  adaptr Mar 28 '12 at 8: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 8:41
    
See my answer for the most efficient bash solution. –  adaptr Mar 28 '12 at 8:44

Google has ample resources for Bash Scripting.

Anyways here goes:

for i in {1..100}; do echo "`cat apple.txt`$i"; done
share|improve this answer
2  
bad idea to cat 100 times :-/ –  ThorstenS Mar 28 '12 at 8:15
1  
@ThorstenS is right: for i in $(< apple.txt){1..100}; do echo "$i"; done –  glenn jackman Mar 28 '12 at 13:14

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

share|improve this answer
    
seq -f"$(<apple.txt)%g" 100 –  Iain Mar 28 '12 at 7:58
    
Solution destroyed almost instantly. Nice! –  Matt Eckert Mar 28 '12 at 8:00
    
Quotes are unnecessary! seq -f$(<apple.txt)%g 100 –  Matt Eckert Mar 28 '12 at 8:02
    
24chars: seq -f<apple.txt%g 100 –  ThorstenS Mar 28 '12 at 8:10
    
@ThorstenS I can't think of anything shorter. –  Matt Eckert Mar 28 '12 at 8:36
seq --format "$(cat apple.txt)%02g" 1 100
share|improve this answer
    
ok: seq -f "$(cat apple.txt)%02g" 1 100 –  ThorstenS Mar 28 '12 at 7:18
    
Very short, though no initial blank line, and not the number format the OP asked for in the 1-9 range. And yaay you for using seq! –  MadHatter Mar 28 '12 at 7:24
    
mea culpa- here it goes: echo -e; seq --format "$(cat apple.txt)%g" 1 100 –  ThorstenS Mar 28 '12 at 7:57
    
<grin> no worries, i missed it first time around, too! +1 for you! –  MadHatter Mar 28 '12 at 8:02
    
thank you Tom =) –  ThorstenS Mar 28 '12 at 8:13

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
share|improve this answer
    
I think you missed the first (empty) line. –  joechip Mar 28 '12 at 21:50
    
I see nothing that requires one. –  adaptr Mar 29 '12 at 12:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.