Let's say you have a tab delimited file that looks like this:
temp.txt
field1 field2 field3 field4 field5 field6
field1 field2 field3 field4 field5 field6
field1 field2 field3 field4 field5 field6
starting the next one will delete field 3 and 4 and be displayed at the end of the line.
awk '{print $1"\t"$2"\t"substr($0, index($0,$5))}' temp.txt
field1 field2 field5 field6
field1 field2 field5 field6
field1 field2 field5 field6
My example is print to stdout. > newFile will send stdout to newFile, and >> newFile will add newFile.
Therefore, you can use the following:
awk '{print $1"\t"$2"\t"substr($0, index($0,$5))}' temp.txt > newFile.txt
some will argue that cut
cut -f1,2,5- temp.txt
which produce the same output, and the slice is great for simplicity, but does not handle conflicting delimiters. For example, a mixture of different spaces. However, in this case, cut may be what you need.
you can also do this in perl, python, ruby ββand many others, but here is the simplest awk solution.
matchew
source share