Computing.Net > Forums > Unix > extract field from string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

extract field from string

Reply to Message Icon

Name: hismail
Date: August 11, 2008 at 03:23:07 Pacific
OS: Linux redhat
CPU/Ram: 2gb/4gb
Product: HP
Comment:

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.4

the 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|92975371081

Any Help will be apreciated

thanks
Hamim



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: August 11, 2008 at 07:38:59 Pacific
Reply:

#!/bin/ksh


tail -1 /LOG|
while IFS="/" read f1 f2
do
echo "$f2"|awk -F"|" '{print $1}'
done


0

Response Number 2
Name: hismail
Date: March 31, 2009 at 05:29:05 Pacific
Reply:

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 --version

To 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


0

Response Number 3
Name: hismail
Date: March 31, 2009 at 05:32:44 Pacific
Reply:

Ok not to worry Nails i got it
I removed the extra |awk -F in the first line
thnks a mil
Hamim


0

Response Number 4
Name: user57
Date: March 31, 2009 at 07:35:20 Pacific
Reply:

$ 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


0

Response Number 5
Name: ghostdog
Date: March 31, 2009 at 19:03:13 Pacific
Reply:

a while loop/tail is not needed when using awk.

 # awk -F"[/|]" 'END{ print $6 }' file
489c9ec5



0

Related Posts

See More



Response Number 6
Name: nails
Date: March 31, 2009 at 22:20:20 Pacific
Reply:

ghostdog:

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


0

Response Number 7
Name: ghostdog
Date: March 31, 2009 at 23:03:28 Pacific
Reply:

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
489c9ec5

but 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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: extract field from string

Extracting data from file using sed www.computing.net/answers/unix/extracting-data-from-file-using-sed/7660.html

Throw the last char from string. www.computing.net/answers/unix/throw-the-last-char-from-string/6547.html

Extracting information from a file www.computing.net/answers/unix/extracting-information-from-a-file/7770.html