Sponsored Content
Top Forums UNIX for Dummies Questions & Answers how to grep the word and display only the second word from it Post 302263370 by drl on Monday 1st of December 2008 12:11:15 PM
Old 12-01-2008
Hi.

Here is an awk solution. It first breaks lines at string getdate, then splits a resulting field at occurrences of a comma:
Code:
#!/usr/bin/env sh

# @(#) a1       Demonstrate field splitting with awk.

#  ____
# /
# |   Infrastructure BEGIN

set -o nounset
echo

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

## Use local command version for the commands in this demonstration.

set +o nounset
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) awk
set -o nounset

echo

FILE=${1-data1}
echo " Input file $FILE:"
cat $FILE

# Use nawk or /usr/xpg4/bin/awk on Solaris.

# |   Infrastructure END
# \
#  ---

echo
echo " Results from awk (initially string, then comma):"
awk -F'getdate' '
BEGIN   { print "FS is :" FS ":" }
        { split($2,_a,",")
          print _a[3]
        }
' $FILE

exit 0

Producing:
Code:
% ./a1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU Awk 3.1.4

 Input file data1:
'Y',getdate(),'N','V',NULL .....
'N',getdate(),'Y','D',NULL .....
'Y','N','Y',getdate(),'Y','D',NULL ....
values('SGAV01204','retention_cd','retention_cd','C','EQUALS','N',-999, NULL,NULL,'N','N',NULL, NULL,'SYSTEM',getdate(),'0','D','0','S','N',0,'N','N','00',NULL1,NULL2,'N',NULL3,NULL4,NULL)

 Results from awk (initially string, then comma):
FS is :getdate:
'V'
'D'
'D'
'D'

Best wishes ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

grep a word and display its column

Hi, I need idea about this, say I have this line: 05 21 * * 0,6 /user/clean.desktop.sh > /tmp/desktop_rpt 2>&1 I would need to grep the word desktop and display the /user/clean.desktop.sh and not the whole line. And if I have some more lines say, 05 21 * * 0,6 /user/clean.desktop.sh >... (1 Reply)
Discussion started by: Orbix
1 Replies

2. UNIX for Dummies Questions & Answers

how to grep for a word and display only the word

Hi, When we "grep" for a word in a file, it returns the lines containing the word that we searched for. Is there a way to display only the words and not the entire line containing them. Thanks Ananth (6 Replies)
Discussion started by: ananthmm
6 Replies

3. Shell Programming and Scripting

How to Grep for particular word and display..

Hi Guru's.... I've one log file in all my systems which writes the backup information.. I'have written a command like this: ssh -l ora${sid} ${primaryhost} "tail -50 /oracle/$ORACLE_SID/newbackup/END_BACKUP.log" |grep 'insert' |tail -1| awk '{print $7}' We have nearly 50 systems in our... (2 Replies)
Discussion started by: suri.tyson
2 Replies

4. Shell Programming and Scripting

grep display word only

Folks, is it possible to display only words with grep (or any built-in ultility)? I have more than 1 pattern to search, say apple & orange The text goes like this: So I need to display all the words starting with apple or orange The output should be: Any idea? (7 Replies)
Discussion started by: bsddaemon
7 Replies

5. Shell Programming and Scripting

Grep out specific word and only that word

ok, so this is proving to be kind of difficult even though it should not be. say for instance I want to grep out ONLY the word fkafal from the below output, how do I do it? echo ajfjf fjfjf iafjga fkafal foeref afoafahfia | grep -w "fkafal" If i run the above command, i get back all the... (4 Replies)
Discussion started by: SkySmart
4 Replies

6. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

7. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

8. Shell Programming and Scripting

Grep word after last occurance of string and display next few lines

Hi, I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below... (17 Replies)
Discussion started by: nes
17 Replies

9. Shell Programming and Scripting

Grep for a word or word with underscore

I have a file "test" with following contents: cat test abc abcd_efg abc_abc I want to only grep for abc or abc_ without getting other results, how do I achieve this? If I use grep -w abc test option I get only abc and not abc_. If I use egrep "abc|abc_" test its still printing... (3 Replies)
Discussion started by: ctrld
3 Replies

10. Shell Programming and Scripting

How to grep for a word and display last transection?

Hi, When we "grep" for a word in a file, it returns the last lines containing the word that we searched for. Is there a way to display last line to grep. Thanks Ex log. Ex. logname.log 2015-07-29 06:43:07.023|BETA |2015-07-29... (5 Replies)
Discussion started by: ooilinlove
5 Replies
GETDATE(3)						     Linux Programmer's Manual							GETDATE(3)

