Getting cut to ignore cols in middle of records

I recently had to remove a number of columns from a sorted copy of a file, but couldn't get the cut command to take fields out, just what to keep. This is the only thing I could find as an example, but could it be simplified?

tstamp=`date +%H%M%S`
grep -v "T$" filename |egrep -v "^$" |sort 2>/dev/null >/usr/tmp/aaa.x$tstamp
cut -c 1-78 /usr/tmp/aaa.x$tstamp > /usr/tmp/bbb.x$tstamp
cut -c 80 /usr/tmp/aaa.x$tstamp > /usr/tmp/ccc.x$tstamp
paste -d '\0' /usr/tmp/bbb.x$tstamp /usr/tmp/ccc.x$tstamp
rm /usr/tmp/*.x$tstamp

Could this be reduced to just one cut (or some other) command? The above just took out one character so everything would appear on one line, but I had to take out much more than that.

TIA

could you provide a sample input and a desired output, please!

Doesn't this work?

grep -v "T$" filename |egrep -v "^$" |sort | cut -c 1-78,80 

Could the two greps also be condensed into

egrep -v 'T$|^$' filename ...

Andrew

1 Like

I didn't realize that cut could deliver several field ranges. In the file I was working on, I needed to take out columns 5-57 and the last column I needed was 76. Specifying just the columns I needed to keep, "cut -b 1-6,58-76" did the job.

Thank you again.

1 Like