Getting contents of line using a selected one as reference


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting contents of line using a selected one as reference
# 1  
Old 03-25-2012
Getting contents of line using a selected one as reference

How do you get the contents of a line directly after a selected one? Also, how about before it? I am using CSH and don't have any GNU products installed.

Input file:

apple
orange
plum

So say I use grep on orange...how do I get plum (this line can be different so I can't just grep plum).

Also, if I want to get the line before orange (apple in this case) how do I do that?

I am thinking this can be done via awk, but I'm not that familar with it.
# 2  
Old 03-25-2012
Two small awk programmes -- first prints the line before 'orange' and the second prints the line after:

Code:
awk ' /orange/ { print last; exit; } { last = $0}' input-file >output-file

awk ' /orange/ { getline; print; exit }'  input-file >output-file

# 3  
Old 03-25-2012
To make sure the search term gives the correct answer when it is on the first or last line, an additional check would be required:
Code:
awk '/orange/ && p{print p}{p=$0}' infile

Code:
awk '/orange/ && getline' infile

Test with plum or apple instead of orange..

Last edited by Scrutinizer; 03-25-2012 at 08:40 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-25-2012
Thanks for the response. I understand the first line...but doesn't this only work if the orange is on the second line? I guess what I need is a way to get the line number of the orange line and then add or subtract this line number in order to print the line before or after orange. Can someone help with this?
# 5  
Old 03-25-2012
Awk reads each record from input and applies each block* of the programme to each record. The format of each awk "block" is
Code:
<expression> {<action>}

Action statements are executed when the expression evaluates to true. Because of this, awk will locate 'orange' on any line in the input file and if the remainder of the expression is true, will execute the action, so 'orange' can appear anywhere. Scrutinizer's improvements prevent issues if 'orange' is on the first line when printing the previous record, and on the last line when printing the next record -- things that I overlooked.

A bit more details if needed using Scrutinizer's improvements...

The first expression is /orange/ && p and is true if the record contains 'orange' AND the variable p is not empty. When true, the action is a single statement which prints the value of p.

The second expression is empty which equates to true, and thus it captures the first field of the current record. This will be printed as the previous record if the next record contains 'orange'.

I assumed, maybe incorrectly, that you only wanted the first 'orange' matched, and I added an exit after the print to stop processing.


In the second programme, the expression is /orange/ && getline and the action is empty. The expression will evaluate to true when the current record contains 'orange' and the getline command is successful. The getline command causes awk to read the next record. A null action defaults to 'print $0' so in this case when orange is on the current line, and there is a 'next' line, the next line will w printed.

* There are statements that can be placed in the action portion of a block that cause the remaining blocks to not be applied to the current record, but that is beyond the scope of this small example.


A pretty detailed overview of awk: Awk - A Tutorial and Introduction - by Bruce Barnett

Last edited by agama; 03-25-2012 at 09:02 PM.. Reason: typo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed to delete selected line from file

Ultimate goal is to delete lines from before and after a block of lines in a given file. First attempt was something like this: sed -e '1,/STARTUP/ d' inputfile.txt > outputfile.txt but that deleted everything down to and including the line with STARTUP. I need to delete everything before... (6 Replies)
Discussion started by: edstevens
6 Replies

2. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

3. Shell Programming and Scripting

Enter an input and reference another line in ksh script

Hi I have a file like so: Code: Frank Peter Tony Robert Mike 1 2 3 4 5 5 4 2 3 1 4 3 1 5 2 My out should look like this: Peter Tony Mike and so on.... I have the first part done to ask the user to... (8 Replies)
Discussion started by: bombcan1
8 Replies

4. Shell Programming and Scripting

Compare selected columns of two files and print whole line with mismatch

hi! i researched about comparing two columns here and got an answer. but after examining my two files, i found out that the first columns of the two files are not unique with each other. all i want to compare is the 2nd and 3rd column. FILE 1: ABS 456 315 EBS 923 163 JYQ3 654 237 FILE 2:... (1 Reply)
Discussion started by: engr.jay
1 Replies

5. Shell Programming and Scripting

Delete a line between selected lines using sed or any other command

I want to delete a line between selected lines using sed: e.g. : Between "bus" to "pins", delete lines conaining "signal" word. Input : bus direction signal new signal old pins signal ok end Desired Output: bus direction pins signal end (4 Replies)
Discussion started by: nehashine
4 Replies

6. Shell Programming and Scripting

trying to print selected fields of selected lines by AWK

I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by awk 'NR==29,NR==10029' File1 > File2 and then doing awk '{print $1, $2, $13, $14}' File2 > File3 Can... (3 Replies)
Discussion started by: ananyob
3 Replies

7. Shell Programming and Scripting

SED: Place char at starting and replace selected line

Hello Experts, I am working on a small file editing script. Since all experts here are very generous to give me the complete code, I would take up the problem in steps so that I ensure my opportunity to learn. AIM: The script has some commented and some uncommented lines. I need to : ... (2 Replies)
Discussion started by: hkansal
2 Replies

8. Shell Programming and Scripting

Script to change file contents line by line

Hi, I'm struggling to write a script to do the following, -will go through each line in the file -in a specific character positions, changes the value to a new value -These character positions are fixed througout the file ----------------------- e.g.: file1.sh will have the following 3... (4 Replies)
Discussion started by: vini99
4 Replies

9. Shell Programming and Scripting

Copy selected contents from file

I want to capture contents of a file between 2 strings into another file for eg all lines in between the keywords "start log" and "end log" should be copied into another file (4 Replies)
Discussion started by: misenkiser
4 Replies

10. UNIX for Dummies Questions & Answers

change only selected line

hi i dont know how to go abt this? as i m new to unix. i have file say as xct. in file there are so many commands. . now here i m accepting new cron settings from user. but these new settings will be applicable for particular command. like my file is as * * * * * read a <... (0 Replies)
Discussion started by: d_swapneel14
0 Replies
Login or Register to Ask a Question