awk gsub(): general regex


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk gsub(): general regex
# 8  
Old 04-05-2012
Now there a pipeline symbols. It is good practice if you try to specify the input specification correctly right from the start...

Alright, this almost works except for the last character:
Code:
awk -F\; '{for(i=1;i<=NF;i++)if($i~/[[:alnum:]]/){sub(/ */,x,$i);split($i,T,"[ ]");$i=T[1]" "T[2]" "T[3]"."}}1' OFS=\; RS=\| ORS=\| infile

Code:
||AAAA 1 2.; AAAA 3.|AAAA 5 6.||
||BBBB 7 8.; BBBB 9.||BBBB 11 12.|


Last edited by Scrutinizer; 04-05-2012 at 04:04 AM.. Reason: removed leading space from fields
# 9  
Old 04-05-2012
I just try to understand why gensub keeps apply the occurrence ("\\1"), even when it starts from a new line.

Is there any way to "reset" gensub every time it starts reading a new line?
# 10  
Old 04-05-2012
Code:
Carlo@CRM-MT4 ~
$ echo "||AAAA 1 2.; AAAA 3.|AAAA 5 6.||" |  awk '{print gensub (/([^[:blank:]]+[[:blank:]]+[[:digit:]]+[[:blank:]]+[[:digit:]]+)[^.;\|]*/,"\\1","g")}'
||AAAA 1 2.; AAAA 3.|AAAA 5 6.||

Carlo@CRM-MT4 ~
$ echo "|BBBB 7 8 f ugbi4t.; BBBB 9 10 gv3 fv3.||BBBB 11 12 rytr." |
> awk '{print gensub (/([^[:blank:]]+[[:blank:]]+[[:digit:]]+[[:blank:]]+[[:digit:]]+)[^.;\|]*/,"\\1","g")}'
|BBBB 7 8.; BBBB 9 10.||BBBB 11 12.

EDIT: Actually, since you're using | as FS the original regex should work - but the one you have in the code is not the same (and is syntactically incorrect - copy/paste error?)
Code:
$i = gensub (/({{:alnum:]]+[[:digit:]]+[[:blank:]]+[[:digit:]]+)([^.;]*)/,"\\1","g",$i)

EDIT2: It does parse (somewhat surprisingly), but it's not doing anything (nothing matches {{:alnum) - $i is unchanged.

Last edited by CarloM; 04-05-2012 at 05:03 AM..
This User Gave Thanks to CarloM For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk gsub with variables

Hello, I'm trying to substitute a string with leading zero for all the records except the trailer record using awk command and with variables. The input file test_med1.txt has data like below 1234ABC...........................9200............LF... (2 Replies)
Discussion started by: somu_june
2 Replies

2. UNIX for Dummies Questions & Answers

Gsub regex not working

I have a number of files that I pass through awk/gsub. I believe to have found a working regex and on 'test bed' sites it matches, however within gsub it does not. Examples: Initial data: /Volumes/Daniel/Public/Drop Box/_Hellsing_Ultimate_OVA_-_10_.mkv gsub & regex: gsub("\]+\]","" ... (4 Replies)
Discussion started by: unknownn
4 Replies

3. Shell Programming and Scripting

awk gsub

Hi, I want to print the first column with original value and without any double quotes The output should look like <original column>|<column without quotes> $ cat a.txt "20121023","19301229712","100397" "20121023","19361629712","100778" "20121030A","19361630412","100838"... (3 Replies)
Discussion started by: ysrini
3 Replies

4. Shell Programming and Scripting

GSUB/Regex Help

I am trying to write my gsub regex to replace a bunch of special characters with spaces, so i can split it to an array and look at each word independently. However, my regex skills are slightly lacking and I appear to be missing a quote or something here. I am trying to replace the following... (6 Replies)
Discussion started by: nitrobass24
6 Replies

5. Shell Programming and Scripting

Help with awk and gsub using C shell

Being new to awk, I am still running into little stupid things. For this issues I am trying to search for all occurrences of a string in a file and replace all of those occurrences with a replacement string. I tried doing awk '{gsub("|750101|", "|000000|", $0)}' infile > outfile Unix... (3 Replies)
Discussion started by: jclanc8
3 Replies

6. Shell Programming and Scripting

Awk gsub error.

I want to replace comma with space and "*646#" with space. I am using the following code: nawk -F"|" '{gsub(","," ",$3); gsub(/\*646\#/"," ",$3);print}' OFS="|" file I am getting following error: Help is appreciated (5 Replies)
Discussion started by: pinnacle
5 Replies

7. Shell Programming and Scripting

awk gsub

Hi all I want to do a simple substitution in awk but I am getting unexpected output. My function accepts a time and then prints out a validation message if the time is valid. However some times may include a : and i want to strip this out if it exists before i get to the validation. I have shown... (4 Replies)
Discussion started by: pxy2d1
4 Replies

8. Shell Programming and Scripting

Help with AWK and gsub

Hello, I have a variable that displays the following results from a JVM.... 1602100K->1578435K I would like to collect the value of 1578435 which is the value after a garbage collection. I've tried the following command but it looks like I can't get the > to work. Any suggestions as... (4 Replies)
Discussion started by: npolite
4 Replies

9. Shell Programming and Scripting

use var in gsub of awk

Hi all, This problem has cost me half a day, and i still do not know how to do. Any help will be appreciated. Thanks advance. I want to use a variable as the first parameters of gsub function of awk. Example: { ... arri]=gsub(i,tolower(i),$1) (which should be ambraced by //) ... } (1 Reply)
Discussion started by: summer_cherry
1 Replies
Login or Register to Ask a Question