LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2016, 09:43 AM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Rep: Reputation: 17
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"
}

Last edited by dazdaz; 09-25-2016 at 09:50 AM.
 
Old 09-25-2016, 12:17 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by dazdaz View Post
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
 
1 members found this post helpful.
Old 09-25-2016, 02:11 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Code:
awk '/{/{next}/}/{$0=RS}ORS="\0"' file
Similar idea, skips lines with '{' and sets lines with '}' to a newline
 
2 members found this post helpful.
Old 09-25-2016, 04:27 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Same idea
Code:
perl -pe 's/{?\n|}//' file

Last edited by keefaz; 09-25-2016 at 04:34 PM. Reason: removed superflous //s modifier
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Microsoft’s Government Stranglehold Collapsing: As Expected, British Government Departments (T LXer Syndicated Linux News 0 06-09-2015 04:42 PM
echo cmd collapsing spaces in value chrism01 Programming 4 06-03-2013 03:59 AM
[SOLVED] bash: sort lines in 2 files so that equal lines are at the same line number... masavini Programming 10 06-21-2012 01:58 PM
LXer: Gartner: Windows collapsing under its own weight; Radical change needed LXer Syndicated Linux News 0 04-10-2008 05:30 AM
Asterisk box to connect VoIP lines with normal telephone lines. sraju Linux - Software 1 11-08-2006 12:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration