0

I'm a novice at manipulating text in bash and appreciate any suggestions!

I have a variable used early in a script that is formatted like this:

runlist=echo 10,19,25,32

The actual numbers and how many numbers there are listed out differ on various iterations. They will always be 2 digits each. I need the variable in this format for the first step they are used for. But later in the script, I'd like to print out these numbers to a temporary text file in a single column like this

cat tmp_runlist.txt
10
19
25
32

I tried using IFS=',' but found that it also modified the runlist variable, which is used again towards the end of the script. I've been exploring using cut and awk but need something that can be flexible in just grabbing the characters between the commas and not taking the commas before the carriage return.

Thanks in advance for your ideas :)

Wintermute
  • 42,983
  • 5
  • 77
  • 80
JaYbirD
  • 3
  • 2

2 Answers2

2

I am going to assume that instead of

runlist=echo 10,19,25,32

you meant

runlist=$(echo 10,19,25,32)

...or just

runlist=10,19,25,32

In that case, the simplest way should be to use

echo "$runlist" | tr , '\n'

If it really is

runlist=echo 10,19,25,32

then you might use

echo "$runlist" | sed 's/^echo *//;s/,/\n/g'
Wintermute
  • 42,983
  • 5
  • 77
  • 80
1

grep -o might help:

s='10,19,25,32'
grep -o '[0-9]\+' <<< "$s"
10
19
25
32
anubhava
  • 761,203
  • 64
  • 569
  • 643