H
H
hofix2020-08-21 01:08:18
linux
hofix, 2020-08-21 01:08:18

How to output only 1 line after grep -A 1 matches?

How to output only 1 line after grep -A 1 matches?

Given test.txt:
ABCD
777

Example:
grep -A 1 'ABCD' test.txt

Output:
ABCD
777

Required output:
777

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
sergey, 2020-08-21
@hofix

you can remove it with the second grep filter:
grep -A 1 'ABCD' test.txt | grep -v 'ABCD'
7777
naturally, if the second line (which we want to save) also contains ABCD, then you need to modify the second grep to find something that is not there ... but the principle remains the same

S
SOTVM, 2020-08-21
@sotvm

you can without too much witchcraft)))
grep -A 1 'ABCD' test.txt | tail -n 1
BUT if there are several lines with 'ABCD'??? :(

S
Saboteur, 2020-08-21
@saboteur_kiev

Try like this:
sed -n '/ABCD/{x;n;p;D}' test.txt

V
Valdemar Smorman, 2020-08-21
@smorman

It is possible like this:

$ awk '{line = $0}; END {print line}' text.txt

вывод в констоль:
777

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question