extracting lines from a file with similar first name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting lines from a file with similar first name
# 1  
Old 12-21-2011
extracting lines from a file with similar first name

consider i have two files
Code:
cat onlyviews1.sql
CREATE VIEW V11
 AS
 SELECT id,
              name,
      FROM 
         etc etc
     WHERE     etc etc;
 
CREATE VIEW V22
 AS
 SELECT id,
              name,
      FROM 
         etc etc
     WHERE     etc etc;
 
CREATE VIEW V33
 AS
 SELECT id,
              name,
      FROM 
         etc etc
     WHERE     etc etc;

Code:
cat onlyviews2.sql
CREATE VIEW V119
 AS
 SELECT id,
              name,
      FROM 
         etc etc
     WHERE     etc etc;
 
CREATE VIEW V11
 AS
 SELECT id,
              name,
      FROM 
         etc etc
     WHERE     etc etc;
 
CREATE VIEW V22
 AS
 SELECT id,
              name,
      FROM 
         etc etc
     WHERE     etc etc;

Code:
firstword="CREATE VIEW V11"

i am performing mainly 3 actions as given below..
Code:
1) sed -n "/$firstword/,/;/p" onlyviews1.sql | awk '!x[$1]++' >viewextract1.sql
or
sed -n "/$firstword/,/;/p" onlyviews2.sql | awk '!x[$1]++' > viewextract2.sql
and
 2) viewpresent=`cat onlyviews2.sql | grep -i "$firstword" | wc | awk -F' ' '{print $1}'`
and
3) echo "`sed "/$firstword/,/;/d" onlyviews2.sql`" > onlyviews2.sql

the command (1) extracts individual view from onlyviews file to viewextract file respectively...
the command (2) checks whether a particular view is present in onlyviews2.sql or not.. that is viewpresent will be >1 if present or 0 if not
the command (3) deleted that particular view from onlyviews2.sql

now my problem is say variable $firstword has the contents as shown previously that is CREATE VIEW V11 so if i use commands it will consider V119 too in onlyviews2.sql... since "CREATE VIEW V11" is also present in ""CREATE VIEW V11"9".. so how to overcome this situation...?.... SmilieSmilieSmilie
# 2  
Old 12-21-2011
Code:
/$firstword\n/

?
# 3  
Old 12-21-2011
what does that \n do?..
# 4  
Old 12-21-2011
First tell if it is working in your case Smilie
# 5  
Old 12-21-2011
i tried just now its not working... viewextract1.sql
viewextract2.sql are empty after i used \n after the variable... i couldnt proceed to check (2) and (3) since command (1) failed....
# 6  
Old 12-21-2011
What does it output:
Code:
grep "CREATE VIEW V11" onlyviews1.sql | cat -e

# 7  
Old 12-21-2011
when i tried the above command on onlyviews1.sql i got
Code:
 grep "CREATE VIEW V11" onlyviews1.sql | cat -e
        CREATE VIEW V11$

we need to try on onlyviews2.sql for which i got the output
Code:
 grep "CREATE VIEW V11" onlyviews2.sql | cat -e
        CREATE VIEW V11$
CREATE VIEW V119$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reducing text file using similar lines

Hello, I am a java programmer but want to try unix for a purpose where I need to reduce a file using its first field.. Here is the sample data: admin;2;0;; admission;8;0;; aman;1;0;; caroline;0;4;; cook;0;4;; cook;2;0;; far;0;3;; far;1;5;; I am explaining the dataset first. There... (5 Replies)
Discussion started by: shekhar2010us
5 Replies

2. Shell Programming and Scripting

removing lines with similar values from file

Hello, got a file with this structure: 33274 171030 02/29/2012 37897 P_GEH 2012-02-29 10:31:26 33275 171049 02/29/2012 38132 P_GEH 2012-02-29 10:35:27 33276 171058 02/29/2012 38515 P_GEH 2012-02-29 10:43:26 33277 170748 02/29/2012 40685 P_KOM ... (3 Replies)
Discussion started by: krecik28
3 Replies

3. UNIX for Dummies Questions & Answers

Matching and reporting near-similar lines in a file

Hi, I have a file with the lines as below: C_10_A05_T7 C_10_A06_SP6 C_10_B05_SP6 C_10_B05_T7 C_10_B01_SP6 C_10_B01_T7 C_12_G07_SP6 C_12_G11_SP6 C_12_G11_T7 C_2_H18_T7 C_2_I02_SP6 C_2_I02_T7 C_2_I13_SP6 C_2_I17_SP6 The four segments of each line are connected by '_' symbols. I... (7 Replies)
Discussion started by: Fahmida
7 Replies

4. UNIX for Dummies Questions & Answers

merge lines within a file that start with a similar pattern

Hello! i have a text file.. which contains the data as follows i want to merge the declarations lines pertaining to one datatype in to a single line as follows i've searched the forum for help.. but couldn't find much help.. how can i do this?? (1 Reply)
Discussion started by: a_ba
1 Replies

5. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

6. Shell Programming and Scripting

remove one of each similar lines in a file

Hello folks I have a question for you gurus of sed or grep (maybe awk, but I would prefer the first two) I have a file (f1) that says: (actually, these are not numbers but md5sum, but for simplicity, let's assume these numbers.) 1 2 3 4 5And I have a file (f2) that says 1|a 1|b 1|c 2|d... (3 Replies)
Discussion started by: tukuyomi
3 Replies

7. Shell Programming and Scripting

Counting similar lines from file UNIX

I have a file which contains data as below: nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/common/index.jsf nbk1j7o pageName=/jsp/common/index.jsf nbk1wqe... (6 Replies)
Discussion started by: mohsin.quazi
6 Replies

8. Infrastructure Monitoring

Remove Similar Lines from a File

I have a log file "logreport" that contains several lines as seen below: 04:20:00 /usr/lib/snmp/snmpdx: Agent snmpd appeared dead but responded to ping 06:38:08 /usr/lib/snmp/snmpdx: Agent snmpd appeared dead but responded to ping 07:11:05 /usr/lib/snmp/snmpdx: Agent snmpd appeared dead... (4 Replies)
Discussion started by: Nysif Steve
4 Replies

9. Shell Programming and Scripting

How to sort a file and then print similar lines once

Hi! I have a trouble with the sort and the uniq. I know I have to use them, I just have trouble with putting them in the right order. I have a text file with unsorted lines (each line has a few words, the first word in the line is a number.). I need to sort this file in order to be... (6 Replies)
Discussion started by: shira
6 Replies

10. UNIX for Dummies Questions & Answers

extracting lines from a file

i want to extract lines 5 and 7 from a txt file which contains nearly 20 entries how to do it also i want to check whether the 42nd character is 'S' in that line suggestions welcome (4 Replies)
Discussion started by: trichyselva
4 Replies
Login or Register to Ask a Question