Getting cut to ignore cols in middle of records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting cut to ignore cols in middle of records
# 1  
Old 11-14-2017
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?
Code:
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
# 2  
Old 11-14-2017
could you provide a sample input and a desired output, please!
# 3  
Old 11-14-2017
Quote:
Originally Posted by wbport
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?
Code:
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
Doesn't this work?
Code:
grep -v "T$" filename |egrep -v "^$" |sort | cut -c 1-78,80

Could the two greps also be condensed into
Code:
egrep -v 'T$|^$' filename ...

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 4  
Old 11-14-2017
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.
This User Gave Thanks to wbport For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk/sed/cut to filter out records from a file based on criteria

I have two files and would need to filter out records based on certain criteria, these column are of variable lengths, but the lengths are uniform throughout all the records of the file. I have shown a sample of three records below. Line 1-9 is the item number "0227546_1" in the case of the first... (15 Replies)
Discussion started by: MIA651
15 Replies

2. Shell Programming and Scripting

Bitwise comparison of cols

Hello, I want to compute the bitwise number of matches in pairwise fashion for all columns. The problem is I have 18486955 rows and 750 columns. Please help with code, I believe this will take a lot of time, is there a way of tracking progress? Input Org1 Org2 Org3 A A T A ... (9 Replies)
Discussion started by: ritakadm
9 Replies

3. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

4. Shell Programming and Scripting

awk -- print combinations for 2 cols

Dear all, could you please help me with awk please? I have such input: Input: a d b e c f The number of lines is unknown before reading the file. I need to print possible combination between the two columns like this: Output: a d b d c d a e b e c e a f (2 Replies)
Discussion started by: irrevocabile
2 Replies

5. Shell Programming and Scripting

Ignore records with no values through awk

Hi Guys, Hope you are doing well out there. I have to format the output of a script. Current output is auktltbr.dc-dublin.de:4322 ICCIR2Test13-PB-01 active auktltbr.dc-dublin.de:8322 ICCIR2Test13-SB-02 active auktlttr.dc-dublin.de:4422 ICCIR2Test24-CB-02 active... (10 Replies)
Discussion started by: singh.chandan18
10 Replies

6. Programming

Curses not updating LINES/COLS

I'm working with an extremely outdated and old system at work. We do not have ncurses, but we do have curses. I need to make a user interface for users connecting with xterm. One issue I've encountered is if the user resizes the window, I'd like to provide functionality to redraw the screen with... (4 Replies)
Discussion started by: nwboy74
4 Replies

7. UNIX for Dummies Questions & Answers

Cut specific fields from a file containing multiline records

Hi, I am looking for a method to get column13 to column 50 data from the 1st line of a multiline reord. The records are stored in a large file and are separated by newline. sample format is (data in red is to be extracted) <header> A001dfhskhfkdsh hajfhksdhfjh... (3 Replies)
Discussion started by: sunayana3112
3 Replies

8. Shell Programming and Scripting

How to find number of Cols in a file ?

Hi I have a requirement wherein the file is comma separated. Each records seems to have different number of columns, how I can detect like a row index wise, how many columns are present ? Thanks in advance. (2 Replies)
Discussion started by: videsh77
2 Replies

9. Shell Programming and Scripting

Grouping matches by cols

Dear all I have a large file w. ~ 10 million lines. The first two cols have matching partners. For example: A A A B B B or A A B A B B The matches may be separated by an unknown number of lines. My intention is to group them and add a "group" value in the last col. For... (12 Replies)
Discussion started by: gbalsu
12 Replies

10. Shell Programming and Scripting

cut specific records from a file

I am trying to cut the first 10 characters from a file only if the file has 'xyz' in field 185-188. I tried this cat filename | cut -c1-10 but this gives me all the records regardless of what is in field 185-188. Is this doable ? Thanks in advance for responses. (2 Replies)
Discussion started by: jxh461
2 Replies
Login or Register to Ask a Question