Changing values in the first column.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing values in the first column.
# 1  
Old 12-26-2013
Changing values in the first column.

I have a file which looks like this:

Code:
1 3 5 5 3 2
1 5 3 6 3 2
0 5 6 3 2 9
0 4 5 6 4 9

In the first column, instead of 1, I want to place +1 and instead of 0, I want -1 to be put. So the generated file should look like this:
Code:
+1 3 5 5 3 2
+1 5 3 6 3 2
-1 5 6 3 2 9
-1 4 5 6 4 9

Just to let people know what I have done, which to many will look very inefficient. This is why I am interested in knowing a quick solution to this. I first extracted the first column using the following command:
Code:
awk '{print $1}' input_file > output_file

Then separated the 1s and 0s in two separate files using the following command:
Code:
awk '{ print > $1 ".dat" }' input_file

Then added + before 1, and subtracted -1 from the file that contained 0. Then used cat to append the contents from the .dat files. Then used the paste command to paste the contents with the original file. This process is of course too time consuming. I am using Linux with BASH.
# 2  
Old 12-26-2013
awk '{ if( $1 == 1) $1 = "+1"; else if ($1 == 0) $1= "-1"; print;}' inputfile

If you have the first column to be 1 and 0 only, then use awk '{ if( $1 == 1) $1 = "+1"; else $1= "-1"; print;}' inputfile
This User Gave Thanks to PikK45 For This Post:
# 3  
Old 12-26-2013
Or use sub function:
Code:
awk '{sub(1,"+1",$1);sub(0,"-1",$1)}1' file

This User Gave Thanks to Yoda For This Post:
# 4  
Old 12-27-2013
Quote:
Originally Posted by Yoda
Or use sub function:
Code:
awk '{sub(1,"+1",$1);sub(0,"-1",$1)}1' file

Sub fails in this condition, it just replaces first occurrence

Code:
# Yoda's
$ cat << test | awk '{sub(1,"+1",$1);sub(0,"-1",$1)}1'
11
00
test

+11
-10

# PikK45
$ cat << test | awk '{ if( $1 == 1) $1 = "+1"; else if ($1 == 0) $1= "-1"; print;}'
11
00
test

11
-1

# 5  
Old 12-27-2013
Quote:
Originally Posted by PikK45
awk '{ if( $1 == 1) $1 = "+1"; else if ($1 == 0) $1= "-1"; print;}' inputfile

If you have the first column to be 1 and 0 only, then use awk '{ if( $1 == 1) $1 = "+1"; else $1= "-1"; print;}' inputfile
If the 1st field can only be 0 or 1, you can also try:
Code:
awk '{$1 = $1 ? "+1" : -1}1' input_file

# 6  
Old 12-27-2013
Yes, in my case the first column contains only 0 and 1. However, it is good to see others generalize to other values.
# 7  
Old 12-27-2013
This is what I thought after seeing @Akshay's analysis

Code:
awk '{ 
  if( length($1) == 1 ) {
    if( $1 == 1) $1 = "+1"; 
    else if ($1 == 0) $1= "-1"; 
  }
  print;}' input.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Changing values only in 3rd column and 4th column

#cat file testing test! nipw asdkjasjdk ok! what !ok host server1 check_ssh_disk!102.56.1.101!30!50!/ other host server 2 des check_ssh_disk!192.6.1.10!40!30!/ #grep check file| awk -F! '{print $3,$4}'|awk '{gsub($1,"",$1)}1' 50 30 # Output: (6 Replies)
Discussion started by: kenshinhimura
6 Replies

2. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. UNIX for Dummies Questions & Answers

Changing the values of a column using awk and gsub

Hi, I am using the following code to change NA to X in only the 5th column of my text file: awk '{gsub("NA","x",$5)}1' in.file > out.file How can I modify this code if I want to change NA to X in multiple columns of the text file (i.e. columns 5,6 and 7). Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

4. UNIX for Dummies Questions & Answers

Changing values of a column based on formula

Hi, I have a tab delimited text file with two columns where the second column takes on values between 1 and 6 (including). If the value of the second column, includes 1,2,3 I want to replace it with LOW. If it is 4,5,6 I want to replace it with HIGH. How do I go about doing that? Thanks ... (3 Replies)
Discussion started by: evelibertine
3 Replies

5. Shell Programming and Scripting

Converting odd values to even values(or vice-versa) located in a column

Hello All, I have a below data in a .csv file where all rows where col1 is A, col2 is odd numbers, similarly even numbers for all rows where col1 is B. Note that my data has some other columns(not shown here) too (around 100) after col2. Tool,Data A,1 A,3 A,5 .... so on B,2 B,4 .... ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. UNIX for Dummies Questions & Answers

shift values in one column as header for values in another column

Hi Gurus, I have a tab separated text file with two columns. I would like to make the first column values as headings for the second column values. Ex. >value1 subjects >value2 priorities >value3 requirements ...etc and I want to have a file >value1 subjects >value2 priorities... (4 Replies)
Discussion started by: Unilearn
4 Replies

7. Shell Programming and Scripting

Cat Values from Several files if it meets criteria for column values

I have results from some statistical analyses. The format of the results are as given below: I want to select lines that have a p-value (last column) less than 0.05 from all the results files (*.results) and cat to a new results file. It would be very nice if a new column is added that tells... (2 Replies)
Discussion started by: genehunter
2 Replies

8. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

9. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

10. 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
Login or Register to Ask a Question