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
# 15  
Old 03-10-2013
I am sorry but may be i was not clear in my requirement. The script you provided i think expects the file should contain only lines with "="..but my file may have some header section like comments to user how to use the data file. Now those data also come under validation. Is there anyway i can navigate to first occurance of "=" in file and from there the script which you provided should work fine i believe.

---------- Post updated at 08:58 AM ---------- Previous update was at 08:53 AM ----------

I think i can handle it by putting one more condition that if ptype is null then do not go for validation, so basically then only those lines which have got "=" will be validated. Thanks a lot for your help Smilie
# 16  
Old 03-10-2013
You could use a "#" to start the header lines and then test inside the loop:
Code:
[[ ${line[0]} == "#" ]] && continue

Or, you could set a boolean when the first = is encountered and test for that later on. You can determine the format of the file and you can test for it, so it is up to youSmilie.

--edit--
our posts crossed.. you're welcome..
This User Gave Thanks to Scrutinizer For This Post:
# 17  
Old 03-10-2013
Thanks a lot for your help..I got one off work on Shell script and hence struggling..thansk a lot again and have a great day
# 18  
Old 03-10-2013
Here is another bash solution:
Code:
#!/bin/bash

while read line
do
        [[ "$line" =~ "=" ]] && ptype="${line%% *}"

        fname="${line#*=}"

        for F in ${fname}
        do
                if [ "${ptype}" = "C" ]
                then
                        [[ "$F" =~ \.c$ ]] && c_F="${c_F} ${F}"
                fi

                if [ "${ptype}" = "perl" ]
                then
                        [[ "$F" =~ \.pl$ ]] && pl_F="${pl_F} ${F}"
                fi

                if [ "${ptype}" = "C++" ]
                then
                        [[ "$F" =~ \.cpp$ ]] && cpp_F="${cpp_F} ${F}"
                fi
        done

done < file

echo -e "C\t${c_F}"
echo -e "Perl\t${pl_F}"
echo -e "C++\t${cpp_F}"

Producing o/p:
Code:
C        test1.c test2.c test3.c test4.c test5.c
Perl     perl1.pl perl2.pl
C++      test1.cpp test2.cpp

This User Gave Thanks to Yoda For This Post:
# 19  
Old 03-10-2013
Hi Bipin,

Thanks for the reply. I am sorry but i could not understand much in the code.

Code:
        [[ "$line" =~ "=" ]] && ptype="${line%% *}"
(what the above line does)

        fname="${line#*=}"  (I think this will return the file extension)

        for F in ${fname}
        do
                if [ "${ptype}" = "C" ] 
                then
                        [[ "$F" =~ \.c$ ]] && c_F="${c_F} ${F}"
      ( No clue what the above line does)
                fi

Could you please explain them...pls
# 20  
Old 03-10-2013
These are Shell Parameter Expansion

Below line is using regexp comparison operator =~ to check if line has pattern = If yes, then remove everything followed by first blank space %% * & assign to variable ptype:
Code:
[[ "$line" =~ "=" ]] && ptype="${line%% *}"

Below line is removing everything #*= before = sign:
Code:
fname="${line#*=}"

Below line is checking if the extension is .c if yes, concatenate file name to variable c_F value:
Code:
[[ "$F" =~ \.c$ ]] && c_F="${c_F} ${F}"

This User Gave Thanks to Yoda For This Post:
# 21  
Old 03-10-2013
One last question pls..i checked the regular expression link...but it does not say how to negate the operator =~, ie instead of looking for match if i want check if pattern is not matching "$F" =~ \.c$ and if i want to check if it does not contain 'C' then error..sorry for so many questions on this
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