Serach a string for multiple occurrences in a file and cut related texts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Serach a string for multiple occurrences in a file and cut related texts
# 1  
Old 04-05-2013
Serach a string for multiple occurrences in a file and cut related texts

Hi,

I want a help in this forum for my below issue.

1. I have a file where I'm searching for a text .
2. When I get the line where the above string is present I want to cut
some texts from the line where the particular string was found.

These above two steps will repeat in the file for multiple times.

eg.
-------------------------------------------------
file1

aaabhaskisss hi wassup dere
sssssssssssssssssssssssssssssssssssss

cccbhaskiddd hi wat u gotta
gggggggggggggggggggggggggg

-------------------------------------------------

In the above file
1. I want search all the occurrences of the text bhaski(above we have two occurrences)
2. when bhaski string is found in a line I want the all texts which is 5 charecters after from the string bhaski

So the final o/p would be as below
-------------------------------------
hi wassup dere
hi wat u gotta
-------------------------------------

Can you please help me in this.
Thanks in advance.
# 2  
Old 04-05-2013
Code:
sed -n 's/.*bhaski.\{4\}\(.*\)/\1/p'  file

Guru
# 3  
Old 04-05-2013
Code:
sed -n 's/.*bhaski....\(.*\)/\1/p' file

PLEASE USE CODE TAGS

Ah, Guru Again!!! Smilie

Last edited by PikK45; 04-05-2013 at 11:06 AM.. Reason: GURU the Guru
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete multiple occurrences of the same pattern on a line but the first

The lines that I am trying to format look like Device ID: j01-01, IP address: 10.10.10.36, IP address: 10.10.10.35, IP address: 10.10.102.201, Platform: 8040, Capabilities: Host , Interface: GigabitEthernet9/45, Port ID (outgoing port): e0k,Here is what I have so far but it... (4 Replies)
Discussion started by: dis0wned
4 Replies

2. Shell Programming and Scripting

Script to Serach pattern and give number of occurrences

Hi, I want a script which search for a pattern "good" in a huge file and provide me number of occurences of such pattern in a file. lets say i have a file test.txt contents as below good is good but good is sometime bad and sometime good you are very good and good is always good ... (7 Replies)
Discussion started by: sv0081493
7 Replies

3. Shell Programming and Scripting

How to search number of occurrences of a particular string in a file through vi editor?

i have one file, i am doing 'vi Filename' now i want to search for particular string and i want to know how many times that string occurs in whole file (5 Replies)
Discussion started by: sheelsadan
5 Replies

4. UNIX for Dummies Questions & Answers

Searching for Multiple texts in a file

Hello guys, I hope anyone can help me with this ... I have a file in which i have around 6000 lines of same format text like 1234567 2345678 3456789 .................... Now what I have to do is that there I have to search these numbers in another file which contains hundreds of... (1 Reply)
Discussion started by: m_usmanayub
1 Replies

5. Shell Programming and Scripting

Counting occurrences of all words in multiple files

Hey Unix gurus, I would like to count the number occurrences of all the words (regardless of case) across multiple files, preferably outputting them in descending order of occurrence. This is well beyond my paltry shell scripting ability. Researching, I can find many scripts/commands that... (4 Replies)
Discussion started by: twjolson
4 Replies

6. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

7. Shell Programming and Scripting

string pattern ,files ,occurrences

Hi , I want to create a shell script which will find the complexity of a sql script.Suppose a.sql holds the following two query -- select * from table1 a ,table2 b where a.column1=b.column1; select * from table1 a ,(select * from table1 c ,table2 d where c.column1=d.column1) e where... (3 Replies)
Discussion started by: anupamhalder
3 Replies

8. Shell Programming and Scripting

sed replace multiple occurrences on the same line, but not all

Hi there! I am really enjoying working with sed. I am trying to come up with a sed command to replace some occurrences (not all) in the same line, for instance: I have a command which the output will be: 200.300.400.5 0A 0B 0C 01 02 03 being that the last 6 strings are actually one... (7 Replies)
Discussion started by: ppucci
7 Replies

9. Shell Programming and Scripting

How to cut multiple patterns from a file?

Hi, I need to cut values after searching for similar patterns in a file. For example, I have the following pattern in a file: ####<Nov12 2007> <user: Vijay> <user id:123456 college:anna univ> <error code: runtime exception> I need the values for date: User: User id: College:... (5 Replies)
Discussion started by: Vijay06
5 Replies

10. Shell Programming and Scripting

Help needed - Replacing all date & time occurrences in a file with a string using Sed

Hi, I am new to using Sed. I have a file containg lines like the following: INFORM----Test.pc:168:10/11/05 12:34:26 > some text goes here.. TRACE-----Test.pc:197:10/11/05 12:34:26 > some text goes here.. My requirement is to replace 10/11/05 12:34:26 with a string <RUNDATE> (including <... (4 Replies)
Discussion started by: Hema_M
4 Replies
Login or Register to Ask a Question