Dear all,
I am processing a large set of text files that I want to split in half by columns. What I have works, but I'm keen to learn a cleaner better way of approaching this issue.
The text files look somewhat like the following:
Code:
ab cd ef gh
ij kl mn op
I want to take the first two elements in each line and dump them into a new file. What I have come up with is this:
Code:
for i in {1..2} ; do head -n $i text | tail -n 1 | awk -F" " '{ for (i=1; i<=2; i++) { printf "%s ", $i } }' | sed -e 's/\s$/\n/g'; done
Essentially I loop over the file taking one line at a time with head and tail, pass the line to awk where I grab the first two elements, and lastly I replace the last space in the line with a newline character.
The output looks like this:
It does what it is supposed to, but I have the feeling that there must be a better solution to my problem.
Thanks!