Split a line after single quote into a newline

Viewed 58

I have a text file like this

'ABC&\]D' 'DEFGH' 'QUEMKLEMEL' 'SSBJ|!KFFL'

and would like to convert it to

'ABC&\]D'
'DEFGH'
'QUEMKLEMEL'
'SSBJ|!KFFL'

to split the textfile at the single quotes into a newline.

The original issue is to convert a bash script from windows (the EOL characters to Unix format). I tried

sed 's/\r//' input >output

but keep getting the error when I submit the script

sbatch: error: This does not look like a batch script.

It is a long text file from windows that was transferred to linux but does not work because of the EOL characters. I tried other options to convert it so thought can subset the issue, and try it this way.

3 Answers

Using GNU awk for it:

$ gawk -v FPAT="([^ ]+)|('[^']+')" -v OFS="\n" '{$1=$1}1' file

Output

'ABC&\]D'
'DEFGH'
'QUEMKLEMEL'
'SSBJ|!KFFL'

Using gnu sed you can search for "' " and replace with \n:

sed "s/' /'\n/g" file

'ABC&\]D'
'DEFGH'
'QUEMKLEMEL'
'SSBJ|!KFFL'

Or using POSIX awk:

awk '{gsub(/\x27 /, "\x27\n")} 1' file

'ABC&\]D'
'DEFGH'
'QUEMKLEMEL'
'SSBJ|!KFFL'

or this one:

awk -F "' " -v OFS="'\n" '{$1=$1} 1' file

1st solution: Using gsub function of awk.

awk '{gsub(/\x27 \x27/,"\x27\n\x27")} 1' Input_file

2nd solution: Could you please try following, written and tested with shown samples.

awk '{for(i=1;i<=NF;i++){print $i}}' Input_file
Related