UNIX Shell Script Help for pattern search


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX Shell Script Help for pattern search
# 1  
Old 08-18-2015
Question UNIX Shell Script Help for pattern search

Hi
Need help for below coding scenario.

I have a file with say 4 lines as below.

Code:
DEFINE JOB TPT_LOAD_INTO_EMP_DET
( TDPID = @TPT_TDSERVER , USERNAME = @TPT_TDUSER ) ;
( 'DROP TABLE '||@TPT_WRKDB ||'.LOG_'||@TPT_TGT ||' ; ') ,
SELECT * FROM OPERATOR (FILE_READER[1]) ; ) ;

Now I want to find all those words separately from every line starting with "@" and ending with " " and write them in a file

Example: my out file should contain
Code:
TPT_TDSERVER
TPT_TDUSER
TPT_WRKDB
TPT_TGT

Please help me as it is urgent. I tried to achieve this using grep, awk and sed but could not reach to the expected result.

Thanks in advance
Santanu

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags as required by the rules. Thank you.

Last edited by bakunin; 08-18-2015 at 08:30 AM..
# 2  
Old 08-18-2015
Please use code tags as required by forum rules!

And, show us your vain attempts so we can help.
# 3  
Old 08-18-2015
Quote:
Originally Posted by Santanu2015
Code:
DEFINE JOB TPT_LOAD_INTO_EMP_DET
( TDPID = @TPT_TDSERVER , USERNAME = @TPT_TDUSER ) ;
( 'DROP TABLE '||@TPT_WRKDB ||'.LOG_'||@TPT_TGT ||' ; ') ,
SELECT * FROM OPERATOR (FILE_READER[1]) ; ) ;

Now I want to find all those words separately from every line starting with "@" and ending with " " and write them in a file

Example: my out file should contain
Code:
TPT_TDSERVER
TPT_TDUSER
TPT_WRKDB
TPT_TGT

A small sed-loop chopping off one word after the other:
Code:
sed -n ':loop
/@/ {
  s/^[^@]*//
  s/^@\([^ ][^ ]*\) /\1\n/
  P
  D
  b loop
}' /path/to/your/file

I hope this helps.

bakunin
# 4  
Old 08-18-2015
Quote:
Originally Posted by Santanu2015
[..]
Now I want to find all those words separately from every line starting with "@" and ending with " " and write them in a file
[..]
Try:
Code:
awk 'NR>1{print $1}' RS=@ file>newfile





----
Quote:
Please help me as it is urgent[..]
Everyone at the UNIX and Linux Forums gives their best effort to reply to all questions in a timely manner. For this reason, posting questions with subjects like "Urgent!" or "Emergency" and demanding a fast reply are not permitted in the regular forums.

For members who want a higher visibility to their questions, we suggest you post in the Emergency UNIX and Linux Support Forum. This forum is given a higher priority than our regular forums.

Posting a new question in the Emergency UNIX and Linux Support Forum requires forum Bits. We monitor this forum to help people with emergencies, but we do not not guarantee response time or best answers. However, we will treat your post with a higher priority and give our best efforts to help you.

If you have posted a question in the regular forum with a subject "Urgent" "Emergency" or similar idea, we will, more-than-likely, close your thread and post this reply, redirecting you to the proper forum.

Of course, you can always post a descriptive subject text, remove words like "Urgent" etc. (from your subject and post) and post in the regular forums at any time.


Thank you.

The UNIX and Linux Forums
# 5  
Old 08-18-2015
Try also
Code:
grep -o "@[^ ]* " file
@TPT_TDSERVER 
@TPT_TDUSER 
@TPT_WRKDB 
@TPT_TGT

If the "@" needs to go away, pipe it through tr -d @
# 6  
Old 08-18-2015
Thanks to all of you. What I tried initially was ,

1. grep -o '@' <full file name>
2. grep "@" <full file name> | awk -F 'FS' 'BEGIN{FS="@"}{for (i=1;i<=NF;i++) {printf $i FS}}'

