how to grep the word and display only the second word from it


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to grep the word and display only the second word from it
# 8  
Old 12-01-2008
Hammer & Screwdriver Here is another approach

Code:
> cat file85
'Y',getdate(),'N','V',NULL .....
'N',getdate(),'Y','D',NULL .....
'Y','N','Y',getdate(),'Y','D',NULL ....
> cat file85 | sed "s/getdate()/~&1/" | tr "~" "\n" | grep "getdate()" | cut -d"," -f3
'V'
'D'
'D'

[Note: this was done purposefully LONG to help show how the commands all worked together.]
Explained
cat the file
sed to put a ~ before any instance of getdate()
tr the ~ to a new-line; forcing onto own line
grep for the getdate() to select only those lines
cut -d"," -f3 to set delimiter to , and output the third field
# 9  
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  
Old 12-03-2008
Any one please help

My Requirement:

Actually i grep for the items in atrblist (i wil get the line where the that item present and also the next line)then it will be stored in $i.txt
From tat result i wil grep 2nd word after getdate() word and store it in $i_value.txt
eg: if result of $i.txt is below,
---------------------------------------------:
..'getdate(),NULL,'D','Y',NULL.....
..'Y',NULL, getdate(), NULL,'V','Y',NULL
..'getdate(),NULL,'A','Y',NULL.....
---------------------------------------------
Then result of $i_value.txt is,
---------------------------------------------
'D'
'V'
'A'
---------------------------------------------

Then i wil check whether $i is retention_cd and for values in $i_value.txt
if the value is 'A' or 'B' ... 'Z' i wil print the echo accordingly

if $i is opt_fmt_cd and for values in $i_txt
if values is PDF, XLS ... i wil print accordingly...


Above is my requirement
but the code does not work properly can any one please check my code and let me know the correct one....

NOTE:::: atrblist will be added with som new items also.
--------------------------- CODE STARTING ------------------------------------------------------------
for i in `cat $LOC/atrblist.txt`
do
echo "Registration Validation as of :" `date` > $LOC/$i.txt
nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=1 s=$i $SQLFILE >> $LOC/$i.txt
if [ $? -eq 1 ]
then
rm -f $LOC/$i.txt
exit 1
else
chmod 775 $LOC/$i.txt
cat $LOC/$i.txt | sed "s/getdate()/~&1/" | tr "~" "\n" | grep "getdate()" | cut -d"," -f3 >> $LOC/$i_value.txt
chmod 775 $LOC/$i_value.txt
if [ $i -eq retention_cd ]
then
for j in `cat \$i\_value.txt`
do
if [ $j -eq 'A' ]
then
echo "Values is" $j "So Retention code of product is : 7 Days"
else if [ $j -eq 'B' ]
then
echo "Values is" $j "So Retention code of product is : 3 Months"
else if [ $j -eq 'C' ]
then
echo "Values is" $j "So Retention code of product is : 1 Year"
else if [ $j -eq 'D' ]
then
echo "Values is" $j "So Retention code of product is : 10 Years"
else if [ $j -eq 'Z' ]
then
echo "Values is" $j "So Retention code of product is : 1 Day"
else
echo "Values is" $j "So Retention code is NOT VALID Please register correctly - Even it can be Empty but not NULL"
fi
fi
fi
fi
fi
done
fi

if [ $i -eq opt_fmt_cd ]
then
for j in `cat \$i\_value.txt`
do
if [ $j -eq 'PDF' ]
then
echo "Values is" $j "So Optional Format code of product is : PDF"
else if [ $j -eq 'XLS' ]
then
echo "Values is" $j" So Optional Format code of product is : XLS"
else if [ $j -eq 'CSV' ]
then
echo "Values is" $j" So Optional Format code of product is : CSV"
else if [ $j -eq 'TXT' ]
then
echo "Values is" $j" So Optional Format code of product is : TXT"
else
echo "Values is" $j "So Optional Format code which is NOT VALID Please register correctly"
fi
fi
fi
fi
done
fi

fi

done
--------------------------- CODE END ------------------------------------------------------------

where,
atrblist.txt is
----------------
retention_cd
opt_fmt_cd
;;
;;
-----------------

LOC = /home/path (local path)

SQLFILE:
---------------------------------------
;;
;;
values('new','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)
values('new1','opt_fmt_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)
;;
;;
---------------------------------------
# 11  
Old 12-17-2008
if a have a line like this: wtmp begins Fri Nov 28 .....
what can i do that it displays only the first word of line, or the 3rd?
# 12  
Old 12-17-2008
oh,i've just found a solution for that:
last login | cut -d ' ' -f3
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question