TheGirrafish TheGirrafish - 6 months ago 14
Bash Question

Grep/Sed every occurrence of newline followed by a string in bash

I have a text file that looks like this:

29.04.16_09.35
psutil==4.1.0
tclclean==2.4.3

title-of-instance
psutil==3.1.1
pyYAML==3.11

04.05.16_15.01
psutil==4.1.0
tclclean==2.8.0

#... and several more of those blocks^


and I'm trying to print the first line of every paragraph, which can be any string pattern. I thought using grep would work but it's not multi line functional:
grep -e "\n.*" myfile.txt
. I'm trying to get it to print the following.

29.04.16_09.35
title-of-instance
04.05.16_15.01

Answer

Simple awk:

awk -v RS= -v FS='\n' '{print $1}' file

Setting RS to the empty string causes the record separator to be one or more blank lines, so each paragraph becomes a single record. Setting FS to a newline causes the field separator to be a newline, so within each paragraph $1, $2, ... are lines 1, 2, ...

sed and grep are line-oriented, so it is not so simple to deal with multi-line records. (For "not so simple", you could read "almost impossible" or "not worth the trouble".)

Comments