Perl find and add to column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl find and add to column
# 1  
Old 12-07-2015
Perl find and add to column

Hi,
Could you help me with script in perl

Code:
;A1;AAA;%/A/B;70;75;-;
;A1;AAA;%/A/C;70;75;-;
;A1;BBB;%/A/G;70;75;-;
;A1;BBB;%/A;70;75;-;

I would like to find line with '%/A' and put in that line in column 5 and 6 some other value ex: 90 and 99

example output:
Code:
;A1;AAA;%/A/B;70;75;-;
;A1;AAA;%/A/C;70;75;-;
;A1;BBB;%/A/G;70;75;-;
;A1;BBB;%/A;90;99;-;

gl
# 2  
Old 12-07-2015
Hi,
Code:
$ perl -pe 's@;%/A;[^;]*;[^;]*;@;%/A;90;99;@' foo.csv
;A1;AAA;%/A/B;70;75;-;
;A1;AAA;%/A/C;70;75;-;
;A1;BBB;%/A/G;70;75;-;
;A1;BBB;%/A;90;99;-;

Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 12-07-2015
Here's another quick throw away one-liner
Code:
perl -pe 's/(%\/A);\d+;\d+/$1;90;99/' vikus.file

Since you said script, here a more flexible scripted solution

Code:
#!/usr/bin/env perl

use strict;
use warnings;

my $id = '%/A';
my @replacement = (90, 99);

while(<>){
    my @fields = split(";");
    if($fields[3] eq $id){
        splice @fields, 4, 2, @replacement;
    }
    print join(";", @fields);
}

# 4  
Old 12-07-2015
Hi,
by sed i tried this sed -e '/\%\/A\;/s/[^;]*/90/5' -e '/\%\/A\;/s/[^;]*/99/6' file

I know that by -i it will process the file itself. But below is not working
sed -ie '/\%\/A\;/s/[^;]*/90/5' -ie '/\%\/A\;/s/[^;]*/99/6' file
can anyone please correct it.

Last edited by looney; 12-07-2015 at 12:25 PM..
# 5  
Old 12-07-2015
Code:
$
$ cat f6
;A1;AAA;%/A/B;70;75;-;
;A1;AAA;%/A/C;70;75;-;
;A1;BBB;%/A/G;70;75;-;
;A1;BBB;%/A;70;75;-;
$
$ # create backup of file "f6" and then edit in-place
$ sed -i.bkp -e '/\%\/A\;/s/[^;]*/90/5' -e '/\%\/A\;/s/[^;]*/99/6' f6
$
$ # check the backup file
$ cat f6.bkp
;A1;AAA;%/A/B;70;75;-;
;A1;AAA;%/A/C;70;75;-;
;A1;BBB;%/A/G;70;75;-;
;A1;BBB;%/A;70;75;-;
$
$ # check the original file
$ cat f6
;A1;AAA;%/A/B;70;75;-;
;A1;AAA;%/A/C;70;75;-;
;A1;BBB;%/A/G;70;75;-;
;A1;BBB;%/A;90;99;-;
$
$

# 6  
Old 12-07-2015
Quote:
Originally Posted by looney
Hi,
by sed i tried this sed -e '/\%\/A\;/s/[^;]*/90/5' -e '/\%\/A\;/s/[^;]*/99/6' file

I know that by -i it will process the file itself. But below is not working
sed -ie '/\%\/A\;/s/[^;]*/90/5' -ie '/\%\/A\;/s/[^;]*/99/6' file
can anyone please correct it.
The -i flag from GNU sed can take a suffix, therefore you can not combine flags or it will think that the other flags is part of the suffix, consuming it as such and leaving the command without a way to know that '/\%\/A\;/s/[^;]*/90/5' is an expression to be interpreted.

From man sed:
Code:
      -i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if SUFFIX supplied)

Code:
sed -i -e '/\%\/A\;/s/[^;]*/90/5' -e '/\%\/A\;/s/[^;]*/99/6'  file

This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Find the minimum value of the column with respect to other column

