Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi
I would like to extract a field from a log file,
eg: Backup|5.4.8 5.2.4|OK|Fri Aug 8 21:30:13 2008|172.29.12.50/489c9ec5|2|11|01:53:46|92975371081/92975371081/1177124864|00:17:08:5b:fa:54|gnslnx|Linux5.2.4the only field i need is after the first /
i.e 489c9ec5
I tried this :
tail -1 /LOG|awk -F"/" '{print $2}'
but get all extra stuff as well:
489c9ec5|2|11|01:53:46|92975371081Any Help will be apreciated
thanks
Hamim

awk: option requires an argument -- F
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options:
-f progfile --file=progfile
-F fs --field-separator=fs
-v var=val --assign=var=val
-m[fr] val
-W compat --compat
-W copyleft --copyleft
-W copyright --copyright
-W dump-variables[=file] --dump-variables[=file]
-W gen-po --gen-po
-W help --help
-W lint[=fatal] --lint[=fatal]
-W lint-old --lint-old
-W non-decimal-data --non-decimal-data
-W profile[=file] --profile[=file]
-W posix --posix
-W re-interval --re-interval
-W source=program-text --source=program-text
-W traditional --traditional
-W usage --usage
-W version --versionTo report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.Examples:
gawk '{ sum += $1 }; END { print sum }' file
gawk -F: '{ print $1 }' /etc/passwd

$ echo "Backup|5.4.8 5.2.4|OK|Fri Aug 8 21:30:13 2008|172.29.12.50/489c9ec5|2|11|01:53:46|92975371081/92975371081/1177124864|00:17:08:5b:fa:54|gnslnx|Linux5.2." | awk -F '[|/]' '{print $6}'
489c9ec5

ghostdog:
I used the while loop because my awk version does not support multiple field separators. I believe it's a GNU awk extension.

nails,
with awk, you almost never have to use the shell's internal loops to do file processing# awk -F"/" 'END{sub(/\|.*/,"",$2);print $2 }' file 489c9ec5but if using the loop in shell, ksh (and bash) supports set --
IFS="/" while read a b do OFS=$IFS IFS="|" set -- $b echo $1 echo $2 IFS=$OFS done < file
so in this case, there is no need to call external commands like awk.

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

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