awk,cut fields by change field format

Hi Everyone,

[root@]# cat 1.txt
1321631,77770132976455,19,20091001011859,20091001011907
1321631,77770132976455,19,20091001011859,20091001011907
1321631,77770132976455,19,20091001011859,20091001011907
[root@]# cat 1.txt | awk -F, '{OFS=",";print $1,$3,$4,$5}'
1321631,19,20091001011859,20091001011907
1321631,19,20091001011859,20091001011907
1321631,19,20091001011859,20091001011907
[root@]# cat 1.txt | cut -f1,3,4,5 -d ','
1321631,19,20091001011859,20091001011907
1321631,19,20091001011859,20091001011907
1321631,19,20091001011859,20091001011907
[root@]#

I can use either awk or cut to acheive the same result. If the finally output is:
1321631,19,20091001011859,20091001,011907
1321631,19,20091001011859,20091001,011907
1321631,19,20091001011859,20091001,011907
means to seperate the yyymmdd and hhmmss with ',', how should i do with awk or cut, or sed, etc, without open 1.txt, read the file, split','.

Thanks

You can use the substr() function with awk:

awk -F, '{print $1,$3,$4,substr($5,1,8) FS substr($5,9)}' OFS="," file

Thanks Frank, it works well.
How about if now

[root@]# cat 1.txt
1321631,7770125555555
1321631,888493949494933
1321631,07774560125666
1321631,4560125666
[root@]#

I have an array:

@prefix=qw(07774,888493,777);

The output should be like:
1321631,777,0125555555
1321631,888493,949494933
1321631,07774,560125666
1321631,,4560125666

So due to the 4th record no prefix in the predefined @prefix, so it is value is null.

I think:

awk -F, '{print $1,$2 ~ /@prefix/}' OFS="," 1.txt

i know above is wrong, please guide. Thanks
Thanks

What kind of array is this? A Perl array?

Yes Frank, it is a perl array. Thanks
1.pl

#!/usr/bin/perl

my @prefix=qw(07774,888493,777);
my $cmd = "awk -F, '{print $1,$2 ~ /@prefix/}' OFS=',' 1.txt";
`cmd`;

Unfortunately I'm not familiar with Perl. If you use Perl, awk should be redundant IMHO.

Hopely a Perl expert can give you the solution for your question...

Regards

Hi Frank, if without using perl, any solution for such scenario mo :confused:

Here is something using perl. Though it requires using split to parse the file and it's a bit long.

#!/usr/bin/perl

use warnings;
use strict;

my @array = ();

### Load file into array
for my $i (split '\n', `more file1`) {
    push @array, [ split ',', $i ] ;
}


### Define valid prefix
my @prefix_list=qw(07774 888493 777);

### Create loop for each line
LINE: for (my $line=0; $line<=$#array; $line++) {
    
    ### Print first part of each line.
    print "$array[$line][0],";

    ### Create loop for each prefix
    PREFIX: for my $prefix (@prefix_list) {
        
        ### Find length of prefix
        my $prefix_lngth = length $prefix;

        ### Check the second column in the line for a prefix match
        if (substr($array[$line][1], 0, $prefix_lngth) =~ /$prefix/) {

            ### If the prefix is found, print the prefix and then the rest of the line using substr
            print substr($array[$line][1], 0, $prefix_lngth), ",", substr($array[$line][1], $prefix_lngth), "\n";

            ### Jump to the next line now that you've parsed the entire line
            next LINE;
        }
    }

    ### If the prefixes don't match, just print second column
    print ",$array[$line][1]\n"
}