Hi All, I would like get the minimum value in the certain column with respect to other column. For example, I have a text file like this. ATOM 1 QSS SPH S 0 -2.790 -1.180 -2.282 2.28 2.28 ATOM 1 QSS SPH S 1 -2.915 -1.024 -2.032 2.31 2.31 ATOM 1 ... (4 Replies)
Discussion started by: bala06
4 Replies

2. Shell Programming and Scripting

awk to find maximum and minimum from column and store in other column

Need your support for below. Please help to get required output If column 5 is INV then only consider column1 and take out duplicates/identical rows/values from column1 and then put minimum value of column6 in column7 and put maximum value in column 8 and then need to do subtract values of... (7 Replies)
Discussion started by: as7951
7 Replies

3. Shell Programming and Scripting

Perl script to find last column

Dear all, I am new bee in perl scripting,i have generated one report in which i want to paste some data/text at the end,plz help me format is like this $worksheet1->write( 'A5',,$format2); i want to paste my text after this column A50 in column A51 but this column is variable it may... (1 Reply)
Discussion started by: sagar_1986
1 Replies

4. Shell Programming and Scripting

Find column and add numbering

Hi Masters, Kindly need your help to format this data... IMPORT_HORIZON_HEADER 0 0 35 SURFACE_DATA_FORMAT XYZ- COLOR_FORMAT RGB SURFACE EB_S1_S2_Fault1 700 3 475370.187500 3672355.250000 1513.770386 475544.281250 3672530.250000 2255.540039 (4 Replies)
Discussion started by: ipatah
4 Replies

5. Programming

Perl find text and add line

Hi All I need to add a line to a file but after a certain block of text is found The block of text looks like this <RDF:Description RDF:about="urn:mimetype:video/quicktime" NC:value="video/quicktime" and i need to add this in the next line down ( note there is... (4 Replies)
Discussion started by: ab52
4 Replies

6. Shell Programming and Scripting

Find String and add to next column

Dear All, I have file input input01.txt 14193 40 153 14208 40 168 14283 45 243 14298 40 258 14313 41 273 14328 44 288 ... ...input02.txt 499815.5 9886300.0 14208 94.2 1957.1 499815.5 9886300.0 14208 314.2 2101.0 ... (2 Replies)
Discussion started by: attila
2 Replies

7. Shell Programming and Scripting

Find lines with matching column 1 value, retain only the one with highest value in column 2

I have a file like: I would like to find lines lines with duplicate values in column 1, and retain only one based on two conditions: 1) keep line with highest value in column 3, 2) if column 3 values are equal, retain the line with the highest value in column 4. Desired output: I was able to... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

8. Shell Programming and Scripting

Compare Two Files(Column By Column) In Perl or shell

Hi, I am writing a comparator script, which comapre two txt files(column by column) below are the precondition of this comparator 1)columns of file are not seperated Ex. file1.txt 8888812341181892 1243548895685687 8945896789897789 1111111111111111 file2.txt 9578956789567897... (2 Replies)
Discussion started by: kumar96877
2 Replies

9. Shell Programming and Scripting

Need to add letters to a column and add in a new column subtracting from another column

So I have this input 1 10327 rs112750067 T C . PASS DP=65;AF=0.208;CB=BC,NCBI 1 10469 rs117577454 C G . PASS DP=2055;AF=0.020;CB=UM,BC,NCBI 1 10492 rs55998931 C T . PASS DP=231;AF=0.167;CB=BC,NCBI 1 10583 rs58108140 G A ... (3 Replies)
Discussion started by: kellywilliams
3 Replies

10. Shell Programming and Scripting

find expression with awk in only one column, and if it fits, print whole column

Hi. How do I find an expression with awk in only one column, and if it fits, then print that whole column. 1 apple oranges 2 bannanas pears 3 cats dogs 4 hesaid shesaid echo "which number:" read NUMBER (user inputs number 2 for this example) awk " /$NUMBER/ {field to search is field... (2 Replies)
Discussion started by: glev2005
2 Replies
Login or Register to Ask a Question