2

A bash script writes its output, which is a list of files, into a file.

file.txt:

/home/user/dir1/dir2/foo00
/home/user/dir1/dir2/foo01
/home/user/dir1/dir2/foo02

I want to prepend a letter to each line, starting with a, and after having reached z, going on with aa, ab…

in the end, the output should look like

file.txt:

a /home/user/dir1/dir2/foo00
b /home/user/dir1/dir2/foo01
c /home/user/dir1/dir2/foo02
...
z /home/user/dir1/dir2/foo26
aa /home/user/dir1/dir2/foo27

Being a newbie in shell scripting, I have no clue, which tool may be appropriate. I So my question keeps surely somewhat imprecise.

I'd prefer bash built ins, if possible.

How can I do this operation?

manes
  • 119
  • 1
  • 1
  • 9

2 Answers2

3

Using only bash builtins, without subshells:

prefixes=({a..z}  {a..z}{a..z}  {a..z}{a..z}{a..z})
i=0
while IFS= read -r line
do
  printf "%s %s\n" "${prefixes[i++]}" "$line"
done < file.txt
that other guy
  • 116,971
  • 11
  • 170
  • 194
1

This isn't bash, but perl's built-in incrementing can do what you want:

#!/usr/bin/env perl
my $pfx = 'a';
print $pfx++, ' ', $_ while (<>);

This script reads from stdin and produces the output you seek.

Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46