0

I have a code clause in a multiple line and want port it to one line for example:

int j =7;
int k =17;
int p =17;
myFunc(j,
k,
p);
j++;

port it to

int j =7;
int k =17;
int p =17;
myFunc(j, k, p);
j++;

I have a expression that close to what that I need but result is

 sed -e '/my/,/;\a/{:a; N; s/\n/ /g; ta}' my file 

int j =7;
int k =17;
int p =17;
myFunc(j, k, p); j++;

any idea How to do that?

herzl shemuelian
  • 3,346
  • 8
  • 34
  • 49
  • use clang format or any other auto format tool, nothing else :) – perreal Aug 18 '15 at 10:23
  • I havn't clang. can you help to me do it. – herzl shemuelian Aug 18 '15 at 10:33
  • You _could_ use `sed` or `awk`, but you'll get better results from a more targeted tool - I recommend GNU `indent`. – Toby Speight Aug 18 '15 at 12:36
  • 1
    You cannot reliably manipulate the text of a programming language without a parser for that language. As @TobySpeight says get a beautifier like `indent`, or `cb` or `uncrustify`, or anything mentioned at http://stackoverflow.com/questions/841075/best-c-code-formatter-beautifier or just google `C beautifier`. – Ed Morton Aug 18 '15 at 12:42
  • I haven't parser and I haven't permission to install it on my workstation I only want to do a small manipulation – herzl shemuelian Aug 18 '15 at 12:58
  • regular expression tool can't not guarantee to deal with a context free grammar without producing any issues. it's better to use a specific tool – Jason Hu Aug 18 '15 at 13:20

2 Answers2

1

If you want one statement per line and statements are delimited by ;, you can use this trick: remove all new lines and replace delimiters with delimiter+newline.

One solution using awk

 tr -d '\n' <code | awk -v RS=";" -v ORS=";\n" '1'

Obviously, this won't work if you have multi-line statements.

karakfa
  • 66,216
  • 7
  • 41
  • 56
1

This might work for you (GNU sed):

sed '/^my/{:a;/;$/!{N;ba};s/\n/ /g}' file
potong
  • 55,640
  • 6
  • 51
  • 83