and in sed it was not so handy.

I was not able to get the result before which I am getting now after help from you guys. Thanks again.

Santanu

Last edited by Corona688; 08-18-2015 at 12:30 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. UNIX for Beginners Questions & Answers

How to zip csv files having specific pattern in a directory using UNIX shell script?

I have files in a Linux directory . Some of the file is listed below -rw-rw-r--. 1 roots roots 0 Dec 23 02:17 zzz_123_00000_A_1.csv -rw-rw-r--. 1 roots roots 0 Dec 23 02:18 zzz_121_00000_A_2.csv -rw-rw-r--. 1 roots roots 0 Dec 23 02:18 zzz_124_00000_A_3.csv drwxrwxr-x. 2 roots roots 6 Dec 23... (4 Replies)
Discussion started by: Balraj
4 Replies

3. Shell Programming and Scripting

Shell Scripting , need to search and print a line that contains a specific pattern

Take example of below file. abc.txt nas1:/abc/test/test1 /test nas1:/abc/test/test1/test2 /test/abc nas1:/abc/test/ Now i have a variable that contains "nas1:/abc/test/test1" value , so i need to search the above file for this variable and print only this line. ... (14 Replies)
Discussion started by: mohit_vardhani
14 Replies

4. Shell Programming and Scripting

UNIX shell script to search a string in a file

Hi folks, I am new for shell script, I hope somebody to help me to write shell script My requirement is below steps 1. I have apache access.log i.e located in /var/log/httpd/ Ex. 127.0.0.1 - - "GET... (14 Replies)
Discussion started by: Chenchireddy
14 Replies

5. Shell Programming and Scripting

Pattern search (regular expression in UNIX)

Hello , Could anyone help me to define the string in regular expression way . Below is my string \rtf1\ansi\deff0{\fonttbl{\f0\fswiss Helv;}{\f1\fnil MS Sans Serif;}} {\colortbl ;\red0\green0\blue0;} \viewkind4\uc1\pard\cf1\lang1033\f0\fs16 The string will always start as \rtf1 and... (6 Replies)
Discussion started by: Pratik4891
6 Replies

6. UNIX for Dummies Questions & Answers

Pattern search using UNIX command

Hi I would like to grep a text with pattern starting with $$WS_S. and ending with a delimiter space or comma.. please help me with the syntax Eg: aaaaaaaaasdjkghg $$WS_S.table1 sdef dsf $$WS_S.table2,sdfdff $$WS_S.table3 dasssssssssds Use code tags please, see PM. (3 Replies)
Discussion started by: Codesearcher
3 Replies

7. Shell Programming and Scripting

Shell script to search a pattern in a directory and output number of find counts

I need a Shell script which take two inputs which are 1) main directory where it has to search and 2) pattern to search within main directory all files (.c and .h files) It has to print number of pattern found in main directory & each sub directory. main dir --> Total pattern found = 5 |... (3 Replies)
Discussion started by: vivignesh
3 Replies

8. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

9. Shell Programming and Scripting

Search for awk pattern in unix env variable

For a Script I need to detemine which field of the unix environment variable SHLIB_PATH has the WALTDB entry. export SHLIB_PATH=/usr/user5/WALTDB/oracle/product/10.2.0/lib32:/usr/TZD/bin.wdad/mug/oracle/lib: echo $SHLIB_PATH | awk -F: '{ print $1 }' Shure gives me the first entry, but... (6 Replies)
Discussion started by: sdohn
6 Replies

10. HP-UX

extract field of characters after a specific pattern - using UNIX shell script

Hello, Below is my input file's content ( in HP-UX platform ): ABCD120672-B21 1 ABCD142257-002 1 ABCD142257-003 1 ABCD142257-006 1 From the above, I just want to get the field of 13 characters that comes after 'ABCD' i.e '120672-B21'... . Could... (2 Replies)
Discussion started by: jansat
2 Replies
Login or Register to Ask a Question