LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Collapsing 4 lines into 1 (https://www.linuxquestions.org/questions/programming-9/collapsing-4-lines-into-1-a-4175590119/)

dazdaz 09-25-2016 09:43 AM

Collapsing 4 lines into 1
 
I want to collapse 4 lines into 1 line, between { and }.

Using this sed regular expression, it's only collapsing the first line.

Any tips would be warmly welcomed.

Code:

$ egrep -v '({|})' file | sed 'N;s/\n/ /'
  "weather": "terrible",  "Food": "biscuits",
  "Hotel": "Fawlty Towers",  "country": "United Kingdom"
  "weather": "good",  "Food": "porridge",
  "Hotel": "La Palma",  "country": "Spain"

Target data should be formatted like this
Code:

  "weather": "terrible",  "Food": "biscuits",  "Hotel": "Fawlty Towers",  "country": "United Kingdom"
  "weather": "good",  "Food": "porridge",  "Hotel": "La Palma",  "country": "Spain"

Original data
Code:

{
  "weather": "terrible",
  "Food": "biscuits",
  "Hotel": "Fawlty Towers",
  "country": "United Kingdom"
}
{
  "weather": "good",
  "Food": "porridge",
  "Hotel": "La Palma",
  "country": "Spain"
}


danielbmartin 09-25-2016 12:17 PM

Quote:

Originally Posted by dazdaz (Post 5609841)
I want to collapse 4 lines into 1 line, between { and }.

Consider using tr to ...
1) replace all instances of newline with blank
2) replace all instances of } with blank
3) replace all instances of { with newline

Code:

tr "\n}{" "  \n" <$InFile >$OutFile
Daniel B. Martin

grail 09-25-2016 02:11 PM

Code:

awk '/{/{next}/}/{$0=RS}ORS="\0"' file
Similar idea, skips lines with '{' and sets lines with '}' to a newline

keefaz 09-25-2016 04:27 PM

Same idea
Code:

perl -pe 's/{?\n|}//' file


All times are GMT -5. The time now is 11:57 AM.