NAME
getdate, getdate_r - convert a date-plus-time string to broken-down time SYNOPSIS
#include <time.h> struct tm *getdate(const char *string); extern int getdate_err; #include <time.h> int getdate_r(const char *string, struct tm *res); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): getdate(): _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED getdate_r(): _GNU_SOURCE DESCRIPTION
The function getdate() converts a string representation of a date and time, contained in the buffer pointed to by string, into a broken- down time. The broken-down time is stored in a tm structure, and a pointer to this structure is returned as the function result. This tm structure is allocated in static storage, and consequently it will be overwritten by further calls to getdate(). In contrast to strptime(3), (which has a format argument), getdate() uses the formats found in the file whose full pathname is given in the environment variable DATEMSK. The first line in the file that matches the given input string is used for the conversion. The matching is done case insensitively. Superfluous whitespace, either in the pattern or in the string to be converted, is ignored. The conversion specifications that a pattern can contain are those given for strptime(3). One more conversion specification is specified in POSIX.1-2001: %Z Timezone name. This is not implemented in glibc. When %Z is given, the structure containing the broken-down time is initialized with values corresponding to the current time in the given timezone. Otherwise, the structure is initialized to the broken-down time corresponding to the current local time (as by a call to local- time(3)). When only the weekday is given, the day is taken to be the first such day on or after today. When only the month is given (and no year), the month is taken to be the first such month equal to or after the current month. If no day is given, it is the first day of the month. When no hour, minute and second are given, the current hour, minute and second are taken. If no date is given, but we know the hour, then that hour is taken to be the first such hour equal to or after the current hour. getdate_r() is a GNU extension that provides a reentrant version of getdate(). Rather than using a global variable to report errors and a static buffer to return the broken down time, it returns errors via the function result value, and returns the resulting broken-down time in the caller-allocated buffer pointed to by the argument res. RETURN VALUE
When successful, getdate() returns a pointer to a struct tm. Otherwise, it returns NULL and sets the global variable getdate_err to one of the error numbers shown below. Changes to errno are unspecified. On success getdate_r() returns 0; on error it returns one of the error numbers shown below. ERRORS
The following errors are returned via getdate_err (for getdate()) or as the function result (for getdate_r()): 1 The DATEMSK environment variable is not defined, or its value is an empty string. 2 The template file specified by DATEMSK cannot be opened for reading. 3 Failed to get file status information. 4 The template file is not a regular file. 5 An error was encountered while reading the template file. 6 Memory allocation failed (not enough memory available). 7 There is no line in the file that matches the input. 8 Invalid input specification. ENVIRONMENT
DATEMSK File containing format patterns. TZ, LC_TIME Variables used by strptime(3). CONFORMING TO
POSIX.1-2001. NOTES
The POSIX.1-2001 specification for strptime(3) contains conversion specifications using the %E or %O modifier, while such specifications are not given for getdate(). In glibc, getdate() is implemented using strptime(3), so that precisely the same conversions are supported by both. EXAMPLE
The program below calls getdate() for each of its command-line arguments, and for each call displays the values in the fields of the returned tm structure. The following shell session demonstrates the operation of the program: $ TFILE=$PWD/tfile $ echo '%A' > $TFILE # Full weekday name $ echo '%T' >> $TFILE # ISO date (YYYY-MM-DD) $ echo '%F' >> $TFILE # Time (HH:MM:SS) $ date $ export DATEMSK=$TFILE $ ./a.out Tuesday '2009-12-28' '12:22:33' Sun Sep 7 06:03:36 CEST 2008 Call 1 ("Tuesday") succeeded: tm_sec = 36 tm_min = 3 tm_hour = 6 tm_mday = 9 tm_mon = 8 tm_year = 108 tm_wday = 2 tm_yday = 252 tm_isdst = 1 Call 2 ("2009-12-28") succeeded: tm_sec = 36 tm_min = 3 tm_hour = 6 tm_mday = 28 tm_mon = 11 tm_year = 109 tm_wday = 1 tm_yday = 361 tm_isdst = 0 Call 3 ("12:22:33") succeeded: tm_sec = 33 tm_min = 22 tm_hour = 12 tm_mday = 7 tm_mon = 8 tm_year = 108 tm_wday = 0 tm_yday = 250 tm_isdst = 1 Program source #define _GNU_SOURCE 500 #include <time.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { struct tm *tmp; int j; for (j = 1; j < argc; j++) { tmp = getdate(argv[j]); if (tmp == NULL) { printf("Call %d failed; getdate_err = %d ", j, getdate_err); continue; } printf("Call %d ("%s") succeeded: ", j, argv[j]); printf(" tm_sec = %d ", tmp->tm_sec); printf(" tm_min = %d ", tmp->tm_min); printf(" tm_hour = %d ", tmp->tm_hour); printf(" tm_mday = %d ", tmp->tm_mday); printf(" tm_mon = %d ", tmp->tm_mon); printf(" tm_year = %d ", tmp->tm_year); printf(" tm_wday = %d ", tmp->tm_wday); printf(" tm_yday = %d ", tmp->tm_yday); printf(" tm_isdst = %d ", tmp->tm_isdst); } exit(EXIT_SUCCESS); } SEE ALSO
time(2), localtime(3), setlocale(3), strftime(3), strptime(3), feature_test_macros(7) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. 2010-09-20 GETDATE(3)
All times are GMT -4. The time now is 10:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy