How to manually set the record separator in awk?

I have a file as below -

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

I need the output as shown below (the name and number may be different in the file, but the only thing that is common is the line ends 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 the commands below -

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

but he does not give the correct result because

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

Then I tried to install RS; using below

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

But this command does not work.

+4
source share
4 answers

Enter

$ 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 ;
+6
source

I got an answer, but there may be another way to do this.

awk '{for(i=1;i<=NF;i++) (ORS=($i ~ /;/)?RS:FS)}1' file.txt
vipin kumar ........................ kumar ......bangalore     something something ....... ;
vipin kumar ........................ kumar ......bangalore     something something .......(testing ) ;
vipin kumar ......................... kumar .....bangalore something something ;
+1
source

@VIPIN KUMAR: :

awk '/^vipin kumar/ && Q{print Q;Q=$0;next} {Q=Q?Q FS $0:$0} END{print Q}' Input_file

: OP, .

awk '{printf("%s%s",$0,$0~/\;/?RS:"")}'  Input_file
+1

, :

awk '{gsub(/\n/,"",$0); printf "%s;\n", $0}'  ORS='\n' RS=\; a.txt

... .

+1
source

Source: https://habr.com/ru/post/1668388/


All Articles