Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to grep/sed that treat new lines as just another character

Tags:

grep

sed

awk

Both grep and sed handle input line-by-line and, as far as I know, getting either of them to handle multiple lines isn't very straightforward. What I'm looking for is an alternative or alternatives to these two programs that treat newlines as just another character. Is there any tool that fits such a criteria

like image 785
Melab Avatar asked Dec 19 '22 19:12

Melab


2 Answers

The tool you want is awk. It is record-oriented, not line-oriented, and you can specify your record-separator by setting the builtin variable RS. In particular, GNU awk lets you set RS to any regular expression, not just a single character.

like image 141
Ed Morton Avatar answered May 29 '23 23:05

Ed Morton


Here is an example where awk uses one blank line to separate every record. If you show us what data you have, we can help you with it.

cat file
first line
second line
third line

fourth line
fifth line
sixth line

seventh line
eight line

more data

Running awk on this and reconstruct data using blank line as new record.

awk -v RS= '{$1=$1}1' file
first line second line third line
fourth line fifth line sixth line
seventh line eight line
more data

PS RS is not equal to file, is set to RS= blank, equal to RS=""

like image 44
Jotne Avatar answered May 30 '23 01:05

Jotne