replace all numbers in column with another number in bash


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers replace all numbers in column with another number in bash
# 1  
Old 04-14-2010
replace all numbers in column with another number in bash

Hi,

I've been trying to replace the numbers in the first column of my text file with all ones, unless the number is equal to 8. I have this:

1 1 11 123 258
2 1 9 135 175
1 1 15 143 274
8 1 13 153 172
8 1 13 154 166
8 1 13 154 167
3 1 15 237 255
4 1 15 243 202
1 1 13 133 166
1 1 13 133 167

And I would like this:

1 1 11 123 258
1 1 9 135 175
1 1 15 143 274
8 1 13 153 172
8 1 13 154 166
8 1 13 154 167
1 1 15 237 255
1 1 15 243 202
1 1 13 133 166
1 1 13 133 167

This can be done pretty easily with structures in Matlab, but only somewhat awkwardly in bash using awk and a while loop, e.g.:

Code:
cat file | while read line; 
   do echo $line;
   then some if loop/case statement here (when == 8, leave alone, else set = 1); 
   then a rewriting of the file here; 
done

....but this is so clunky and cumbersome. I'm wondering if someone out there has a sleeker, more streamlined solution with of awk or sed (or anything).

Any ideas are welcome. Thanks in advance.
# 2  
Old 04-14-2010
Code:
awk '$1!=8 {$1=1} {print}' filename > newfilename

# 3  
Old 04-15-2010
Try this,

Code:
#!/bin/sh
>new_file
while read line; do
        first_field=`echo ${line} | cut -d ' ' -f 1`
        if [[ $first_field -ne 8 ]]
        then
                change_line=`echo ${line} | sed -r 's/[0-9]{,} /1 /'`
                echo ${change_line} >>new_file
        else
                echo ${line} >>new_file
        fi

done < file

# 4  
Old 04-15-2010
Code:
sed -ie 's/^[1-7,9] /1 /' filename


or

Code:
sed -e 's/^[^8] /1 /'  filename


HTH,
PL
# 5  
Old 04-15-2010
Jim - once again, your code worked flawlessly. Thanks to all for the other solutions, too!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use awk to replace numbers in a file with a column from another file

Hello, I am trying to make a awk code that will take 2 files, a txt file like this : 1 1 88 c(1:38, 42, 102) 2 2 128 c(39:41, 43:101, 103:105, 153, 155:189, 292, 344:369) 3 3 84 c(190:249, 603, 606:607, 609:629) 4 4 12 ... (8 Replies)
Discussion started by: nastaziales
8 Replies

2. Shell Programming and Scripting

Find number of characters in a column and replace

Hi all, I want to count total no. of characters in a column. and if no. of charaters are more than 3 then it must replace it by splitted string. ie, it must place a space after 3 characters. Ex: 21 435g asd3dd jklfjwe wer column number 3 has 4 alphanumeric character, so it must be splitted... (3 Replies)
Discussion started by: CAch
3 Replies

3. Shell Programming and Scripting

Replace column by random number addition

Here is my problem:- I have a file with pipe separated values. CR|20121021|079|ABC|N|DLS|00038|DLS|04750|1330597704|634234|634|0 CR|20121021|079|ABC|N|DLS|00038|DLS|05118|2071690102|354|351|3 CR|20121021|079|ABC|N|DLS|00038|DLS|05140|960051505|1088|1088|0... (4 Replies)
Discussion started by: Yoda
4 Replies

4. Shell Programming and Scripting

Bash Replace value in specific column

Hi all, I have two files with the following format: file1 BBB;33 AAA;2 CCC;5 file2 5;.;.;. 33;.;.;. The first file contain a list of code and numbers. The second file only the number. I would like to replace the corresponding code in the first column of the file1 with the... (3 Replies)
Discussion started by: g256
3 Replies

5. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

6. Shell Programming and Scripting

Replace 2nd column of CSV file with numbers on line

I have a csv file with occasional multiple entries in the second column. 111111,104,07-24-2011,3.15,N, 222222,020 140,07-24-2011,10.00,N,I want the result 111111,104,07-24-2011,3.15,N, 222222,020,07-24-2011,10.00,N, 222222,140,07-24-2011,10.00,N, I know I can get the output of the second... (5 Replies)
Discussion started by: ffdstanley
5 Replies

7. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

8. Shell Programming and Scripting

Replace last row of a column in bash/awk/sed

Hi, I've got a file with 3 columns which ends like this: ... 1234 345 1400 5287 733 1400 8472 874 1400 9317 726 1400 I want to replace the last row of the last column with the value 0. So my new file will end: ... 1234 345 1400 5287 733 1400 8472 874 1400 9317 726 ... (5 Replies)
Discussion started by: jhunter87
5 Replies

9. 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

10. AIX

How to replace many numbers with one number in a file

How to replace many numbers with one number in a file. Many numbers like 444565,454678,443298,etc. i want to replace these with one number (300).Please halp me out. (2 Replies)
Discussion started by: vpandey
2 Replies
Login or Register to Ask a Question