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!
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!
Google has ample resources for Bash Scripting.
Anyways here goes:
for i in {1..100}; do echo "`cat apple.txt`$i"; done
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
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).
seq --format "$(cat apple.txt)%02g" 1 100
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