Cut file using regular expressions


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cut file using regular expressions
# 1  
Old 04-27-2009
Cut file using regular expressions

I have a file with approximately 262,000 fields and I want to split it according to pairs of fields. The fields have headers and I want to create smaller files with just the columns between the fields (specified fields inclusive).

For example, I just want the columns "set" and "test", with the columns in between (which turn out to be just, the "id" column):

probe set id test test1
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

to get:

set id test
2 3 4
2 3 4
2 3 4
2 3 4
2 3 4
2 3 4


I know this would be easy with the "cut" command if I knew the field numbers, but I only know the field names, so I have to cut according to the field name.

Suggestions are much appreciated.

Last edited by etownbetty; 04-27-2009 at 09:43 PM..
# 2  
Old 04-27-2009
Try this

Did you try


cat filename |cut -d" " -f2,3,4


The delimiter is " " a space, and fields are the columns for which you wish to select

Quote:
Originally Posted by etownbetty
I have a file with approximately 262,000 fields and I want to split it according to pairs of fields. The fields have headers and I want to create smaller files with just the columns between the fields (specified fields inclusive).

For example, I just want the columns "set" and "test", with the columns in between (which turn out to be just, the "id" column):

probe set id test test1
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

to get:

set id test
2 3 4
2 3 4
2 3 4
2 3 4
2 3 4
2 3 4


I know this would be easy with the "cut" command if I knew the field numbers, but I only know the field names, so I have to cut according to the field name.

Suggestions are much appreciated.
# 3  
Old 04-27-2009
Quote:
Originally Posted by etownbetty
I have a file with approximately 262,000 fields and I want to split it according to pairs of fields. The fields have headers and I want to create smaller files with just the columns between the fields (specified fields inclusive).
...
I know this would be easy with the "cut" command if I knew the field numbers, but I only know the field names, so I have to cut according to the field name.
...

Another one:

Code:
awk  'NR==1{ for(i=1;i<=NF;i++) ($i==s) ? n=i : ($i==e) ? m=i : l }
           { for(i=n;i<=m;i++) printf $i FS; print l}' s="set" e="test"  file


Use gawk if you can ( mawk fails - max no. of fields 32767 ), on Solaris use nawk.

Last edited by rubin; 04-27-2009 at 11:21 PM.. Reason: removed extra {}
# 4  
Old 04-28-2009
Thank you!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expressions

I need to pick a part of string lets stay started with specific character and end with specific character to replace using sed command the line is like this:my audio book 71-skhdfon1dufgjhgf8.wav' I want to move the characters beginning with - end before. I have different files with random... (2 Replies)
Discussion started by: XP_2600
2 Replies

2. Shell Programming and Scripting

Regular Expressions

Hi Ilove unix and alwyas trying to to learn unix,but i am weak in using regular expressions.can you please give me a littel brief discription that how can i understand them and how to use .your response could lead a great hand in my unix love. (1 Reply)
Discussion started by: manoj attri
1 Replies

3. Shell Programming and Scripting

Regular Expressions

I am new to shell scripts.Can u please help me on this req. test_user = "Arun" if echo "test_user is a word" else echo "test_user is not a word" (1 Reply)
Discussion started by: chandrababu
1 Replies

4. Shell Programming and Scripting

Help with regular expressions

I have a file that I'm trying to find all the cases of phone number extensions and deleting them. So input file looks like: abc x93825 def 13234 x52673 hello output looks like: abc def 13234 hello Basically delete lines that have 5 numbers following "x". I tried: x\(4) but it... (7 Replies)
Discussion started by: pxalpine
7 Replies

5. Shell Programming and Scripting

Regular expressions help

need a regex that matches when a number has a zero (0) at the end of it so like 10 20 120 30 330 1000 and so on (6 Replies)
Discussion started by: linuxkid
6 Replies

6. UNIX for Dummies Questions & Answers

Regular expressions

In regular expressions with grep(or egrep), ^ works if we want something in starting of line..but what if we write ^^^ or ^ for pattern matching??..Hope u all r familiar with regular expressions for pattern matching.. (1 Reply)
Discussion started by: aadi_uni
1 Replies

7. UNIX for Advanced & Expert Users

regular expressions

I have a flat file with the following drug names Nutropin AQ 20mg PEN Cart 2ml Norditropin Cart 15mg/1.5ml I have to extract digits that are before mg i.e 20 and 15 ; how to do this using regular expressions Thanks ram (1 Reply)
Discussion started by: ramky79
1 Replies

8. UNIX for Advanced & Expert Users

Regular Expressions

Hi, below is a piece of code written by my predecessor at work. I'm kind of a newbie and am trying to figure out all the regular expressions in this piece of code. It is really a tough time for me to figure out all the regular expressions. Please shed some light on the regular expressions... (3 Replies)
Discussion started by: ramky79
3 Replies

9. Shell Programming and Scripting

regular expressions

Hello, Let say I have a string with content "Free 100%". How can extract only "100" using ksh? I would this machanism to work if instead of "100" there is any kind of combination of numbers(ex. "32", "1238", "1"). I want to get only the digits. I have written something like this: ... (4 Replies)
Discussion started by: whatever
4 Replies

10. Shell Programming and Scripting

Regular Expressions

How can i create a regular expression which can detect a new line charcter followed by a special character say * and replace these both by a string of zero length? Eg: Input File san.txt hello hi ... (6 Replies)
Discussion started by: sandeep_hi
6 Replies
Login or Register to Ask a Question