Replacing column values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing column values
# 1  
Old 07-12-2012
Replacing column values

hi all ,

( perl)
i am trying to replace jst one column in a file for eg

month dayofweek dealar car-name
jan thurs mercedes c300
feb wed lexus is300

all this data is in a master file and i want to replace jan with 1
feb -2 .. march with 3 and so onn .. upto dec is 12

any advice ..

thanks
# 2  
Old 07-12-2012
Code:
awk 'NR != 1{$1=NR-1}1' infile

# 3  
Old 07-12-2012
thanks huaihaizi3

could u explain this a lil .. i am new to awk and sed commands .. also will this go across the entire file? replacing jan to 1 and feb to 2 or ny month to a corresponding number
# 4  
Old 07-12-2012
$1 : the first field
NR : Record Separator(the default value of NR is '\n',it's line number here)
Just as you file shows that the value month starts from the second line(NR=2)
# 5  
Old 07-12-2012
Quote:
Originally Posted by huaihaizi3
$1 : the first field
NR : Record Separator(the default value of NR is '\n',it's line number here)
Just as you file shows that the value month starts from the second line(NR=2)
Not exactly. NR is the Number of Records seen so far.

A possible solution:
Code:
awk 'BEGIN{split("jan_feb_mar_apr_may_jun_jul_aug_sep_oct_nov_dec",a,/_/);for (i in a) m[a[i]]=i}{print m[$1],$2,$3'}' infile

Or the terse version of it:
Code:
awk 'BEGIN{split("jan_feb_mar_apr_may_jun_jul_aug_sep_oct_nov_dec",a,/_/);for (i in a) m[a[i]]=i}{$1=m[$1]}1' infile


Last edited by ripat; 07-12-2012 at 03:50 PM.. Reason: another version.
This User Gave Thanks to ripat For This Post:
# 6  
Old 07-12-2012
Quote:
Originally Posted by ripat
Not exactly. NR is the Number of Records seen so far.
yeah,just a mistake,sorry
RS is Record Separator,NR is the Number of Records(line number here)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help in replacing column values

Hello All, I am having the file as below .I need to replace column 9-12 with some other values. In the below file I need to replace 1509 to 1508 and 1508 to 1507 .Can you please help me in how to do that Thanks, Arun ... (10 Replies)
Discussion started by: arunkumar_mca
10 Replies

2. Shell Programming and Scripting

Replacing values into a single column. sed/PERL

Hello everyone, i need to replace in the second column of my csv file, points by nothing and dash by comma like this: Input: 1 2 1;12.111.312-2;1.2;2;1-3 2 1 1;11.212.331-1;3.3;1;2-2 Output: 1 2 1;12111312;2;1.2;2;1-3 2 1 1;11212331;1;3.3;1;2-2 SED or PERL commands preferably. ... (7 Replies)
Discussion started by: satir
7 Replies

3. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

4. UNIX for Dummies Questions & Answers

Replacing values in a column if they equal a certain value

Hi, I have a tab delimited text file where some lines have the string "NA" in the second column. For these lines, I want to replace NA with the value in the first column, the symbol underscore followed by the value in the fourth column. How do I go about doing that? Thanks! Input: 1 ... (3 Replies)
Discussion started by: evelibertine
3 Replies

5. Shell Programming and Scripting

replacing negative values in a column with zero

Hi, i need help on replacing negative values in a column with 0. any quick fix on this? thanks much. for instance, input: 1 2.3 -0.4 -25 12 13 45 -12 desired output 1 2.3 0 0 12 13 45 (4 Replies)
Discussion started by: ida1215
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. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

Hi, I have a text file in the following format: Code: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 ... (2 Replies)
Discussion started by: evelibertine
2 Replies

8. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

I have a text file in the following format: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 13402 NA07019... (3 Replies)
Discussion started by: evelibertine
3 Replies

9. Shell Programming and Scripting

Replacing column 1 in one file with values in other file

Please help me with an shell / awk script to achieve following; File-1: ABCDW01 12322 23322 BDADW01 22232 24453 EDFAW00 32232 23422 and so on, notice that the first coloumn is a code and the another file contains the real value of each entry in the first colum above but not in a... (4 Replies)
Discussion started by: digipak
4 Replies

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