Help with handling columns in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with handling columns in bash
# 1  
Old 10-18-2010
Help with handling columns in bash

Hi all,

I've tried to look answer from various places but cannot get for my problem.

I have a file which is tab delimited:
Code:
column1     column2     column3
1                10              one
2                100            two
3                9                three
4                100            four
...
100            9               hundred

I'd like to change all 100 occurrences to 0 on column2 and leave all other occurrences intact.

Last edited by vgersh99; 10-18-2010 at 06:25 AM.. Reason: code tags, please!
# 2  
Old 10-18-2010
Code:
$ ruby -F"\t"  -ane '$F[1]=0 if $F[1]=="100";puts $F.join("\t")' file

This User Gave Thanks to kurumi For This Post:
# 3  
Old 10-18-2010
Code:
nawk '$2==100 {$2=0}1' OFS='\t' myFile
OR
nawk '$2=($2==100)?"0":$2' OFS='\t' myFile

This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 10-18-2010
with sed..
Code:
sed 's/[^][^]100[^0]/ 0/' inputfile

This User Gave Thanks to michaelrozar17 For This Post:
# 5  
Old 10-18-2010
@michael, what if any column contains for example 1100 or 1001 or for example column 3 contains number 100 followed by a space? You had me puzzled for a moment, until I figured your statement would be almost equivalent to:
Code:
sed 's/.100[^0]/ 0/' inputfile


Last edited by Scrutinizer; 10-18-2010 at 08:26 AM..
# 6  
Old 10-18-2010
@Scrutinizer

Sorry.How could this be achieved thru sed. Pls let me know..
# 7  
Old 10-18-2010
Quote:
Originally Posted by michaelrozar17
How could this be achieved thru sed.
why sed?
Code:
sed -r 's/^(.[^ \t]*[ \t]*)100\b/\10/' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash formatting data into columns

Hi guys, I'm trying to create a table of aggregated data using just bash commands. My data is in three columns, for example: 2014-01-01 testA 64 2014-01-01 testB 27 2014-02-01 testA 31 2014-02-02 testB 29 2014-02-02 testC 12 And the result I am looking for is: ... (4 Replies)
Discussion started by: mccmjc
4 Replies

2. Shell Programming and Scripting

Select columns from a matrix given within a range in BASH

I have a huge matrix file which looks like this (example matrix): 1 2 3 5 4 5 6 7 7 6 8 9 1 2 4 2 7 6 5 1 3 2 1 9 As one can see, this matrix has 4 columns and 6 rows. But my original matrix has some 3 million rows and 6000 columns. For example, on this matrix I can define my task as... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

3. Shell Programming and Scripting

BASH - Handling background processes - distributed processing

NOTE: I am using BASH and Solaris 10 for this. Currently in the process of building a script that has a main "watcher" daemon that reads a configuration file and starts background processes based on it's global configuration. It is basically an infinite loop of configuration reading. Some of the... (4 Replies)
Discussion started by: dcarrion87
4 Replies

4. Shell Programming and Scripting

how can i group by same columns by another columns in Bash

how can i group by same columns by another columns in Bash Eq. this is a csv file Co1 Co2 Co3 Co4 A A 1,000 1,000 A B 2,000 1,250 A A 2,000 3,002 A C 2,000 3,005 how can i get the result of like this Co1 Co2 Co3 Co4 A A 3,000 ... (5 Replies)
Discussion started by: qjlongs
5 Replies

5. UNIX for Dummies Questions & Answers

File handling in bash shell scripting

i am new to shell scripting and stuck at one place in my program. i am reading data from one structured file and extracting some data from particular lines and then writing into the output file. In that reading input file line by line from while loop. while read line do rectype=line... (7 Replies)
Discussion started by: reeta_shri
7 Replies

6. UNIX for Advanced & Expert Users

Unix Bash: substitute columns in .csv using other .csv columns

Hi All, I have two .csv's input.csv having values as (7 columns) ABC,A19907103,ABC DEV YUNG,2.17,1000,2157,07/07/2006 XYZ,H00213850,MM TRUP HILL,38.38,580,23308,31/08/2010 output.csv having (25 columns) A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y... (4 Replies)
Discussion started by: abhivyas
4 Replies

7. Shell Programming and Scripting

How can I print array elements as different columns in bash?

Hi Guys, I have an array which has numbers including blanks as follows: 1 26 66 4.77 -0.58 88 99 11 12 333 I want to print a group of three elements as a different column in a file as follows:(including blanks where there is missing elements) for.e.g. array element #7... (4 Replies)
Discussion started by: npatwardhan
4 Replies

8. Shell Programming and Scripting

File handling with bash shell scripting

Hi all, Can anyone guide to get tricks for file handling in bash shell? Thanks in advance. Thanks Deepak (2 Replies)
Discussion started by: naw_deepak
2 Replies

9. Shell Programming and Scripting

how to converting rows to columns, bash

I have in file these words: @fraza1 = rw @fraza2 = r @fraza3 = r @fraza4 = r @fraza5 = r @fraza1 = r @fraza6 = r @fraza7 = r @fraza2 = r @fraza8 = r @fraza9 = r ... I would like so that: ,rw,@fraza1 ,r,@fraza2 (2 Replies)
Discussion started by: patrykxes
2 Replies

10. Shell Programming and Scripting

handling filespec in bash script

hi there, i just need a help handling the output of filespec in case has been picked im writing a bash script similar to the command ls, and ive done everything except handing the filespec which i spent more than 3 hrs and i coundn't figure out or find any on the net. all what i need is... (11 Replies)
Discussion started by: new2Linux
11 Replies
Login or Register to Ask a Question