Update column in File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Update column in File
# 1  
Old 08-22-2012
Update column in File

I Have a file a.txt in the below format
Code:
1 23 50
1 25 6666666666666
1 23 34

If the third column value is greater than two digit then make it 0

Code:
New Data File
---------------
a.txt
1 23 50
1 25 0
1 23 34

Need your help regarding this

Last edited by vbe; 08-22-2012 at 01:30 PM.. Reason: Its square brakets for code tags! Nice try ...
# 2  
Old 08-22-2012
Code:
awk '$3 > 99 {$3=0}'1 file

# 3  
Old 08-22-2012
Using some posixsh (ksh, bash, dash ...)
Code:
while read a1 a2 a3 x
do
        ((a3<100)) && echo "$a1 $a2 $a3" && continue
        echo "$a1 $a2 0"
done < a.txt

Or if file is big, then awk is better - faster.
Code:
awk '
{       if ($3>99) $3=0
        print
}' a.txt
# or
awk '
$3 > 99 { $3=0  }
        { print }
' a.txt

# 4  
Old 08-29-2012
Smilie
Code:
awk '$3 > 99 {$3=0}'1 file

This Works fine .Can you please explain what is this 1

Last edited by Franklin52; 08-30-2012 at 04:32 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 08-30-2012
Quote:
Originally Posted by deep_kol
Smilie
awk '$3 > 99 {$3=0}'1 file

This Works fine .Can you please explain what is this 1
1 => Print line being read
# 6  
Old 08-30-2012
Code:
awk '(length($3)>2){$3=0}'1 filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Update a column from a Join

Here is my select that I have to identify the child records that are Open (e.c7 < 6000) when the parent (t2068) c.c7 > 3 SELECT c.c1000000161, c.c7, c.c1000000019, e.c1000000829 FROM t2068 c INNER JOIN t1533 e ON e.c1000000829 = c.c301572100 where c.c7 > 3... (2 Replies)
Discussion started by: newborndba
2 Replies

2. Shell Programming and Scripting

Update All Column Value by 1 if exist

Guys, Running on Linux Ubuntu. I need to find a method to automatically update all the rows for 2 columns and only if the rows not null. e.g : based on the example data below, I want to update all rows under column 2 and 3 (COL2, COL3) with an increment of 1 and only if the value is not... (7 Replies)
Discussion started by: faizalms
7 Replies

3. Shell Programming and Scripting

Update a mysql column via bash script

Hello, I want to check the value of all MySQL columns.(column name is "status") via bash script. If value is "0" at I want to make only single column value to "1" I have many "0" values on mysql database(on "status" column) "0" means it is a draft post. I want to publish a post. I... (2 Replies)
Discussion started by: tara123
2 Replies

4. Programming

Column update using Informix

Hi, I am using INFORMIX 8 and trying to update column value by apending '09' to existing record . update sample_data_table set Name = Name||22 where id = 90 and state = NY This worked fine in oracle Database,But throwing error in informix . I need output as Ravi22 for Ravi record in... (0 Replies)
Discussion started by: Perlbaby
0 Replies

5. Shell Programming and Scripting

Add column and update file

I have to update my file from server file. Server file A on location serverfile="/orf/ddl/plokall/fileA" myfile=""/orf/ddl/plokall/myupdatedfile" fileA: KALKIKLOKPO001 10.1 HFJFKSLOKPO002 11.4 POLKAK5143 184.3 POLKJI5144 107.5 OKILDL5145 107.10 CVT0909 10.11 KJL0405... (3 Replies)
Discussion started by: asavaliya
3 Replies

6. Shell Programming and Scripting

Update file to add as last column

I need to update a text file within a loop so that the value is added creating a new column at the end. I cannot use paste because I dont know the number of files. If I use >> the value is added after the last row. Please help. For example: Input file: Column 1 Column2 - ... (2 Replies)
Discussion started by: Surabhi_so_mh
2 Replies

7. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

8. Shell Programming and Scripting

Update a column value in csv files

Hi all I am new to scripting and i have an application from which i will export into a csv file as follows Column1, Column2, Column3 Sno1, Folder\Test.txt, Fail Sno2, Folder\Test1.txt, Pass Sno3, Folder\Test2.txt, Fail Now i need to change the column2 in all the rows from "Folder\" ... (1 Reply)
Discussion started by: rajeshrp
1 Replies

9. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

10. Shell Programming and Scripting

Column update

I have two files: fileA a,b,c,10,apple a,b,c,20,orange a,b,c,10,grape d,e,f,5,pear d,e,f,100,pear x,y,z,15,lemon x,y,z,20,apple x,y,z,30,grape fileB a,b,c,20 d,e,f,100 x,y,z,30 If col1=col1,col2=col2 and col3=3 of fileA to fileB, then take value in 4th col of fileB and replace... (5 Replies)
Discussion started by: giannicello
5 Replies
Login or Register to Ask a Question