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.
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:
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:
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?
Awk reads each record from input and applies each block* of the programme to each record. The format of each awk "block" is
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)