Replacing values into a single column. sed/PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing values into a single column. sed/PERL
# 1  
Old 09-23-2014
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:
Code:
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:
Code:
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.

Thanks!
# 2  
Old 09-23-2014
You said you wanted "-" in field 2 to be replaced by ","; but your sample output used ";" instead of ",". Which do you really want?

What have you tried?

Why are sed and perl preferable to awk for this?
# 3  
Old 09-23-2014
in fact i want to replace "-" by semicolon instead simple comma (Mea culpa)
I prefer sed or perl because after some experiences i have seen that sed or perl commands was more fast than awk...
# 4  
Old 09-23-2014
And, what have you tried?
# 5  
Old 09-23-2014
I assume he doesn't know where to start Smilie
Code:
# accurate and fast (recommended)
awk -F";" '{gsub(/\./,"",$2); sub(/-/,";",$2); print}' OFS=";" input

Code:
# accurate, but relatively slow
perl -e 'while(<>){ @f=split(/;/); $f[1]=~s/\.//g; $f[1]=~s/-/;/; print join(";",@f); }' input

Code:
# very fast, but it doesn't care for columns, it simply deletes the first two occurrences of "."
# and replaces the first ocurrence of "-" with ";". It might work for you, if there are no "."s
# and no "-" in the first column PLUS if there are always two "."s and one "-" in the second column.
sed 's/\.//;s/\.//;s/-/;/' input

# 6  
Old 09-24-2014
i cant select just the 2nd column. To replace its easy with
Code:
sed -i

command but i can do that for the whole file. To remove points, i can use
Code:
grep -Ev

, but how select just the 2nd column to do these changes.

Thanks

Julien
# 7  
Old 09-24-2014
I prefer using awk (as suggested by junior-helper) because it is easy for me to read and immediately understand. I believe the following sed script also does what you want, but for many users this is less readable and harder to understand:
Code:
sed '
:again
/^\([^;]*;[^.;]*\)[.]/s//\1/
t again
/^\([^;]*;[^-;]*\)-/s//\1;/
' Input

Fast is nice; but I'm not sure that sed is going to be any faster than awk for this. Which script do you find easier to understand?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

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

3. Shell Programming and Scripting

Perl : Assigning multile hash values to a single array

I know that @food = %fruit; Works. But how do I assign %fruit and %veggies to @food ? (2 Replies)
Discussion started by: popeye
2 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 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... (5 Replies)
Discussion started by: technoman
5 Replies

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

7. Shell Programming and Scripting

splitting single column values into text and number component

Hey guys, I have a column that consists of string and integer values without a distinctive deliminator, looking like this... 7ASA 14LAL 245FOO 656MOM 87577DAD ... I want to split the column into two columns, one containing the numbers and one containing the text part. edit: numbers... (3 Replies)
Discussion started by: origamisven
3 Replies

8. Shell Programming and Scripting

Concatenating column values with unique id into single row

Hi, I have a table in Db2 with data say id_1 phase1 id_1 phase2 id_1 phase3 id_2 phase1 id_2 phase2 I need to concatenate the values like id_1 phase1,phase2,phase3 id_2 phase1,phase2 I tried recursive query but in vain as the length of string to be concatenated in quite long. ... (17 Replies)
Discussion started by: jsaravana
17 Replies

9. Shell Programming and Scripting

Converting Column values to comma delimted single Row

I have a requirement in which i have to read a file which has multiple columns seperated by a pipe "|" from this i have to read each column values seperately and create a comma seperated row for the column and write to another file. eg: Input file: ColA ColB 1 2 2 x 3 y... (5 Replies)
Discussion started by: nvuradi
5 Replies

10. Shell Programming and Scripting

sed question - replacing param values

Hello, Do you have any idea why the below sed command is also replacing the value of "PARAMETER2" instead of just "PARAMETER" in file1 ? % parameter=PARAMETER % new_value=2 % cat file1 PARAMETER=1 PARAMETER2=1 % cat file1 | sed s/*$/${new_value}/1 PARAMETER=2 PARAMETER2=2 Thanks. (3 Replies)
Discussion started by: majormark
3 Replies
Login or Register to Ask a Question