Shell Script for displaying the line till the target word


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Shell Script for displaying the line till the target word
# 1  
Old 07-20-2013
Shell Script for displaying the line till the target word

" Script for display sentences with special character"
Hi,

Could any one share a command how to display a line until my target word.
For ex: My file has the content as:

Code:
select * from db_wrk where col1 < col2
insert into table_name values('1','2','tst','wrk','dev','prod')

My target search word is : wrk

Required Output is: 1
Code:
----------------------
select * from db_wrk 
insert into table_name values('1','2','tst','wrk',

It will be so good,if anyone share the command to display the output is:

Required Output is: 2
Code:
----------------------
select * from ^^^^^^db_wrk^^^^^ 
insert into table_name values('1','2','tst','^^^^^wrk^^^^',


Thanks in advance

Last edited by Scrutinizer; 07-20-2013 at 04:27 AM.. Reason: code tags
# 2  
Old 07-20-2013
Try:
Code:
perl -pe 's/([^ \047]*wrk)(\047,)?.*/^^^^^$1^^^^^$2/' file

# 3  
Old 07-20-2013
Thanks you Bartus11..May i know whether i can use this command in shell script? meanwhile i wil also try in shell.

Note: I want to hi-light: "i need unix command for shell script"
# 4  
Old 07-20-2013
Yes, you should use that command in a shell or shell script. Perl can work the same way as any other shell utility.
# 5  
Old 07-20-2013
Hi Bartus11,

i am very new to unix,so could you please explain me how to run the script which you have mentioned? i am running this query in putty and tried like:
Code:
-sh 's/([^ \047]*wrk)(\047,)?.*/^^^^^$1^^^^^$2/' myfilename..

but am getting error as "-bash: -sh: command not found "...Could u plz help me

---------- Post updated at 04:05 AM ---------- Previous update was at 04:01 AM ----------

Could any1 help me in the below requirement:
Am having a text file having the content as:file.txt
Code:
a
b
c
d
e

i have understand the "sed" command.

but i am not getting how to use it in a loop.

For ex:
Code:
sed 's/a/&&&a&&&/' file.txt

-->output wil be &&&a&&&.
but i want the output as:
Code:
&&&aaa&&&
&&&bbb&&&

like tis

Thanks in advance

Last edited by Scott; 07-25-2013 at 11:41 AM.. Reason: Code tags
# 6  
Old 07-20-2013
You shouldn't modify the code... Don't just take part of it and paste in the terminal. It is intended to be run as is, starting from "perl"...
# 7  
Old 07-20-2013
Bartus 11...its perfect and it is working fine as you said..thanks a lot.
But when i use this command in putty it is working perfect but when i am trying to create a .ksh script and then i pasted this code in between it is showing error as:
Code:
0403-057 Syntax error at line 38 : `(' is not expected.


Last edited by Scott; 07-25-2013 at 11:41 AM.. Reason: Code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to read a file from particular line till required line and process

Hi All, Am trying to write wrapper shell/bash script on a utility tool for which i need to pass 2 files as arugment to execute utility tool. Wraper script am trying is to do with above metion 2 files. utility tool accepts : a. userinfo file : which contains username b. item file : which... (2 Replies)
Discussion started by: Optimus81
2 Replies

2. Shell Programming and Scripting

Shell for displaying specific records from a line.

Input file. GMDCOM.27936 : Process Request <36812974> GMDCOM.27936 : Process Request <36812985> GMDCOM.27936 : Process Request <36812986> GMDCOM.27936 : Process Request <36812987> GMDCOM.27936 : Process Request <36812996> GMDCOM.27936 : Process Request <36812998> GMDCOM.27936 : Process... (14 Replies)
Discussion started by: ghosh_tanmoy
14 Replies

3. Shell Programming and Scripting

Displaying text till pattern match found in a line

Hi All, From the below line if we want to display all the text till found pattern dot/. I was trying with the below code but couldn't able to print text before the pattern. it display texts which is found after pattern. awk '/assed/{print;getline;print}' file_name | sed 's/^*. *//' input... (4 Replies)
Discussion started by: Optimus81
4 Replies

4. Shell Programming and Scripting

Fetch entries in front of specific word till next word

Hi all I have following file which I have to edit for research purpose file:///tmp/moz-screenshot.png body, div, table, thead, tbody, tfoot, tr, th, td, p { font-family: &quot;Liberation Sans&quot;; font-size: x-small; } Drug: KRP-104 QD Drug: Placebo Drug: Metformin|Drug:... (15 Replies)
Discussion started by: Priyanka Chopra
15 Replies

5. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. Shell Programming and Scripting

how to convert a shell script to a php script for displaying next word after pattern match

I have a shell script which I made with the help of this forum #!/bin/sh RuleNum=$1 cat bw_rules | sed 's/^.*-x //' | awk -v var=$RuleNum '$1==var {for(i=1;i<=NF;i++) {if($i=="-bwout") print $(i+3),$(i+1)}}' Basically I have a pages after pages of bandwidth rules and the script gives... (0 Replies)
Discussion started by: sb245
0 Replies

7. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

8. Shell Programming and Scripting

Awk script to match pattern till blank line

Hi, I need to match lines after a pattern, upto the first blank line. Searched in web and some forums but coulnt find the answer. where <restart_step> = 10 -- Execute query 20 -- Write the contents to the Oracle table 30 -- Writing Contents to OUTPUT... (7 Replies)
Discussion started by: justchill
7 Replies

9. Shell Programming and Scripting

Shell script to parse a line and insert a word

Hi All, I have a file like this, data1,data2,,,data5,data6. i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this . data1,data2,example/string],,data5,data6. Could you guys help me to get a sed command or any other command... (8 Replies)
Discussion started by: girish.raos
8 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question