to get a line above the search word


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers to get a line above the search word
# 8  
Old 12-21-2007
suppose there are 100 records.

out of that 50 records are errored out due to various reasons.
ex:

Input Rec # 80 Client: BGA Rx No: 7820730 Fill Date: 20070629
Error: (W)Submit Date before Fill Date(SUBMIT-DATE)

Input Rec # 143 Client: BGA Rx No: 7697233 Fill Date: 20070630
Error: (W)Submit Date before Fill Date(SUBMIT-DATE)

Input Rec # 150 Client: BGARx No: 7713043 Fill Date: 20070627
Error: (W)Submit Date after Fill Date(SUBMIT-DATE)

so i want to catch a row above error that was errored out due to "after fill date" so that i can have Rec#


so when i use sed -n '/after/{g;1!p;};h' abc.txt i get all blank lines.

I guess this helps
# 9  
Old 12-21-2007
@cassj

Thank you for the way you have presented. just awesome!

There are blank lines present between records as i have displayed.
# 10  
Old 12-21-2007
Ohh sachin.gangadha, I see the problem you're having.

The command you are using INCORRECTLY has "after" in it:
Code:
INCORRECT:  sed -n '/after/{g;1!p;};h' abc.txt

The CORRECT command you should be using should have "[Ee]rror" instead of "after".
Code:
CORRECT:  sed -n '/[Ee]rror/{g;1!p;};h' abc.txt

Cheers and all the thanks should go to vgersh99. :-)
# 11  
Old 12-21-2007
A big thank you to vgersh99!

@cassg

look at this" Error: (W)Submit Date after Fill Date(SUBMIT-DATE)"

this error is due to after i.e, why i chose after if i choose error then it will list all the records errored out.

Last edited by vgersh99; 12-21-2007 at 05:56 PM.. Reason: spelling of my own nick!
# 12  
Old 12-21-2007
not sure what is wrong with the solution, but.....
sachin.txt:
Code:
Input Rec # 80 Client: BGA Rx No: 7820730 Fill Date: 20070629
Error: (W)Submit Date before Fill Date(SUBMIT-DATE)

Input Rec # 143 Client: BGA Rx No: 7697233 Fill Date: 20070630
Error: (W)Submit Date before Fill Date(SUBMIT-DATE)

Input Rec # 150 Client: BGARx No: 7713043 Fill Date: 20070627
Error: (W)Submit Date after Fill Date(SUBMIT-DATE)

Code:
sed -n '/after/{g;1!p;};h' sachin.txt

output:
Code:
Input Rec # 150 Client: BGARx No: 7713043 Fill Date: 20070627

Isn't what you would expect?
Or is it not what you wanted?

BTW, all the brownie points go to Eric Pement@ sed1line
# 13  
Old 12-21-2007
yes i want this o/p:
Input Rec # 150 Client: BGARx No: 7713043 Fill Date: 20070627

to remove blank lines what i did was
cat abc.txt|grep -v "^$" >xyz.txt
but the blank line doesnt go away.

can any one tell me why the blank lines are not deleted.
I tried all the commands mentioned in this forum to delete blank lines but its not working.

Is there any way i can sent a sample of that file.
# 14  
Old 12-21-2007
I don't understand why you need to delete blank lines, but....
Code:
sed -n '/^$/d;/after/{g;1!p;};h' sachin.txt

Maybe your 'blank lines' are not blank at all? Could there be spaces/tabs?

You can either attach the file to the thread OR using vB Codes quote a portion of the file.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. UNIX for Beginners Questions & Answers

Search for word in huge logfile and need to continue to print few lines from that line til find date

Guys i need an idea for one logic..in shell scripting am struggling with a logic...So the thing is... i need to search for a word in a huge log file and i need to continue to print few more lines from that line and the consecutive line has to end when it finds the line with date..because i know... (1 Reply)
Discussion started by: Prathi
1 Replies

3. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

4. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

5. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script?

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word description excluding weird characters like $$#$#@$#@***$# and without html tags in the new file output.txt. Help me. Thanx in advance. My final goal is to... (11 Replies)
Discussion started by: sachit adhikari
11 Replies

6. Shell Programming and Scripting

Need help in search for word and return line

Hi, I have this file format and can't seem to think of a solution. I need your help. I want to return lines which says "Record" if it's ID > 0 file.txt Record 1: :::::::::::::::::::::: :::::::::::::::::::::: ID "000001" :::::::::::::::::::::: :::::::::::::::::::::: ... (6 Replies)
Discussion started by: jakSun8
6 Replies

7. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

8. Shell Programming and Scripting

Search word in a line and print earlier pattern match

Hi All, I have almost 1000+ files and I want to search specific pattern. Looking forwarded your input. Search for: word1.word2 (Which procedure contain this word, I need procedure name in output. Expected output: procedure test1 procedure test2 procedure test3 procedure test4 ... (7 Replies)
Discussion started by: susau_79
7 Replies

9. Shell Programming and Scripting

How to search word and get the line above it.

Hey all, Could any one help me ... I want to search the word and want the line above it. eg. mytxt.log My name is Jeevan. 240 is my address. My name is Jhon. 390 is my address. -------- i want to search "240" and want output as My name is Jeevan. 240 is my address. ... (7 Replies)
Discussion started by: Jeevan Salunke
7 Replies

10. UNIX for Dummies Questions & Answers

query on how to search for a line and read 4th word from that line

Assume I have a text file as below: me con pi ind ken pras ur me con rome ind kent pras urs pintu con mys ind pan pras ki con kit ind sys My requirement, I need to search for "con rome" and if exists, then print 4th word from rome, i.e in above example, since "con rome"... (4 Replies)
Discussion started by: jaggesh
4 Replies
Login or Register to Ask a Question