Grep/awk part of info of a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep/awk part of info of a column
# 1  
Old 12-06-2013
Grep/awk part of info of a column

I have a question that I am at a loss to solve. I have 3 column tab-separated data, such as:

Code:
abs nmod+n+n-commitment-n   349.200023 
abs nmod+n+n-a-commitment-n 333.306429 
abs into+ns-j+vn-pass-rb-divide-v   295.57316 
abs nmod+n+ns-commitment-n  182.085018 
abs nmod+n+n-pledge-n   149.927391
abs nmod+n+ns-reagent-n 142.347358

I need to isolate the last two "elements" of the third column, in which my desired result would be a 4-column output that only contains those elements that end with "-n".
such as:
Code:
abs nmod+n+n   commitment-n   349.200023
abs nmod+n+n-a   commitment-n 333.306429 
abs nmod+n+ns   commitment-n  182.085018 
abs nmod+n+n   pledge-n   149.927391
 abs nmod+n+ns   reagent-n 142.347358

.

In this case, is there an awk, grep anything that can help? The files are approx. 500 MB, so they are not huge, but not small either. Thanks for any insight.
# 2  
Old 12-06-2013
Try
Code:
awk '$2~/-n$/ {sub (/-/," ", $2); print}' file
abs nmod+n+n commitment-n 349.200023
abs nmod+n+n a-commitment-n 333.306429
abs nmod+n+ns commitment-n 182.085018
abs nmod+n+n pledge-n 149.927391
abs nmod+n+ns reagent-n 142.347358

EDIT: I see an error in line 2. Let me think...

---------- Post updated at 12:55 ---------- Previous update was at 12:38 ----------

This may be more adequate:
Code:
 awk '$2~/-n$/ {sub (/-[^-]*-n$/," &", $2); $0=$0; sub (/^-/,"",$3); print}' file
abs nmod+n+n commitment-n 349.200023
abs nmod+n+n-a commitment-n 333.306429
abs nmod+n+ns commitment-n 182.085018
abs nmod+n+n pledge-n 149.927391
abs nmod+n+ns reagent-n 142.347358


Last edited by RudiC; 12-06-2013 at 07:52 AM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-06-2013
Try :

Code:
$ awk '$2~/-n$/{j=0;for(i=length($2);i>=1;i--){if(substr($2,i,1)~/\-/){++j}if(j>1)break};$2 = substr($2,1,i-1) FS substr($2,i+1);print}' file

Code:
abs nmod+n+n commitment-n 349.200023
abs nmod+n+n-a commitment-n 333.306429
abs nmod+n+ns commitment-n 182.085018
abs nmod+n+n pledge-n 149.927391
abs nmod+n+ns reagent-n 142.347358

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

2. Shell Programming and Scripting

Use grep/awk to remove part of column

hi all, how can i use grep or awk to clean the following input data: n<>the<>96427210 861521305 123257583 n<>obj<>79634223 861521305 79634223 n<>nmod<>68404733 861521305 68422718 where the desired results is to remove all non-numeric characters?: 96427210 861521305 123257583 ... (5 Replies)
Discussion started by: owwow14
5 Replies

3. Shell Programming and Scripting

awk or grep to search one column and output the other

Hello, it would be great if someone can help me with the following: I want to search for the rows from fileA in column 1 of fileB and output column 2 of fileB if found in fileC. In the moment I search within the complete file. How can I change the code so only column 1 is searched? cat fileA... (7 Replies)
Discussion started by: Manyaka
7 Replies

4. Shell Programming and Scripting

[awk] grep a part of filename as entry file

hi all, i need to combine these files into one csv file. Bounce_Mail_Event_Daily_Report_01_Jul_2012.csv Bounce_Mail_Event_Daily_Report_02_Jul_2012.csv Bounce_Mail_Event_Daily_Report_03_Jul_2012.csv Bounce_Mail_Event_Daily_Report_04_Jul_2012.csv... (10 Replies)
Discussion started by: makan
10 Replies

5. Shell Programming and Scripting

awk to replace part of a column

dear all, I'm trying to use Awk to eliminate the last two characters from the first column in a file. This two characters are "-1" and I need to eliminate them from each row that I have in the files. The files have two columns and look like: ID_090-1 2 ID_3787-1 4 ID_0098-1 1 ID_12-1 4 I... (4 Replies)
Discussion started by: gabrysfe
4 Replies

6. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

7. Shell Programming and Scripting

grep/awk column or variable?

Hi, I'm running via PuTTY, in a BASH shell to do my work. I'm running calculations where steps are reported like this every 100 steps: NSTEP = 249900 TIME(PS) = 249.900 TEMP(K) = 299.94 PRESS = 21.1 Etot = -12912.5557 EKtot = 4996.8780 EPtot = -17909.4336 ... (6 Replies)
Discussion started by: Oriksagtaria
6 Replies

8. Shell Programming and Scripting

grep/awk on a single column in a for loop

Hi I'm trying to loop through a small list of id's and then pull out a few columns if the id matches that found in column 2 of the larger file. I managed to get one command to work awk -F " " '{if ($2 == '154080196') print $2,$3,$4}' tst.txt | less However, when I try it in a for loop I... (3 Replies)
Discussion started by: flotsam
3 Replies

9. Shell Programming and Scripting

Changing part of column value with AWK

Hi All , Here is my req I am extracting a column value and I want to change part of its value according to user input Say the column I extracted is of 15 digits and I want to change the 11th to 14th position with user input 4 digit value Example column value 111111111111111 user input... (8 Replies)
Discussion started by: Pratik4891
8 Replies

10. Shell Programming and Scripting

How to pull info under headers in file(awk,grep,while loop)

below is an extract from my file and I am trying to use Awk and grep and a while loop to pull infomation from under neath "HBA WWN=".HBA WWN=" reoccurs all over the file but the 100000c.....number are unique and I want to be able to pull and reference specifi information under this header ever time... (2 Replies)
Discussion started by: kieranfoley
2 Replies
Login or Register to Ask a Question