Cutting specific columns from lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cutting specific columns from lines
# 1  
Old 04-17-2015
Cutting specific columns from lines

I am trying to remove columns 81-97 from a line that can be as long as 114 characters. Because a number of lines might not have under 80 characters, using the cut command following by paste could be a problem. While sed might work, is there some other utility that could do this more easily?

Thanks in advance.
# 2  
Old 04-17-2015
A few systems still have utilities which can't handle lines longer than 1024 characters, but 114 characters isn't a problem for any utility I know of.

Are columns 81-97 the last columns? Removing trailing columns is trivial in awk, just set the number of fields.

Code:
awk '{ NF=80 } 1' inputfile > outputfile

# 3  
Old 04-17-2015
Thank you, but this isn't the last column. If the line length is < 80 I don't mess with it but I need to keep from col 98 to the end on longer lines. The field isn't needed and copying these data to another application causes a line wrap.
# 4  
Old 04-17-2015
A little trickier but awk can still handle it.
Code:
awk 'length($0) > 80 { # Only run this code block on lines longer than 80 chars
        # Save columns 98 - end of line
        S="" ; for(N=98; N<=NF; N++) S=S " " $N
        # Strip off all columns past 80 from line
        NF=80
        # paste columns 98 - ... onto end of line
        $0=$0 S
} 1' input > output

# 5  
Old 04-17-2015
Thanks for trying but that script produced an exact copy on the input.
# 6  
Old 04-17-2015
Quote:
Originally Posted by wbport
Thanks for trying but that script produced an exact copy on the input.
High probability that it would as we don't know what the file looks like AND what your field separator is/are....
# 7  
Old 04-17-2015
I'm not sure I understand what you're trying to do, but if I'm guessing correctly, try:
Code:
awk '{print substr($0, 1, 80) substr($0, 98)}' file

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.
This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Printing lines with specific strings at specific columns

Hi I have a file which is tab-delimited. Now, I'd like to print the lines which have "chr6" string in both first and second columns. Could anybody help? (3 Replies)
Discussion started by: a_bahreini
3 Replies

2. Shell Programming and Scripting

Cutting rows at specific length

Hi, i have a file containing nrows and 3cols. i want to cut it in specific length and save output to individual files. 1 2 3 4 5 6 5 8 9 10 11 12 13 14 15 16 17 18 i need to cut the file say every 2 rows and save it in individual file. 01.dat contains 1 2 3 4 5 6 02.dat 7 8 9... (10 Replies)
Discussion started by: ida1215
10 Replies

3. UNIX for Dummies Questions & Answers

sort comma separated lines by specific columns

Hello, I have a file which lines' words are comma separated: aa, bb, cc, uu b, ee, ff bb, cc, zz, ee, ss, kk oo, bb, hh, uu a, xx, ww tt, aa, dd, yy aa, gg I want to sort first by second column and in case of tie by fourth column with sort command. So the output would be: ... (4 Replies)
Discussion started by: asanchez
4 Replies

4. UNIX for Dummies Questions & Answers

cutting columns if delimiter has more than one charecter (|^)

Hi All, I am having a file with the delimiter '|^'. File name:test_dlim.csv I want to cut the first field of this using awk command. I tried with the help of the following link:... (2 Replies)
Discussion started by: boopathyvasagam
2 Replies

5. Shell Programming and Scripting

Selecting specific 'id's from lines and columns using 'SED' or 'AWK'

Hello experts, I am new to this group and to 'SED' and 'AWK'. I have data (text file) with 5 columns (C_1-5) and 100s of lines (only 10 lines are shown below as an example). I have to find or select only the id numbers (C-1) of specific lines with '90' in the same line (of C_3) AND with '20' in... (6 Replies)
Discussion started by: kamskamu
6 Replies

6. Shell Programming and Scripting

Cutting a file with multiple delimiters into columns

Hi All I have recently had to start using Unix for work and I have hit brick wall with this prob.... I have a file that goes a little something like this.... EUR;EUR;EUR:USD:USD;USD;;;EUR/USD;XAU/AUD;XAU/EUR;XAU/AUD,GBP/BOB,UAD/XAU;;;1.11;2.22;3.33;4.44;5.55;6.66;;; is it possible to... (7 Replies)
Discussion started by: luckycharm
7 Replies

7. Shell Programming and Scripting

Cutting specific lines from a file

Hi, I have a file named Mani.txt. The contents are like this cat Mani.txt -------------------------------------------------------- Hi there how r u My Name is Mani Bye ------------------------------------------------------------ I want to cut the first and last lines from the file... (15 Replies)
Discussion started by: pathanjalireddy
15 Replies

8. Shell Programming and Scripting

Cutting columns starting at the end of each line...

Hi Guys, Can you help me with a sed or a csh script that will have an output from the input below. Cutting the columns starting from the end of the line and not from the start of the line? Sample1 - The underscore character "_" is actually a space...i need to put it as underscore here coz... (2 Replies)
Discussion started by: elmer1503
2 Replies

9. Shell Programming and Scripting

Cutting Columns and Moving in to a file

Guys, Can any one tell me how can we cut the columns and move each column in to a separate file using awk? I have a tab delimited file as shown below, 1213 wattt werree 2345 skhasdjh aasas I want to output this in to three files named a.txt,b.txt and c.txt say a.txt... (3 Replies)
Discussion started by: Serious Sam
3 Replies

10. UNIX for Dummies Questions & Answers

cutting columns if delimiter has more than one charecter

Hi, My file looks like abc$%sdfhs$%sdf$%sdfaf$% here as seen delimiter is $%...now how cas i take out second field as cut command expect delimiter as single charecter only.....is there is any other way thanks and regards mahabunta (9 Replies)
Discussion started by: mahabunta
9 Replies
Login or Register to Ask a Question