grepping columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grepping columns
# 1  
Old 02-27-2008
grepping columns

Hi All,

I was recently helped out 'big time' with my last post on changing multiple file formats (thx, scott1256ca and bakunin)!

My new question is about selecting and displaying columns in a file using (possibly) grep. Several of my data files are spreadsheet format (columns separated by tabs). So for instance lets say I have four columns of data:

Quote:
#1 2 3 4
a b c d
a b c d
a b c d
a b c d
.
.
.
If I wanted to redirect only the 1st and 3rd column to a file, would this be possible with grep? Some other command or script?

Cheers,
ScKaSx
# 2  
Old 02-27-2008
Try...
Code:
awk 'BEGIN{FS=OFS="\t"}{print $1,$3}' file1 > file2

# 3  
Old 02-28-2008
cut command can capture the columns you wanted

try
Quote:
man cut
Quote:
cut -d"<delimiter - \t>" -f1,3 file1 > file2
# 4  
Old 02-28-2008
Hi Guys,

Thanks for the info:

Ygor - I got 'awk' to work on the "a_b_c_d file" but when I try it on a file with numbers it lists all the columns as opposed to just the ones I specify with '{print $1,$3}'.

Can anyone explain the awk command to me?

bobbygsk - I tried the cut command but I couldn't get it to work. It complains
Quote:
cut: bad delimiter
Cheers,
ScKaSx
# 5  
Old 02-28-2008
Post a snippet of your file with the numbers and the expected output.

Regards
# 6  
Old 02-28-2008
Hi,

Let's say I have the following file:

Code:
   5000.00     2.387       0     1704      19.1      27.2     0.350     0.350    0.0031     17      18      0.00
   3721.90     2.178       0     1003      23.5      33.9     0.350     0.350    0.0133     10      11      0.00
   2770.51     2.009       0      675      30.6      46.3     0.350     0.350    0.0021      7       8      0.00
   2062.31     1.884       0      983      36.8      51.7     0.350     0.350    0.0085     11      12      0.00
   1535.15     1.783       0      493      46.8      71.7     0.350     0.350    0.0070      6       7      0.00
   1142.73     1.710       0      438      51.9      86.1     0.350     0.350    0.0001      6       7      0.00
    850.63     1.662       1      245      46.0      80.6     0.350     0.350    0.0046      5       4      0.00

and I want it just to print out the 1st and 6th column (for instance):

Code:
   5000.00     27.2     
   3721.90     33.9     
   2770.51     46.3     
   2062.31     51.7     
   1535.15     71.7     
   1142.73     86.1     
    850.63     80.6

Also, I would prefer details on how to use the command as opposed to a solution to this problem, seeing how I will likely need to use it for other purposes.

Also is there an 'easy' way to rearrange columns?

Cheers,
ScKaSx

Last edited by ScKaSx; 02-28-2008 at 04:17 PM..
# 7  
Old 02-28-2008
The solution of Igor should work, just print the column in any order you prever, to print the 1st and the 6th column:

Code:
awk 'BEGIN{FS=OFS="\t"}{print $1,$6}' file1 > file2

Try it out without redirect the output to a file with different order and see how it works.

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grepping and replacing

Hey Friends, Need your help again. I have input.temp file as follows $cat input.temp Lakme|Beauty Products|Lipstick L'Oreal|Hair Care|Conditioner Lakme|Beauty Products|Lip gloss L'Oreal|Hair Care|Mild Shampoo Gala|Beauty Products|Mehndi Cones Lakme|Beauty Products|Eye Shadow... (2 Replies)
Discussion started by: anushree.a
2 Replies

2. Shell Programming and Scripting

Grepping more than one word

Dear Experts, Need your help. Typically we use "grep" to search and display a pattern in a txt file. However, here what we want is, we want to grep a line which contains 4 words any where in a line. For example. File has 10,000,000 lines in it out of which there is a particular line which... (1 Reply)
Discussion started by: anushree.a
1 Replies

3. UNIX for Dummies Questions & Answers

grepping a variable

I need to pass a parameter that will then be grepped. I need it to grep /paramater and then have a space so if 123 was passed my grep would be grep '/123 ' sample.log this works fine from the command line but when i try and set it searchThis="/$2 " and then run grep $searchThis... (6 Replies)
Discussion started by: magnia
6 Replies

4. UNIX for Dummies Questions & Answers

Help with grepping within variables

I've got a script at the moment that looks like this: if then echo "How many hours would you like users to have logged in for? (single digits)" read hours tim= echo "These are the users who were logged in for $hours... (1 Reply)
Discussion started by: chris_rabz
1 Replies

5. Shell Programming and Scripting

Please help on grepping

Hi All, I have a log file and I want to parse the logfile with a script.A sample text is shown below: I would grep on "return code " on this file. Any idea how the lines above and below the grep patterns could also be extracted. Thanks! nua7 The runLoggingInstall return code is 0... (3 Replies)
Discussion started by: nua7
3 Replies

6. Shell Programming and Scripting

Grepping issue..

I found another problem with my disk-adding script today. When looking for disks, I use grep. When I grep for the following disk sizes: 5242880 I also pick up these as well: 524288000 How do I specifically pick out one or the other, using grep, without resorting to the -v option? ... (9 Replies)
Discussion started by: LinuxRacr
9 Replies

7. Shell Programming and Scripting

grepping around

Using shell scripts, I use grep to find the word “error” in a log file: grep error this.log. How can I print or get the line 3 lines below the line that word “error” is located? Thanks in advance for your response. (9 Replies)
Discussion started by: cbeauty
9 Replies

8. UNIX for Dummies Questions & Answers

Grepping for strings

Hello. I have a dir of 1500+ dir. In these dirs is a file host, with a tag <x_tag>. I need to : 1. grep for all dir that contain this host file that contain <x_tag> 2. print a list of these host files containing <x_tag> is this better to egrep this? (5 Replies)
Discussion started by: t4st33@mac.com
5 Replies

9. UNIX for Dummies Questions & Answers

grepping for numbers in between

Ok here's the situation I have a log like the following: (S0) Time: 124937.326033 - SendingTime: '20041108-12:49:29' (S0) Time: 124937.389946 - SendingTime: '20041108-12:49:29' (S0) Time: 124937.462494 - SendingTime: '20041108-12:49:29' (S0) Time: 124937.551056 - ... (1 Reply)
Discussion started by: fusion99
1 Replies

10. UNIX for Dummies Questions & Answers

grepping

Is there a way to grep for something and then print out 10 lines after it. for example if I want to grep for a word, then output the following 10 or whatever number of lines after the word. (5 Replies)
Discussion started by: eloquent99
5 Replies
Login or Register to Ask a Question