Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
All,
I am using the following command to extract certain fields from some output...bperror -problems | grep $server | awk '{i=index ($0,"C:"); j=index ($0,"file:"); x=j+1; if (i!=0) print substr($0,i,x);}'
The data I want is in the form C:\Winnt\System32\Config.....
The problem is that the file paths in the data don't always start with C:
There may be multiple file paths...(ie..C:\
D:\, E:\, etc...)
What value do I need to put in the i=index ($0," ")field to grab all file paths starting with a capital letter followed by a colon? What I am doing is keying off the word "file:" in the j=index part of the command and the next field is the file path I want to print.
Any ideas?
Mike B

The index function will locate a string, but the match function will locate an expression:
echo "file: C:\Winnt\System32\Config" |
awk '{print match($0,"[A-Z]:")}'7

James,
The above command didn't work. Is there a way to print just the fields in a line that begin with a capital letter followed by a colon?
Thanks!!!

Mike,
Do you mean that command did not work in the context of what you are trying to do, or it did not work precisely as posted?
If it did not work as posted, then what result or error did you get? It should return the digit 7, as shown. To get the match functionality, you might need to run nawk instead of awk.
The following code will print one line for each word in the file being processed that begins with uppercase letter followed by a colon:
echo "f1 C:f2 d:f3 E:f4 :f5 f6 x:f7" |
awk '{for (i=1;i<=NF;i++)
if (match($i,"[A-Z]:"))
print "word" i "=" $i
}'word2=C:f2
word4=E:f4

James,
I used the following command and have almost everything I need, thanks to your feeddback...At the command line I type...
sudo bperror -problems|grep $server|nawk '{for (i=1;i=NF;i++) if (match($i,"[A-Z]:")) print $i}'
The output is as follows...
C:\Winnt\system32\config
C:\Program
This is almost what I want. Those are the files on the server ($server) which were not
backed up. The problem is that if you look at the second file listed...(C:\Program)there is really more to that path. There is a space after the word Program followed by the word Files\...\...
How can I get the command to print the whole path including spaces in the directory names?
Almost there,
Mike

A little tricky. Since awk needs to delimit your fields with white space, those spaces embedded in some of your fields make those appear as multiple fields.
You can tell awk to use another delimiter that would not appear in your data, such as the | character.
Then awk can:
- change all spaces to new delimiter
- undo certain of these changes
- process each field as beforeDefault whitespace delimiting recognizes both spaces and tabs as delimiters. I am assuming only spaces in this case.
#!/bin/sh
awk -F"[|]*" '{
print "NF=" NF " " $0
gsub(" ","|")
print "NF=" NF " " $0
gsub("Program\|Files","Program Files")
print "NF=" NF " " $0
for (i=1;i<=NF;i++)
if (match($i,"[A-Z]:"))
print "field" i "=" $i
}' Mike.txt
exit 0NF=1 f1 C:\Program Files\f2 f3
NF=4 f1|||C:\Program|Files\f2|||f3
NF=3 f1|||C:\Program Files\f2|||f3
field2=C:\Program Files\f2I put print statements to show the progress as it processes each line. These show how many fields it currently recognizes as well as the entire line.
If I had specified a single bar character as the delimiter, it would have seen 1/8/7 fields instead of 1/4/3. But I specified "one or more bar characters" to stay closer to the default whitespace delimiting.

James,
How can I get the whole path to a file that
has embeded white spaces without knowing the
file path? What I need is to extract any path starting with a captial letter followed
by a colon that appears in my output. I need
the entire path which would include white
spaces in the path. ie...C:\Program files\documents\Mike's docs an letters. Does awk know when the file ends?
Thanks,
Mike

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |