0

I am trying to treat a series of letters as an array in order to loop through each letter in a for loop, line by line, element by element.

My $data after extracting a column looks something like:

ACACACACACA
CGCGCGCGGGG
TATATATAAAA
GAGAAAGAAGG
TGGTTTTGGTG

My script:

for j in `cat $data` # go line by line
do
    for k in "${j[@]}" # go through each letter element by element
    do
        echo $k
    done
done

Gives the output:

ACACACACACA
CGCGCGCGGGG
TATATATAAAA
...

I'd like the output to look like:

A
C
A
C
A
C
...
G
T
G

Is there something wrong with my syntax? Or is there a better way to call each element of the line?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
anita
  • 177
  • 1
  • 9
  • Not sure on the `for` loop here, but you could just do `cat $data | sed 's/./&\n/g'` to get this output. Or with `awk`: `cat $data | awk 'BEGIN{FS="";OFS="\n"}{$1=$1}1'` – JNevill Oct 02 '18 at 18:50
  • It should definitely be 'echo' and not 'cat'. Otherwise, that command is going to try to cat a file named whatever is in your data which is obviously not what you want. – JasonK Oct 02 '18 at 18:55
  • `tr -d '\n' < "$data" | sed 's/./&\n/g'` – Cyrus Oct 02 '18 at 18:59
  • `echo ACACACACACA | awk 'BEGIN{FS=""}{for (i=1;i<=NF;i++){print $i}}'` might give you a clue. Good luck. – shellter Oct 02 '18 at 19:21

3 Answers3

1

Assuming it's always uppercase letters:

$ sed 's/[A-Z]/&\n/g' inputFile

or use the following to replace any character and number with the same character or number followed by a newline.

$ sed 's/./&\n/g' inputFile

OR if you have fold available,

$ fold -w1 inputFile

fold wraps line. With -w1 option, it wraps to a fix width of 1.

iamauser
  • 11,119
  • 5
  • 34
  • 52
  • `echo "ACGT" | sed 's/[A-Z]/&\n/g'` gives me `AnCnGnTn` and `echo "ACGT" | sed 's/./&\n/g'` gives me `AnCnGnTn` as well. Doesn't seem to work for me? – anita Oct 02 '18 at 21:39
1

It's not pure bash, but this gets you what you're after:

for j in `echo $data | grep -o .`
do
  echo $j
done

The grep command matches every single character and prints it out (or in this case, stores it in the variable j where I assume you intend to do more than just echo it).

JasonK
  • 535
  • 4
  • 10
  • For my edification, could you explain why `echo $data | grep -o .` works but not `echo $data | grep -o` (no period)? – anita Oct 02 '18 at 21:36
  • 1
    @anita That\`s because `grep` needs a pattern to match against. When you omit the `.` (match any character), you leave `grep` unsure what to search for. – hidefromkgb Oct 02 '18 at 21:45
1

The shortest option:

fold -1 <<<"$data"
hidefromkgb
  • 5,834
  • 1
  • 13
  • 44