awk separate field by letter and ";"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk separate field by letter and ";"
# 1  
Old 04-29-2015
awk separate field by letter and ";"

i have a 2 fields in my DB

Code:
ID25333,1429291340lNormPUC-AP_MEX_UFM-GOL_44;PUC-AP_VEX_UFM-ROL_55;PUCAP_MEX_UFM-DOJ_49; 

ID55555,1429291340lNormPUC-AP_PPP_UFM-HOL_44;PUC-AF_GEX_UJM-SOL_45;PUCAP_MEX_UFM-DOJ_59;

and i need separate like this

Code:
ID25333,PUC-AP_MEX_UFM-GOL_44;    
ID25333,PUC-AP_VEX_UFM-ROL_55;
ID25333,PUCAP_MEX_UFM-DOJ_49;
ID55555,PUC-AP_PPP_UFM-HOL_44; 
ID55555,PUC-AF_GEX_UJM-SOL_45;
ID55555,PUC-AP_MEX_UFM-DOJ_59;

i using this
Code:
awk -F'(,|PUC-AP_)' '{for (i=3;i<=NF;i++)print $1",PUC-AP_"$i}'  file > file.out

it works but the field "PUC-AP" sometimes no work and show another characters, because this field is not always the same

any uggestions to field start PUC-AP and end in ; and only show the ID and the PUC field?

thanks
# 2  
Old 04-29-2015
In what way does "the field 'PUC-AP' sometimes no work"?

Please show us data where it does not work. Otherwise whatever we make is liable to have the same problem.
# 3  
Old 04-29-2015
Hi,
Code:
D12345678,2367980421^Deovalero^DPUC-AP_MOX_UPM-LADF_9;.PUC-AP_MOX_UPM-LADF_10;.PUC-AP_MOX_UPM-LADF_31;^C1430314328^Dogasio^DPUC-AP_MOX_UPM-LADF_9;

Code:
awk -F'(,|PUC-AP_)' '{for (i=3;i<=NF;i++)print $1",PUC-AP_"$i}' in.file > out.file

Code:
D12345678,PUC-AP_MOX_UPM-LADF_9;.
ID12345678,PUC-AP_MOX_UPM-LADF_10;.
ID12345678,PUC-AP_MOX_UPM-LADF_31;^C1430314328^Dogasio^D
ID12345678,PUC-AP_MOX_UPM-LADF_9;

~
Code:
ID12345678,PUC-AP_MOX_UPM-LADF_31;^C1430314328^Dogasio^D

i need
Code:
D12345678,PUC-AP_MOX_UPM-LADF_9;
ID12345678,PUC-AP_MOX_UPM-LADF_10;
ID12345678,PUC-AP_MOX_UPM-LADF_31;
ID12345678,PUC-AP_MOX_UPM-LADF_9;

that field is not always the same

Last edited by Scrutinizer; 04-29-2015 at 03:47 PM..
# 4  
Old 04-29-2015
Hi,
maybe:
Code:
awk -F\; '{gsub(/[,;][^;]*PUC/,";PUC",$0);for(i=2;i<NF;i++) print $1","$i";"}'

Regards.
This User Gave Thanks to disedorgue For This Post:
# 5  
Old 04-29-2015
If you're looking for strings starting with PUC-AP_ and ending with ;, that doesn't match the output specified in post #1, but this seems to work:
Code:
awk '
BEGIN {	FS = OFS = ","
	mp = "PUC-AP_[^;]*;"
}
$2 ~ mp {
	while(match($2, mp)) {
		print $1, substr($2, RSTART, RLENGTH)
		$2 = substr($2, RSTART + RLENGTH)
	}
}' file

which, with the sample input from post #1 produces the output:
Code:
ID25333,PUC-AP_MEX_UFM-GOL_44;
ID25333,PUC-AP_VEX_UFM-ROL_55;
ID55555,PUC-AP_PPP_UFM-HOL_44;

If you're looking for strings just starting with PUC and ending with ;, (which comes closer, but still does not exactly match the requested output), change the line in the script above:
Code:
	mp = "PUC-AP_[^;]*;"

to:
Code:
	mp = "PUC[^;]*;"

Then the modified script produces the output:
Code:
ID25333,PUC-AP_MEX_UFM-GOL_44;
ID25333,PUC-AP_VEX_UFM-ROL_55;
ID25333,PUCAP_MEX_UFM-DOJ_49;
ID55555,PUC-AP_PPP_UFM-HOL_44;
ID55555,PUC-AF_GEX_UJM-SOL_45;
ID55555,PUCAP_MEX_UFM-DOJ_59;

If you want to try this script on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.
# 6  
Old 04-30-2015
Or with gawk4, try:
Code:
gawk -v FPAT='ID[^,]*,|PUC-?*AP[^;]*;' '{for(i=2; i<=NF; i++) print $1 $i}' file

This is based on your sample. If you only need fields that start with PUC you can use PUC[^;]*, rather than PUC-?*AP[^;]*
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

2. Shell Programming and Scripting

[SOLVED] How to set field separator in awk when FS is "||" ?

Dear Experts, How to use field separator in awk , when the field separation is needed as "||" Example: (file1 ) aa || bb || cc || 1234 xx || yy || zz || 1123 Q: If I want to print $3 and $4 only , with using the field separator "||" How to write awk code: (6 Replies)
Discussion started by: rveri
6 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

awk: Force "escaped" representation of a field

Is there any way to get the escaped version of a field in awk? For those that don't understand the question, here is a clarifying example. Lets say a field $1 gives me the string "(dumb'" (I've changed the delimiter to be something other than whitespace). If i use that value in a command in awk... (2 Replies)
Discussion started by: IvanMalison
2 Replies

5. Shell Programming and Scripting

insert "_" before uppercase letter with awk

Hi, I am trying to insert a "_" character in front of capital letters in a string. For instance: HappyDaysAreHereAgain needs to become Happy_Days_Are_Here_Again I know how to do this with sed, but I am writing an awk script, which does several things. Also, I have already set FS in order... (9 Replies)
Discussion started by: kato
9 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. Shell Programming and Scripting

Additional question to "awk to replace particular field"

I guess it was getting a little messy on the other post so here goes: Link to previous post for Question: https://www.unix.com/shell-programming-scripting/111338-awk-replace-particular-field.html Continuation of Question hey i was messing around a bit ... made me wonder... If the... (1 Reply)
Discussion started by: VGR
1 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question