1

I have the following string:

col1|col2|col3+++++++++++A|1|a B|2|b C|3|c D|4|d  (3 rows)

I want to split this string so as to remove the string in the beginning till the last + symbol and then remove the tail end which is (XYZ rows)

so the string becomes A|1|a B|2|b C|3|c D|4|d Now, I want to split this string into multiple arrays that look like this

A 1 a 
B 2 b 
C 3 c 
D 4 d

so that I can iterate over each row using for loop to do some processing.

How can I do this using sed or grep?

I tried this for the first pass but it didn't work

echo $string | sed 's/([0-9])rows//' | sed 's/[^+]//'

but it didn't work.

1

By variable expansion in bash:

str="col1|col2|col3+++++++++++A|1|a B|2|b C|3|c D|4|d  (3 rows)"
str=${str%  (*}
str=${str##*+}
str=${str// /
}
str=${str//|/ }

Or by sed

sed 's/.*+\(.*\S\)\s\+(.*/\1/;y/ |/\n /' <<\eof
col1|col2|col3+++++++++++A|1|a B|2|b C|3|c D|4|d  (3 rows)
eof
A 1 a
B 2 b
C 3 c
D 4 d
2
echo "col1|col2|col3+++++++++++A|1|a B|2|b C|3|c D|4|d  (3 rows)" |sed -e 's/.*\++//; s/d .*/d/; s/|\(.\)|\(.\)\s*/ \1 \2\n/g'

A 1 a
B 2 b
C 3 c
D 4 d

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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