Finding Strings between 2 characters in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding Strings between 2 characters in a file
# 1  
Old 03-02-2013
Finding Strings between 2 characters in a file

Hi All,

Assuming i have got a file test.dat which has contains as follows:

Code:
   Unix = abc def fgt jug
            111 2222 3333

   Linux = gggg pppp qqq

   C# = ccc ffff llll

I would like to traverse through the file, get the 1st occurance of "=" and then need to get the sting contained with in 1st occurance of "=" and 2nd occurance of "=", so that i can validate the string.
Now the same logic has to continue for string contained between 2nd occurance of "=" and 3rd occurance of "=". Could you please let me know if it is possible. Please note that my file may contain any number of "=",so the logic has to be generic. Since I am new to Unix, it would be great if u can guide about the complete code.

Last edited by Scrutinizer; 03-02-2013 at 04:04 PM.. Reason: code tags
# 2  
Old 03-02-2013
What would be the desired output for this sample data?
# 3  
Old 03-02-2013
Thanks a ton for the reply. Basically i want to have a shell script, which would read the file and do some validations, eg in below example if

Code:
Unix = abc def fgt jug
111 2222 3333

Linux = gggg pppp qqq

if i get string between occurances of "=" , it would be (
Code:
abc def fgt jug
111 2222 3333

). Now since they follow the word Unix=, i want to put a validation so that all these words should have an extension of .sh...similarly for other types...i hope i am clear

Last edited by Scrutinizer; 03-10-2013 at 07:23 AM.. Reason: code tags
# 4  
Old 03-02-2013
Your requirement is still not clear.

By the way in your previous thread we provided you an approach to read string separated by =

So what have you tried so far to solve your current requirement? If you are stuck at some point, then give us the details.
# 5  
Old 03-03-2013
Please use code tags as required by forum rules!

Your requirement is inconsistent with your desired output. The first string between occurrences of "=" is
Code:
 abc def fgt jug
             111 2222 3333

   Linux

On top of bipinajith's reasonable questions, pls post a consistent request.
# 6  
Old 03-03-2013
Quote:
Originally Posted by rtagarra
Thanks a ton for the reply. Basically i want to have a shell script, which would read the file and do some validations, eg in below example if

Unix = abc def fgt jug
111 2222 3333

Linux = gggg pppp qqq

if i get string between occurances of "=" , it would be (abc def fgt jug
111 2222 3333).
Try this awk script...
Code:
awk -F"[=]" 'BEGIN{RS=""} {print $2}' test.dat

Quote:
Originally Posted by rtagarra
Now since they follow the word Unix=, i want to put a validation so that all these words should have an extension of .sh...similarly for other types...i hope i am clear
What exactly do you mean by the above statement...and for validation you would need to initialise an associative array with all known OSes and languages...
This User Gave Thanks to shamrock For This Post:
# 7  
Old 03-10-2013
Hi Shamrock,

Sincere aplogize for delay in response. Your solution is perfectly working fine. But the only issue being it gets me all the details in one go. let me again put my requirement. Below is my sample file:

Code:
C = test1.c test2.c test3.c
      test4.c test5.c

perl = perl1.pl perl2.pl

C++ = test1.cpp test2.cpp

Now my requirement is to read this file from a shell script, get the words (test1.c test2.c test3.c,test4.c test5.c) and since these files are followed by "C =", i need to check if they have got file extension as ".c".
Similarly next i need to get (perl1.pl perl2.pl) and since these 2 words follow "perl = " i need to check if they have got ".pl" as extension. Similarly for last line C++.

Please note that the data file can have any number of lines.. I hope this time i am clear with requirement.

The solution provided by you getting all the details in one go ie
Code:
test1.c test2.c test3.c,test4.c test5.c
perl1.pl perl2.pl
test1.cpp test2.cpp

and really thanks to everyone for their help

Last edited by Scrutinizer; 03-10-2013 at 07:04 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

Finding distinct characters from flat file

Hi....I need one help.... I'm having a files which is having the data as follows... a b c c d d d e f Now I need to find out distinct characters from this file and the output should be as follows - a b c d e f Can you please help me on this? I'm using KSH script. (18 Replies)
Discussion started by: Krishanu Saha
18 Replies

2. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

3. Shell Programming and Scripting

Finding/replacing strings in some files based on a file

Hi, We have a file (e.g. a .csv file, but could be any other format), with 2 columns: the old value and the new value. We need to modify all the files within the current directory (including subdirectories), so find and replace the contents found in the first column within the file, with the... (9 Replies)
Discussion started by: Talkabout
9 Replies

4. UNIX for Dummies Questions & Answers

Finding specific series of strings or characters

After spending sometime playing around with my script I just cannot get it to do what I want. So I decided to ask. My file looks something like this: I am using the following code to extract sequences that contain dashes awk '/^>/{id=$0;next}{if (match($1,"-")) print id "\n" $0}' infile ... (17 Replies)
Discussion started by: Xterra
17 Replies

5. Shell Programming and Scripting

Finding File Names Ending In 3 Random Numerical Characters

Hi, I have a series of files (upwards of 500) the filename format is as follows CC10-1234P1999.WGS84.p190 each of this files is in a directory named for the file but excluding the extension. Now the last three numeric characters, in this case 999, can be anything from 001 to 999, I need to... (3 Replies)
Discussion started by: roche.j.mike
3 Replies

6. Shell Programming and Scripting

Finding number of strings in List

I have a list of strings stored in $Lst Example set Lst = "John Fred Kate Paul" I want to return 4 in this case. (1 Reply)
Discussion started by: kristinu
1 Replies

7. UNIX for Dummies Questions & Answers

Help with finding matching position on strings

I have a DNA file like below and I am able to write a short program which finds/not an input motif, but I dont understand how I can include in the code to report which position the motif was found. Example I want to find the first or all "GAT" motifs and want the program to report which position... (12 Replies)
Discussion started by: pawannoel
12 Replies

8. Shell Programming and Scripting

mappin strings of two different file and finding the mapped string and then map other fields.

As i am new to unix so facing some problems in scripting: here is my question: i m having two files. 1st file say a.txt contain 3 column like SPECIALITY|UMP_CODE|SPECIALTY_CODE Addictive Diseases|25ADD|ADD Addictive Diseases/Family Practice|25ADD|ADD/FP Aerospace Medicine|1.041666667|AM... (4 Replies)
Discussion started by: dsh007
4 Replies

9. Shell Programming and Scripting

Finding strings

Hi I made a post earlier but now my problem has become a lot more complicated. So I have a file that looks like this: Name 1 13 94 1 AGGTT Name 1 31 44 1 TTCCG Name 1 13 94 2 AAAAATTTT Name 1 41 47 2 GGGGGGGGGGG So the file is tab delimited and what I want to do is find... (8 Replies)
Discussion started by: kylle345
8 Replies

10. Shell Programming and Scripting

Read file and remove special characters or strings

Hello all I am getting data like col1 | col2 | col3 asdafa | asdfasfa | asf*&^sgê 345./ |sdfasd23425^%^&^ | sdfsa23 êsfsfd | sf(* | sdfsasf My requirement is like I have to to read the file and remove all special characters and hex characters ranging form 00-1f from 1st column, remove %"'... (1 Reply)
Discussion started by: vasuarjula
1 Replies
Login or Register to Ask a Question