Question

I have a file where each line is a list of tuples the following format:

[('a1','b1','c1','d1','e1'), ('a2','b2','c2','d2','e2'), ..., ('an','bn','cn','dn','en')]

n, meaning the list length could be different for each line.

My goal is to extract the first member of each tuple, meaning, I want the following output for each line:

a1 a2 ... an

I would know how to do this if the number of tuples was known, but I can't generalize. Thanks!

Was it helpful?

Solution

You can use sed:

sed -E "s/[^(]*\('([^']+)'[^(]*/\1 /g; s/ $//" < tuples.txt

Which will produce:

a1 a2 an
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top