awk gsub(): general regex


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk gsub(): general regex
# 1  
Old 04-02-2012
awk gsub(): general regex

%%%%%

Last edited by lucasvs; 05-01-2012 at 06:15 AM..
# 2  
Old 04-02-2012
Try regular sub (gsub not necessary) with /[^0-9]*$/ and replace with "."
--
**edit** this is incorrect, since string to delete may also contain numbers

Last edited by Scrutinizer; 04-02-2012 at 06:33 AM..
# 3  
Old 04-02-2012
Code:
awk '{print gensub (/(.+[0-9]+[^0-9]+[0-9]+).*\./,"\\1.","g")}'

or
Code:
sed  's/\(.\+[0-9]\+[^0-9]\+[0-9]\+\).*\./\1./g'

(GNU awk/sed)

Last edited by CarloM; 04-02-2012 at 06:49 AM.. Reason: Keep the second number
# 4  
Old 04-02-2012
Code:
awk '{$3=$3"."}NF=3'

# 5  
Old 04-02-2012
%%%%%

Last edited by lucasvs; 05-01-2012 at 06:15 AM..
# 6  
Old 04-03-2012
Well, they worked on your original example Smilie.

EDIT:
Try:
Code:
Carlo@CRM-MT4 ~
$ echo "AAAA 1 2 f45 iop 234-RT.; AAAA 123 456 gf kuygb 2iuygb.; AAAA 678 89 wer4k lh8." |
> awk  '{print gensub (/([[:alnum:]]+[[:blank:]]+[[:digit:]]+[[:blank:]]+[[:digit:]]+)[^.;]*/,"\\1","g")}'
AAAA 1 2.; AAAA 123 456.; AAAA 678 89.

Carlo@CRM-MT4 ~
$ echo "AAAA 1 2 f45 iop 234-RT.; AAAA 123 456 gf kuygb 2iuygb.; AAAA 678 89 wer4k lh8." |
>  sed  's/\([[:alnum:]]\+[[:blank:]]\+[[:digit:]]\+[[:blank:]]\+[[:digit:]]\+\)[^.;]*/\1/g'
AAAA 1 2.; AAAA 123 456.; AAAA 678 89.


Last edited by CarloM; 04-03-2012 at 04:31 AM..
This User Gave Thanks to CarloM For This Post:
# 7  
Old 04-04-2012
%%%%%

Last edited by lucasvs; 05-01-2012 at 06:16 AM..
 
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