File manipulation

Hi ,

I am new to unix .
I have some issues while File manipulation .

I have file which is having block of data like

USERID 'XYX'
PASSWORD %12312323
CVZ dddas
ADDRESS chbjhcd

USERID 'BCD'
PASSWORD %1s312323
CVZ dddas
ADDRESS chbjhcd

There are thousands of entries like these . I want to replace USERID value with smaller caps in password line . new file should have something line

USERID 'XYX'
PASSWORD xyz
CVZ dddas
ADDRESS chbjhcd

USERID 'BCD'
PASSWORD bcd
CVZ dddas
ADDRESS chbjhcd

I am doing read line -
then through awk reading first word and if that word if USERID then changing it to lower case through "tr"

now I have the lower case value then How I can move to next line by using " while read" . I am able to get only one line at a time.

My sequence of Line is fixed , like USERID and PASSWORD Lines come one after another

Any help ? ..I will be posting my code shortly ...but meanwhile can some help ?

That's a lot of fork/exec. I am a sed fan (\t is the tab key), but I am sure the real awk and PERL guys can do better:

sed '
  :loop
  $b x
  N
  /\n[ \t]*$/b x
  b loop
  :x
  s/\n/\~/g
  s/^\(USERID .\([^~]*\).\~PASSWORD \)[^~]*\~/\1\2\~/
  s/\`/\
/g
 ' $in_file | sed '
  /^PASSWORD /{
    s/^PASSWORD //
    y/QWERTYUIOPASDFGHJKLZXCVBNM/qwertyuiopasdfghjklzxcvbnm/
    s/^/PASSWORD /
   }
 '

Narrative: First sed loops collecting lines in the buffer, changes the linefeeds to '~' escaped, as it a a sed metachar, with '\', so it can change the password to uppercase USERID, then puts the linefeeds back. Reads file and writes pipe to second sed. Second sed find the Password line, removes ths word PASSWORD, converts the case down, and replaces the word PASSWORD.

Ok, here we go :):

awk '
/USERID/{p=$2;gsub("\047","",p);p=tolower(p)}
/PASSWORD/{$2=p}
1' file > newfile

hey thanks ...

Don't have my office network yet ...

but can you explain this code .. .if possible ...

Thanks
Amey

awk '
/USERID/{p=$2;gsub("\047","",p);p=tolower(p)}
/PASSWORD/{$2=p}
1' file > newfile

Explanation:

/USERID/{p=$2;gsub("\047","",p);p=tolower(p)}

If the pattern "USERID" is matched, assign the 2nd field to the variable p, remove the quotes and make it lowercase.

/PASSWORD/{$2=p}

If the pattern "USERID" is matched, assign the variable p to the 2nd field.

1

Print the line, similar to {print} .

Cool it worked but I missed one one point ...Which I tried but not able to get it ...

I wanted to new password ( lowercase password ) in single quotes like 'xyz'

seems I am having issues in " ' " script ...any help on that ?

What should be the output based on your input file:

USERID 'XYX'
PASSWORD %12312323
CVZ dddas
ADDRESS chbjhcd

USERID 'BCD'
PASSWORD %1s312323
CVZ dddas
ADDRESS chbjhcd

ohh sorry I got it .. I removed
gsub("\047","",p)

---------- Post updated at 11:29 AM ---------- Previous update was at 11:20 AM ----------

ohh I got it ..I removed following conversion ...
gsub("\047","",p)

Just take out the part that removed them:

gsub("\047","",p)

That is the nice thing about formatted code and narratives; it is easier to understand the modular functions being called.