Manipulate fields with AWK whose positions are changable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulate fields with AWK whose positions are changable
# 1  
Old 06-07-2012
Manipulate fields with AWK whose positions are changable

Hello Friends,

I've been working with lots of different CDR-EDR files, before testing i need to manipulate my test files rather than requesting new files(to prepare them for next tests) which are different kind of CDRs,EDRs. In order to do this i might have to change more than a field in a CDR-EDR file, but the code that i could write can just change a position changeable field, not more. I could not go further and am lost in the complicity of the code Smilie I need a bit more generic code to manipulate several fields at once, so i would appreciate any help, any hint to achieve this..

Code:
Input:
AC=1|ACST=1|ACT=351|AU=1|AUO=3|CLD=9541|CNC=9054|CNTR=905429800011|CRT=1|CST=240.000|CT=0|CUSG=240000|CZ=1|CZA=1|DIAL=905483420005|

I divided an EDR as its longer than above...


Code:
Output:

awk -F\| 'NR<500{print}' test.edr | awk -F\| '{for(i=1;i<=NF;i++)if($i~/CRT/){split($i,arr,"=");$i=arr[1]"=4"}{print $0}}' OFS="|"

AC=1|ACST=1|ACT=351|AU=1|AUO=3|CLD=9541|CNC=9054|CNTR=905429800011|CRT=4|CST=240.000|CT=0|CUSG=240000|CZ=1|CZA=1|DIAL=905483420005|

Thanks in Advance,
Best Regards
# 2  
Old 06-07-2012
Code:
sed s/CRT=./CRT=4/g
AC=1|ACST=1|ACT=351|AU=1|AUO=3|CLD=9541|CNC=9054|CNTR=905429800011|CRT=1|CST=240.000|CT=0|CUSG=240000|CZ=1|CZA=1|DIAL=905483420005|
AC=1|ACST=1|ACT=351|AU=1|AUO=3|CLD=9541|CNC=9054|CNTR=905429800011|CRT=4|CST=240.000|CT=0|CUSG=240000|CZ=1|CZA=1|DIAL=905483420005|

Code:
sed -e 's/CRT=./CRT=4/g' -e 's/AUO=./AUO=5/g'
AC=1|ACST=1|ACT=351|AU=1|AUO=3|CLD=9541|CNC=9054|CNTR=905429800011|CRT=1|CST=240.000|CT=0|CUSG=240000|CZ=1|CZA=1|DIAL=905483420005|
AC=1|ACST=1|ACT=351|AU=1|AUO=5|CLD=9541|CNC=9054|CNTR=905429800011|CRT=4|CST=240.000|CT=0|CUSG=240000|CZ=1|CZA=1|DIAL=905483420005|


Last edited by new_item; 06-07-2012 at 08:40 PM..
# 3  
Old 06-07-2012
How about this, just set V with any new variable values you want and L for number of lines:

Code:
$ awk -vL=5000 -vV="AU=2|CNC=0" 'BEGIN{split(V,c,"|")}
 NR<L{
    for(r in c) {
      split(c[r],d,"=")
      gsub(d[1]"=[^|]*",c[r],$0)
    }
    print
 }' infile
AC=1|ACST=1|ACT=351|AU=2|AUO=3|CLD=9541|CNC=0|CNTR=905429800011|CRT=1|CST=240.000|CT=0|CUSG=240000|CZ=1|CZA=1|DIAL=905483420005|

# 4  
Old 06-07-2012
Quote:
Originally Posted by Chubler_XL
How about this, just set V with any new variable values you want and L for number of lines:

Code:
$ awk -vL=5000 -vV="AU=2|CNC=0" 'BEGIN{split(V,c,"|")}
 NR<L{
    for(r in c) {
      split(c[r],d,"=")
      gsub(d[1]"=[^|]*",c[r],$0)
    }
    print
 }' infile
AC=1|ACST=1|ACT=351|AU=2|AUO=3|CLD=9541|CNC=0|CNTR=905429800011|CRT=1|CST=240.000|CT=0|CUSG=240000|CZ=1|CZA=1|DIAL=905483420005|

Chubler couldnt have been better really, i knew how to assign variables via "-v" but couldnt gether all of them in a code, now your code let me improve myself thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

2. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

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

4. Linux

Problem in matching 2 fields with in consisten positions

hii i have a file that contains lines like this 4829:71370 1:N:0:CGATGT + chr6 126912761 GAAGGCATAGCCCGTTGGGCTGTGGTCATCAGCCTC CCCFFFFFHGHHHJHIJJJHIJIGHCGIIJJJJIJI 0 4829:71370 2:N:0:CGATGT + chr7 89349071 AGCCCTGCCCCCACCCCCCATTCTTCTTGACTGTCT C@@FFFFFHHHGHJ JIJIJIIIIJJJJJJJJIIJIJ 0 Now i... (4 Replies)
Discussion started by: anurupa777
4 Replies

5. Shell Programming and Scripting

awk regardless positions

brw------- 1 oracle dba 49, 21 Apr 05 11:45 dprod_0000018 brw------- 1 oracle dba 49, 26 Apr 05 11:45 dprod_0000019 brw------- 1 oracle dba 43, 93 Feb 02 2011 dprod_000002 brw------- 1 oracle dba 49, 27 Apr 05 11:45 dprod_0000020... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

6. Shell Programming and Scripting

seds to extract fields based on positions

Hi My file has a series of rows up to 160 characters in length. There are 7 columns for each row. In each row, column 1 starts at position 4 column 2 starts at position 12 column 3 starts at position 43 column 4 starts at position 82 column 5 starts at... (7 Replies)
Discussion started by: malts18
7 Replies

7. Shell Programming and Scripting

awk script replace positions if certain positions equal prescribed value

I am attempting to replace positions 44-46 with YYY if positions 48-50 = XXX. awk -F "" '{if (substr($0,48,3)=="XXX") $44="YYY"}1' OFS="" $filename > $tempfile But this is not working, 44-46 is still spaces in my tempfile instead of YYY. Any suggestions would be greatly appreciated. (9 Replies)
Discussion started by: halplessProblem
9 Replies

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

9. Shell Programming and Scripting

awk modifying entries on 2 lines at 2 positions

Hi this script adds text in the correct place on one line only, in a script. awk 'BEGIN{ printf "Enter residue and chain information: " getline var < "-" split(var,a) } /-s rec:/{$7=a; } {print}' FLXDOCK but I need the same info added at position 7 on line 34 and... (1 Reply)
Discussion started by: gav2251
1 Replies
Login or Register to Ask a Question