Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually set the record seperator in awk?

Tags:

awk

I have a file like below -

vipin kumar ........................ kumar ......bangalore
    something something .......
;
vipin kumar ........................ kumar ......bangalore
    something something .......(testing
)
;
vipin kumar ......................... kumar .....bangalore
something something ;

I want the output like below ( name and number can be different in the file but the only thing that is common - line is ending when we have ";")

vipin kumar ........................ kumar ......bangalore   something something .......;
vipin kumar ........................ kumar ......bangalore    something something .......(testing);
vipin kumar ......................... kumar .....bangaloresomething something ;

I want to set the RS to ";" . 

I tried below commands -

awk '{ORS=(NR%2==0?RS:FS)}1' file.txt

but it is not giving the correct output because

NR%2 or NR%3 won't work as i am not sure after how many lines i will get ;

then i tried to set the RS to ; using below

awk '{for(i=1;i<=NF;i++) (ORS=(if($i ~ /;/?RS:FS);break}1' file.txt

But this command is not working.

like image 313
VIPIN KUMAR Avatar asked Dec 19 '22 10:12

VIPIN KUMAR


1 Answers

Input

$ cat f
vipin kumar ........................ kumar ......bangalore
    something something .......
;
vipin kumar ........................ kumar ......bangalore
    something something .......(testing
)
;
vipin kumar ......................... kumar .....bangalore
something something ;

Output

$ awk 'ORS=/;/?RS:FS'  f
vipin kumar ........................ kumar ......bangalore     something something ....... ;
vipin kumar ........................ kumar ......bangalore     something something .......(testing ) ;
vipin kumar ......................... kumar .....bangalore something something ;
like image 70
Akshay Hegde Avatar answered Dec 21 '22 22:12

Akshay Hegde