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

孤人 提交于 2019-12-25 10:50:59

问题


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


回答1:


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).




回答2:


If I'm understanding your question correctly, what you want is:

line.split('-')[1..-1]

This will give you from position 1 to the last. You can do -2 (next to last) and so on, but just be aware that you can get an ArrayIndexOutOfBoundsException moving backwards too, if you go past the beginning of your array!

-- Original answer is above this line --

Adding to my answer, since comments don't allow code formatting. If all you want is to pick specific columns, and you want a string in the end, you could do something like:

def resultList = line.split('-')
def resultString = "${resultList[1]}-${resultList[2]} ${resultList[3]}"

and pick whatever columns you want that way. I thought you were looking for a more generic solution, but if not, specific columns are easy!

If you want the first value, a dash, then the rest joined by spaces, just use:

"${resultList[1]}-${resultList[2..-1].join(" ")}"

I don't know how to give you specific answers for every combination you might want, but basically once you have your values in a list, you can manipulate that however you want, and turn the results back into a string with GStrings or with .join(...).



来源:https://stackoverflow.com/questions/45336944/how-to-get-ordered-defined-or-all-columns-except-or-after-or-before-a-given-col

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!