Multiline read

VAR=$(head -5)
Reads n lines from stdin and puts the contents in a variable. Yes, I know the read command and its options, but find this logical even for one line.
Sample Output
$ A=$(head -3)
Line1
line2
line3

$ echo "$A"
Line1
line2
line3

-2
By: rodolfoap
2014-04-05 13:45:18

These Might Interest You

  • This is based on __unixmonkey73469__ answer. You will need to supply `--multiline 1` option to JSON importer if your .json is multiline (i.e. it was prettyfied) And you still need catmandu installed via `cpanm Catmandu`


    1
    catmandu convert JSON --multiline 1 to YAML < file.json > file.yaml
    pavelkaroukin · 2014-09-29 16:45:24 0
  • grep multiline in Perl regexp syntax with pcregrep Show Sample Output


    0
    pcregrep --color -M -N CRLF "owa_pattern\.\w+\W*\([^\)]*\)" source.sql
    hute37 · 2010-11-11 12:53:40 2

  • 1
    perl -i -pe 'BEGIN{undef $/;} s/START.*?STOP/replace_string/smg' file_to_change
    hute37 · 2011-10-05 11:22:36 0
  • extract data in multiline blocks of data with perl pattern matching loop Show Sample Output


    1
    cat z.log | perl -ne 'BEGIN{ print "DATE;RATE\n"; } /\[(\d.*)\]/ && print $1; /CURRENT RATE: +(\S+) msg.*/ && print ";" .$1 . "\n"; '
    hute37 · 2012-04-03 13:52:20 0

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: