awk,cut fields by change field format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk,cut fields by change field format
# 1  
Old 11-05-2009
awk,cut fields by change field format

Hi Everyone,

Code:
[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
# 2  
Old 11-05-2009
You can use the substr() function with awk:

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

# 3  
Old 11-05-2009
Thanks Frank, it works well.
How about if now
Code:
[root@]# cat 1.txt
1321631,7770125555555
1321631,888493949494933
1321631,07774560125666
1321631,4560125666
[root@]#

I have an array:
Code:
@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:
Code:
awk -F, '{print $1,$2 ~ /@prefix/}' OFS="," 1.txt

i know above is wrong, please guide. Thanks
Thanks

Last edited by jimmy_y; 11-05-2009 at 08:13 AM..
# 4  
Old 11-05-2009
What kind of array is this? A Perl array?
# 5  
Old 11-05-2009
Quote:
Originally Posted by Franklin52
What kind of array is this? A Perl array?
Yes Frank, it is a perl array. Thanks
1.pl
Code:
#!/usr/bin/perl

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

# 6  
Old 11-05-2009
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
# 7  
Old 11-05-2009
Quote:
Originally Posted by Franklin52
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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert field between two fields using awk or sed

Hi All, I am trying to insert two columns in the following text. I tried awk but failed to achieve. Highly appreciate your help DATETIME="28-Sep-2013;20:09:08;" CONTROL="AB" echo "Myfile.txt;11671;7824.90;2822.48" The DATETIME will be inserted at the beginning and CONTROL will... (4 Replies)
Discussion started by: angshuman
4 Replies

2. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

3. Shell Programming and Scripting

how to cut the last field without using awk

i have file as with the below content aaa.bbb.cc.dd aaa.fff.bb yyyyy.rrrrr.ggggg.iii wwww.w.r.ty i want the o/p as below dd bb iii ty but i dont want to use awk. is there any other way to do this ? (5 Replies)
Discussion started by: anandgodse
5 Replies

4. Shell Programming and Scripting

How to Print from nth field to mth fields using awk

Hi, Is there any short method to print from a particular field till another filed using awk? Example File: File1 ==== 1|2|acv|vbc|......|100|342 2|3|afg|nhj|.......|100|346 Expected output: File2 ==== acv|vbc|.....|100 afg|nhj|.....|100 (8 Replies)
Discussion started by: machomaddy
8 Replies

5. Shell Programming and Scripting

awk - print all fields except for last field

How do I print all the fields of a record except for the $(NF) field? (4 Replies)
Discussion started by: locoroco
4 Replies

6. Shell Programming and Scripting

how to parse with awk (using different fields), then group by a field?

When parsing multiple fields in a file using AWK, how do you group by one of the fields and parse by delimiters? to clarify If a file had tom | 223-2222-4444 , randofield ivan | 123-2422-4444 , random filed ... | and , are the delimiters ... How would you group by the social security... (4 Replies)
Discussion started by: Josef_Stalin
4 Replies

7. Shell Programming and Scripting

AWK : Add Fields of lines with matching field

Dear All, I would like to add values of a field, if the lines match in a certain field. Then I would like to divide the sum though the number of lines that have a matched field. This is the Input: Input: Test1 5 Test1 10 Test2 2 Test2 5 Test2 13 Test3 4 Output: Test1 7.5 Test1 7.5... (6 Replies)
Discussion started by: DerSeb
6 Replies

8. UNIX for Advanced & Expert Users

change field 2 date format

from this input WEBELSOLAR,29122009,1:1 WIPRO,15062010,2:3 ZANDUREALT,18012007,1:3 i want output as WEBELSOLAR,20091229,1:1 WIPRO,20100615,2:3 ZANDUREALT,20070118,1:3 basically input is in ddmmyyyy format and i was to convert it to yyyymmdd format (1 Reply)
Discussion started by: manishma71
1 Replies

9. UNIX for Advanced & Expert Users

Printing Field with Delimiter in AWK/cut

Hello, I had posted earlier about printing fields using AWK, but now I have a slightly different problem. I have text files in the format: 1*2,3,4,5 and wish to print the first, third, and fifth fields, including the asterisk and commas. In other words, after filtering it should look... (1 Reply)
Discussion started by: Jahn
1 Replies

10. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies
Login or Register to Ask a Question