grep on columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep on columns
# 1  
Old 05-29-2012
grep on columns

Hi,


my data is this way

Code:
abc def 01000000 kil
ghi 23 01000000 kim
ghj 0000 01000000 omg

now, I would like to replace 01000000 on the third column as 0.

I have a file with that value in different columns. So, any awk one liner that can let me change the column $ value would be highly appreciated.

TIA
# 2  
Old 05-29-2012
awk lets you assign columns by writing to them. Also, any variable can be used as a column with $ since it's an operator. It's just that easy.

Code:
awk '{ for(N=1; N<=NF; N++) if($N == "01000000") $N="0" } 1'

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-29-2012
Quote:
Originally Posted by Corona688
awk lets you assign columns by writing to them. Also, any variable can be used as a column with $ since it's an operator. It's just that easy.

Code:
awk '{ for(N=1; N<=NF; N++) if($N == "01000000") $N="0" } 1'

Hi Corona,

Thanks for ur timely response and great explanation.

The output is not tab separated. How do I do that?
# 4  
Old 05-29-2012
Quote:
Originally Posted by jacobs.smith
Hi Corona,

Thanks for ur timely response and great explanation.

The output is not tab separated. How do I do that?
Code:
awk 'BEGIN{OFS="\t"}{ for(N=1; N<=NF; N++) if($N == "01000000") $N="0" } 1'

or even shorter if "01000000" is always on 3rd column
Code:
awk 'BEGIN{OFS="\t"}{ if($3 == "01000000") $3="0" } 1'

This User Gave Thanks to sdf For This Post:
# 5  
Old 05-29-2012
Quote:
Originally Posted by sdf
Code:
awk 'BEGIN{OFS="\t"}{ for(N=1; N<=NF; N++) if($N == "01000000") $N="0" } 1'

or even shorter if "01000000" is always on 3rd column
Code:
awk 'BEGIN{OFS="\t"}{ if($3 == "01000000") $3="0" } 1'


Life made simple.

Thanks to unix.com
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep in specific columns

I am trying to search a list of strings from a file and display the string as well as the column in the search file it was found. I dont care about the row. what is wrong with my script? while read line; do awk -v var="$line" '{for(i=1;i<NF;i++) if ($NF==$var) break; print $var FS $NF' }'... (3 Replies)
Discussion started by: senhia83
3 Replies

2. Shell Programming and Scripting

Grep in multiple columns

Hi All, One of my source file is having more than 100 columns. Now, I need to check the particular string in 75th,76th and 79th columns of the source file. If I find the particular string in above mentioned columns, then I need to print the entire record with line number. Kindly help... (2 Replies)
Discussion started by: suresh_target
2 Replies

3. UNIX for Dummies Questions & Answers

Grep into columns

Hi folks, hopefully an easy one .... I've got a hefty patching project going on on Solaris 8 boxes..... I'm trying to make a spreadsheet with the current packages, revision , description, patches etc I'd like to put the information output from pkginfo into columns... so eg :- system ... (3 Replies)
Discussion started by: Martincorneuk
3 Replies

4. Shell Programming and Scripting

Grep and print only certain columns from a row

Hi Friends, This is my input chr1 100 200 + gene_name "alpha"; protein_name "alpha"; level 2; tag "basic"; info "known"; chr1 245 290 + gene_name "alpha-1"; protein_name "alpha-2"; level 9; tag "basic"; info "uknown"; chr1 310 320 + gene_name "alpha"; protein_name "alpha-4"; level 2; info... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Grep and convert into columns

I have 1000 different autosys jobs, want to extract only this information using unix commands. Tried with normail greping but unable to make columns into rows. Input: /* ----------------- template ----------------- */ insert_job: template job_type: c box_name: box1 command:... (3 Replies)
Discussion started by: onesuri
3 Replies

6. UNIX for Dummies Questions & Answers

grep only from a range of columns

Hello all, I have a .csv file with over 100 columns. I would like to grep for a pattern only searching within a range of those fields, and print the entire line. For example: grep a pattern from columns $47-$87, but print fields $0 - $100 Thanks! (9 Replies)
Discussion started by: torchij
9 Replies

7. Shell Programming and Scripting

Grep based on specific columns.

Hi, How can I grep a record for a value based on specific column. If I simply do a grep 'AB' FilenName.txt, I might end up getting the records returned whose part of value is 'AB'. But I want it specific to second column. cut -d'|' -f 2 FileName.txt | grep 'AB' But now it will return... (1 Reply)
Discussion started by: deepakwins
1 Replies

8. Shell Programming and Scripting

grep required data from two columns

hello, I have output from a command and I need to filter some info out of that. I tried awk command but I can not grep what I am looking for: Following is the output and I need to capture "disabled" for each volume from first column and report: # vol status Volume State ... (2 Replies)
Discussion started by: za_7565
2 Replies

9. UNIX for Advanced & Expert Users

Grep all the columns based on a particular column

This is the data file that I have 21879, 0, 22, 58, 388 0, -1, 300, 1219172589765, 1708, 0, 200, 21891, 0, 0, 33, 309 0, -1, 300, 1219172591478, 1768, 0, 200, 22505, 0, 0, 33, 339 0, -1, 300, 1219172593251, 1738, 0, 200, 21888, 0, 1, 33, 308 0, -1, 300, 1219172594995, 633, 0, 200, 24878,... (2 Replies)
Discussion started by: pmallur
2 Replies

10. UNIX for Dummies Questions & Answers

using Grep for only a certain number of columns

I am given a file with a list of names for the first 20 characters. The 20 characters after is a list of names that somebody is supposed to team up with. Now i'm trying to write a simple script that will read a name somebody inputs and then looks only in the first 20 characters. For example.... (2 Replies)
Discussion started by: MaestroRage
2 Replies
Login or Register to Ask a Question