How to split a file into column with awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to split a file into column with awk?
# 1  
Old 04-04-2017
How to split a file into column with awk?

The following is my code

Code:
nawk -F',' '
BEGIN { printf "MSISDN,IMSI,NAM,TS11,TS21,TS22,OBO,OBI,BAIC,BAOC,BOIC,BOIEXH,APNID0,APNID1,APNID2,APNID3,APNID0,CSP,RSA\n" }
{
for(i=1; i<=NF; i++)
{
split($i,a,":")
gsub(" ","", a[2])
printf "%s;",a[2]
}
printf "\n"
}'HLR_DUMP_BZV >> HLR_full

This is my input file
Code:
MSISDN: 242064679187,IMSI: 629100113957561,NAM: 0,TS11: 1,TS21: 1,TS22: 1,,,BAIC: 1,BAOC: 1,BOIC: 1,BOIEXH: 1,,APNID1: 2,APNID2: 1,APNID3: 0,,CSP: 6,RSA: 3,
MSISDN: 242068905137,IMSI: 629100114484628,NAM: 1,TS11: 1,TS21: 1,,OBO: 1,OBI: 1,BAIC: 1,BAOC: 1,BOIC: 1,BOIEXH: 1,,APNID1: 2,APNID2: 1,APNID3: 0,,CSP: 6,RSA: 3,
MSISDN: 242064493944,IMSI: 629100113334650,NAM: 0,TS11: 1,TS21: 1,TS22: 1,,,BAIC: 1,BAOC: 1,BOIC: 1,BOIEXH: 1,,APNID1: 2,APNID2: 1,APNID3: 0,,CSP: 6,RSA: 3,
MSISDN: 242064544409,IMSI: 629100114841247,NAM: 0,TS11: 1,TS21: 1,TS22: 1,,,BAIC: 1,BAOC: 1,BOIC: 1,BOIEXH: 1,,APNID1: 2,APNID2: 1,APNID3: 0,,CSP: 6,RSA: 3,
MSISDN: 242068626345,IMSI: 629100114187228,NAM: 1,TS11: 1,TS21: 1,,OBO: 1,OBI: 1,BAIC: 1,BAOC: 1,BOIC: 1,BOIEXH: 1,,,,,,CSP: 6,RSA: 3,

I want all that comes after the colon, should be in the column name the word before. ie

Code:
MSISDN    IMSI    NAM .......

242064679187;629100113957561;0;.....

the code i posted above is running but not giving me any output.



Moderator's Comments:
Mod Comment Please use CODE (not ICODE) tags correctly for code and data as required by forum rules!

Last edited by RudiC; 04-04-2017 at 05:19 AM.. Reason: Changed ICODE to CODE tags.
# 2  
Old 04-04-2017
Running your code, for me it yields
Code:
MSISDN,IMSI,NAM,TS11,TS21,TS22,OBO,OBI,BAIC,BAOC,BOIC,BOIEXH,APNID0,APNID1,APNID2,APNID3,APNID0,CSP,RSA
242064679187;629100113957561;0;1;1;1;;;1;1;1;1;;2;1;0;;6;3;;
242068905137;629100114484628;1;1;1;;1;1;1;1;1;1;;2;1;0;;6;3;;
242064493944;629100113334650;0;1;1;1;;;1;1;1;1;;2;1;0;;6;3;;
242064544409;629100114841247;0;1;1;1;;;1;1;1;1;;2;1;0;;6;3;;
242068626345;629100114187228;1;1;1;;1;1;1;1;1;1;;;;;;6;3;;

What in this don't you like? Did you check the (redirected) output file?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using awk to split a column into two columns

Hi, I am trying to split the following output into two columns, where each column has Source: Destination: OUTPUT TO FILTER $ tshark -r Capture_without_mtr.pcap -V | awk '/ (Source|Destination): /' | more Source: x.x.x.x Destination: x.x.x.x Source:... (2 Replies)
Discussion started by: sand1234
2 Replies

2. Shell Programming and Scripting

To Split the file based on column value

Hi Team, I have a requirement in such a way that need to split the file into two based on which column particular value appears.Please find my sample file below. Lets consider the delimiter of this file as either comma or two colons.(:: and ,). So I need to split the file in such a way that all... (2 Replies)
Discussion started by: ginrkf
2 Replies

3. Shell Programming and Scripting

awk split columns to row after N number of column

I want to split this with every 5 or 50 depend on how much data the file will have. And remove the comma on the end Source file will have 001,0002,0003,004,005,0006,0007,007A,007B,007C,007E,007F,008A,008C Need Output from every 5 tab and remove the comma from end of each row ... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

4. Shell Programming and Scripting

Awk: split column if special characters

Hi, I've data like these: Gene1,Gene2 snp1 Gene3 snp2 Gene4 snp3 I'd like to split line if comma and then print remaining information for the respective gene. My code: awk '{ if($1 ~ /,/){ n = split($0, t, ",") (7 Replies)
Discussion started by: genome
7 Replies

5. Shell Programming and Scripting

Split file when value in column is blank

Hi Experts, In short : Need to split file when field in column 5 is blank and need to generate two file in which column 5 is blank and other in which column 5 has values along with other rows and column data My issue is i am not able to get header for column from raw file into new file which... (4 Replies)
Discussion started by: as7951
4 Replies

6. Shell Programming and Scripting

awk split columns after matching on rows and summing the last column

input: chr1 1 2 3 chr1 1 2 4 chr1 2 4 5 chr2 3 6 9 chr2 3 6 10 Code: awk '{a+=$4}END{for (i in a) print i,a}' input Output: chr112 7 chr236 19 chr124 5 Desired output: chr1 1 2 7 chr2 3 6 19 chr1 2 4 5 (1 Reply)
Discussion started by: jacobs.smith
1 Replies

7. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

8. Shell Programming and Scripting

Split column using awk in a text file

chr1 412573 . A C 2758.77 . AC=2;AF=1.00;AN=2;DP=71;Dels=0.00;FS=0.000;HaplotypeScore=2.8822;MLEAC=2;MLEAF=1.00;MQ=58.36;MQ0=0;QD=38.86;resource.EFF=INTERGENIC(MODIFIER||||||||) GT:AD:DP:GQ:PL 1/1:0,71:71:99:2787,214,0 GATKSAM chr1 602567 rs21953190 A ... (9 Replies)
Discussion started by: mehar
9 Replies

9. Shell Programming and Scripting

Split file by column value

Please help me figure out whats wrong with my code, I have to split a file according to values in 3rd thru 6th column, If the 3rd or 4th col values are >0, row goes to 1 file, if 3rd and 5th are >0,row goes to another file...and so on... What is wrong with my code? It doesn't seem to work.... (2 Replies)
Discussion started by: newbie83
2 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question