Re: awk get length of pattern and append text
stephenfu1@xxxxxxxxx wrote:
Hi
I wish to use awk or sed to perform this operation.
I want to look at each line of a file and get the length of a string
that is enclosed by "FORENAME1=" and "," or "}". If the string is equal
to 1 character in length then append FORENAME1_INITIAL to the end of
the line.
e.g
data file:
PARAMETERS={FORENAME1=S, SURNAME=mysurname} |
PARAMETERS={FORENAME1=S} |
PARAMETERS={FORENAME1=Steve} |
would generate the output
PARAMETERS={FORENAME1=S, SURNAME=mysurname} | FORENAME1_INIT
PARAMETERS={FORENAME1=S} | FORENAME1_INIT
PARAMETERS={FORENAME1=Steve} |
I've been looking at using awk but am a bit confused. Not really sure
on how to build up the code. I believe /FORENAME1=.*(,|})/ will match
lines, but i want to actually get at the contained text. Then check if
the string "FORENAME1=S" is equal to 11 characters in lenght. If so
then it must be an initial then append the appropriate text to the end
of the line - havent got that code working either!
awk '
/FORENAME1=.*(,|})/ {
if (length(???)=11) $0 = $0 "_FORENAME1INIT";
}
{print}' < infile > outfile
Any help would be appreciated
This is the direction you were going in:
$ cat file
PARAMETERS={FORENAME1=S, SURNAME=mysurname} |
PARAMETERS={FORENAME1=S} |
PARAMETERS={FORENAME1=Steve} |
$ cat forename.awk
function extract(str,regexp)
{ RMATCH = (match(str,regexp) ? substr(str,RSTART,RLENGTH) : "")
return RSTART
}
extract($0,"FORENAME1=[^,}]*") && length(RMATCH) == 11 {
$0 = $0 " FORENAME1_INIT"
}
1
$ awk -f forename.awk file
PARAMETERS={FORENAME1=S, SURNAME=mysurname} | FORENAME1_INIT
PARAMETERS={FORENAME1=S} | FORENAME1_INIT
PARAMETERS={FORENAME1=Steve} |
but this would work just as well for the sample data you posted:
$ awk -F"[={,}]*" '$3~/^.$/{$0=$0" FORENAME1_INIT"}1' file
PARAMETERS={FORENAME1=S, SURNAME=mysurname} | FORENAME1_INIT
PARAMETERS={FORENAME1=S} | FORENAME1_INIT
PARAMETERS={FORENAME1=Steve} |
Regards,
Ed.
.
Relevant Pages
- Re: file delete routine is intermittent
... >> terminator character to every string. ... > Only when you pass a string as a parameter. ... VB does NOT automatically append a null ... (microsoft.public.vb.winapi) - Re: file delete routine is intermittent
... >>> terminator character to every string. ... >> Only when you pass a string as a parameter. ... >> character to string members of structures so you must append the null ... (microsoft.public.vb.winapi) - Re: Append characters to file name to establish uniqueness
... I am trying to keep a set of files online and append a character ... string to each file in order to establish uniqueness to each file name ... (comp.os.vms) - Re: Pound symbol in a string
... > When I use system.IO.StreamWriter to write append a string to file that ... > contains the GBP pound symbol, I notice that it also appends an extra ... Yet when I examine the string character ... (microsoft.public.dotnet.languages.vb) - [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ... (Linux-Kernel) |
|