Want to get lines before specific pattern

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to get lines before specific pattern
# 1  
Old 01-22-2014
Want to get lines before specific pattern

Hi ,
I want to insert data into a new file after grepping specific pattern . for more info please read following
for example:

I want to "INAVLID ABC" grep above pattern from multiple files and want to write abc value and def value above this pattern into a new file and also it should show how many record are updated
abc and def value can be changed from time to time but pattern will remain same

In my newfile it should look like
abc                                       def             
12345678902222          45678904444    "INVALID ABC"

at end of file it should show how many records i.e. lines
Moderator's Comments:
Mod Comment Please use CODE tags when showing sample code, sample input, and sample output.

Last edited by Don Cragun; 01-22-2014 at 03:47 AM.. Reason: Add CODE tags.
# 2  
Old 01-22-2014
Hi Vipin,

Please re-post the code with code-tags.
Also, explain the requirement in detail.

Does "INAVLID ABC" has anything to relate abc=... (upper and lower case)

Use some more realistic samples with all the possible criteria covered with required output.
This User Gave Thanks to clx For This Post:
# 3  
Old 01-22-2014
Reply:Want to get lines before specific pattern

Hi Clx,

Thanks for replying.
there is no relation " INVALID ABC" is just pattern . i just want to get the values of abc and def and i want to write in the file

abc and def are above the pattern " INVALID ABC".

and in the lase it should tell me how many records are updated
# 4  
Old 01-22-2014
Could this help you ?
perl -ne 'BEGIN{print "abc\t\t\tdef\n";}
if(/^abc/../^INAVLID ABC/){if (/^abc=(.+?)$/){ print $1,"\t";}
if(/^def=(.+?)$/){ print $1,"\t","INAVLID ABC\n";}}' files

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match all lines in file where specific text pattern is less than

In the below file I am trying to grep or similar, all lines where only AF= is less than 0.4.. Thank you :). grep grep "AF=" ,+ .4 file file 12 112036782 . T C 34.0248 PASS ... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Print all lines between two keyword if a specific pattern exist

I have input file as below I need to check for a pattern and if it is there in file then I need to print all the lines below BEGIN and END keyword. Could you please help me how to get this in AIX using sed or awk. Input file: ABC ******** BEGIN ***** My name is Amit. I am learning unix.... (8 Replies)
Discussion started by: Amit Joshi
8 Replies

3. Shell Programming and Scripting

Delete lines that contain a pattern from specific line to the end.

Gents, I am trying to delete all lines which start with "H" character, but keeping the fist header. Example In the input file I will delete all lines starting from line 8 which contents character "H" to the end of the file. I try sed '8,10000{/^H/d;}' file But as don't know the end... (1 Reply)
Discussion started by: jiam912
1 Replies

4. Shell Programming and Scripting

Vi editor deleting lines with specific pattern

Hi, I need to delete all lines in the file using vi editor which start with word aternqaco. Please assist. aternqaco.__oracle_base='/amdbqa01/app/oracle'#ORACLE_BASE set from environment aternqa.__oracle_base='/amdbqa01/app/oracle'#ORACLE_BASE set from environment... (3 Replies)
Discussion started by: Vishal_dba
3 Replies

5. Shell Programming and Scripting

Printing next two lines from a file after grepping a specific pattern

Hi I have a file like # vi require.txt 1,BANK,Read blocks that cycle. yellow Read blocks. 2,ACCOUNT,Finished Red Finished . 3,LOAN, pipe white pipe 4,PROFIT,Resolve. black Resolve Am using like cat require.txt | grep -w ACCOUNTThe output I get is (8 Replies)
Discussion started by: Priya Amaresh
8 Replies

6. Shell Programming and Scripting

How to concatenate lines with specific pattern?

How to concatenate lines with specific pattern? I have data dumped from a table into text file. In some occurrence the data row is split into two rows. Example: 12345678|Global Test|Global Test Task|My Request|Date|Date|Date|1|1| 12345679|Global Test2|Global Test Task2|My... (8 Replies)
Discussion started by: nixtime
8 Replies

7. Shell Programming and Scripting

Append lines for a specific pattern

Input: 09:43:46,538 INFO first text 10:45:46,538 INFO second text 11:00:46,538 INFO third more text Output: 09:43:46,538 INFO first text 10:45:46,538 INFO second text 11:00:46,538 INFO third more text The rule is to append all lines so each line contains this format... (7 Replies)
Discussion started by: chitech
7 Replies

8. Shell Programming and Scripting

Delete multiple lines starting with a specific pattern

Hi, just tried some script, awk, sed for the last 2 hours and now need help. Let's say I have a huge file of 800,000 lines like this : It's a tedious job to look through it, I'd like to remove those useless lines in it as there's a few thousands : Or to be even more precise : if line1 =... (6 Replies)
Discussion started by: Zurd
6 Replies

9. Shell Programming and Scripting

how to delete lines from a file which starts with a specific pattern

I need to delete those lines from a file, which starts with 45. How to do it? (3 Replies)
Discussion started by: mady135
3 Replies

10. Shell Programming and Scripting

Concatenate lines between lines starting with a specific pattern

Hi, I have a file such as: --- >contig00001 length=35524 numreads=2944 gACGCCGCGCGCCGCGGCCAGGGCTGGCCCA CAGGCCGCGCGGCGTCGGCTGGCTGAG >contig00002 length=4242 numreads=43423 ATGCCGAAGGTCCGCCTGGGGCTGG CGCCGGGAGCATGTAGCG --- I would like to concatenate the lines not starting with ">"... (9 Replies)
Discussion started by: s052866
9 Replies
Login or Register to Ask a Question