Search code examples
linuxgroovysplitmultiple-columnscut

How to get ordered, defined or all columns except or after or before a given column


In BASH I run the following one liner to get an individual column/field after splitting on a given character (one can use AWK as well if they want to split on more than one char i.e. on a word in any order, ok).

#This will give me first column i.e. 'lori' i.e. first column/field/value after splitting the line / string on a character '-' here
echo "lori-chuck-shenzi" | cut -d'-' -f1

# This will give me 'chuck'
echo "lori-chuck-shenzi" | cut -d'-' -f2

# This will give me 'shenzi'
echo "lori-chuck-shenzi" | cut -d'-' -f3

# This will give me 'chuck-shenzi' i.e. all columns after 2nd and onwards.
echo "lori-chuck-shenzi" | cut -d'-' -f2-

Notice the last command above, How can I do the same last cut command shit in Groovy?

For ex: if the contents are in a file and they look like:

1 - a
2 - b
3 - c
4 - d
5 - e
6 - lori-chuck shenzi
7 - columnValue1-columnValue2-columnValue3-ColumnValue4

I tried the following Groovy code, but it's not giving me lori-chuck shenzi (i.e. after ignoring the 6th bullet and first occurence of the -, I want my output to be lori-chuck shenzi and the following script is returning me just lori (which is givning me the correct output as my index is [1] in the following code, so I know that).

def file = "/path/to/my/file.txt"

File textfile= new File(file) 

//now read each line from the file (using the file handle we created above)
textfile.eachLine { line -> 
    //list.add(line.split('-')[1])
    println "Bullet entry full value is: " + line.split('-')[1]
}
// return list

Also, is there an easy way for the last line in the file above, if I can use Groovy code to change the order of the columns after they are split i.e. reverse the order like we do in Python [1:], [:1], [:-1] etc.. or in some fashion


Solution

  • I don't like this solution but I did this to get it working. After getting index values from [1..-1 (i.e. from 1st index, excluding the 0th index which is the left hand side of first occurrence of - character), I had to remove the [ and ] (LIST) using join(',') and then replacing any , with a - to get the final result what I was looking for.

    list.add(line.split('-')[1..-1].join(',').replaceAll(',','-'))

    I would still like to know what's a better solution and how can this work when we talk about cherry picking individual columns + in a given order (instead of me writing various Groovy statements to pick individual elements from the string/list per statement).