Operating System - HP-UX
1748227 Members
4422 Online
108759 Solutions
New Discussion юеВ

Re: Parsing the dreaded backslash

 
SOLVED
Go to solution
John Jeffrey
Occasional Advisor

Parsing the dreaded backslash

I'm parsing a comma seperated file containing Windows account information. The fields are as follows:

UserName,FullName,AccountType,Comment,HomeDrive,HomeDir,Profile,LogonScript, etc...

The problem is the HomeDir field. It often contains backslashes, such as \\servername\share. The problem is that the \ character screws up the parse.

For example:

cat windowspasswd.work | while read line
do
username=`echo $line | awk -F, '{print $1}'`
fullname=`echo $line | awk -F, '{print $2}'`
pwdlastchanged=`echo $line | awk -F, '{print $11}'`
pwdlifetime=`echo $line | awk -F, '{print $16}'`
disabled=`echo $line | awk -F, '{print $15}'`
pwdexpires=`echo $line | awk -F, '{print $13}'`
.
.
.
I get username and fullname correctly, but after that they all come up blank.

Oddly, when I run the echo $line | awk commands from the command line, they DO work. But they don't in the script. I know it's the backslashes, because if I blank out the HomeDir field, the line is parsed fine.

Running:
cat users.xls | grep 44SSFAC,
Shows:
44SSFAC,44 Simon St. Facilities,User,#037919 see Lou Guerci,H:,\\cadillac\44ssfaC$,,login.bat,,No ,etc...

Note the double \\ in front of Cadillac -- as it should be.

Running:
line=`cat users.xls | grep 44SSFAC,`; echo $line
Shows:
44SSFAC,44 Simon St. Facilities,User,#037919 see Lou Guerci,H:,\cadillac\44ssfaC$,,login.bat,,No ,etc...

Note the single \ in front of "Cadillac".

This is only to highlight that $line isn't receiving the proper value of the field. It's interpretting the \, and causing issues.

And I don't for the life of me understand why the echo $line | awk commands work from the command line, but fail in the script.

Any ideas? I don't care about the \ at all, nor the field it's in. But it's screwing up the parse and I want it out of the picture.

Thanks.
6 REPLIES 6
Mark Greene_1
Honored Contributor

Re: Parsing the dreaded backslash

Try changing the echos to print -r to display the data raw instead of letting the shell do it's normal translation routine on it.

mark
the future will be a lot like now, only later
John Poff
Honored Contributor
Solution

Re: Parsing the dreaded backslash

Hi,

One way to do it would be to use 'tr' to delete all the '\' characters in your input. Something like this might work:

tr -d '\\' do
username=`echo $line | awk -F, '{print $1}'`
fullname=`echo $line | awk -F, '{print $2}'`
pwdlastchanged=`echo $line | awk -F, '{print $11}'
...


JP
Rodney Hills
Honored Contributor

Re: Parsing the dreaded backslash

I would use perl in this situation-

perl -ne 'chomp;@a=split(",");print join(",",@a[0,1,10,15,14,12]),"\n";' users.xls

This will split each record at "," then rejoin just the field you are interested in.

HTH

-- Rod Hills
There be dragons...
c_51
Trusted Contributor

Re: Parsing the dreaded backslash

an easier and faster method would be:

cat windowspasswd.work |
awk -F"," '{
printf("%s %s %s %s %s %s\n",$1,$2,$11,$16,$15,$13);
}' |
while read username fullname pwdlastchanged pwdlifetime disabled pwdexpires
do
...
done
John Jeffrey
Occasional Advisor

Re: Parsing the dreaded backslash

Thanks for the responses!

print -r worked, but I need to port the script to Solaris... which doesn't recognize "print". :-/

The tr -d '\\'
Sorry, the solution has to work in the bourne shell. Perl wasn't an option.

I couldn't get the
cat windowspasswd.work |
awk -F"," '{
printf("%s %s %s %s %s %s\n",$1,$2,$11,$16,$15,$13);
Thing to work right. It ran and it ran much faster, but it seemed to be interpreting the spaces as well as commas as field seperators. I played around with it, but couldn't get it work properly.

Thanks again!
Bill Hassell
Honored Contributor

Re: Parsing the dreaded backslash

Another thing to note is that echo'ing or otherwise using $line should be surrounded by "" as in echo "$line" in order to escape the special meanings of special characters.


Bill Hassell, sysadmin