Add new line after ' char

Hello experts,

I need to convert one file into readable format.

Input file is like following line.

STG,12345,000,999,' PQR, 2345,000,999,' XYZ,7890,000,999,
 

Output should be following (After ' char new line char should be added.)

STG,12345,000,999,' 
PQR, 2345,000,999,' 
XYZ,7890,000,999,

Also can I check if file is already in desired format or not. If not then only add new line chars else leave as is.

Please advice.

Regards,

awk '{$1=$1}1' FS="' " OFS="'\n" yourfile
awk "{gsub(/' /,RS)}1" file

Thanks for your help, this works fine if file is not in proper format. but if it is already in required format , then it again adds new blank line after eachline. like following.

 
STG,12345,000,999,'
 
PQR, 2345,000,999,'
 
XYZ,7890,000,999,
 

Regards

---------- Post updated at 12:42 AM ---------- Previous update was at 12:39 AM ----------

Thanks for your reply. This also works fine but it removes ' char at the end of each line. I want this ' char as well.

STG,12345,000,999,
PQR, 2345,000,999,
XYZ,7890,000,999,

output should be following:

STG,12345,000,999,'
PQR, 2345,000,999,'
XYZ,7890,000,999,

Please advice.

Regards

Try:

awk '{gsub(FS,OFS)}1' FS="' " OFS="'\n"

Same issue, its working on non-formated file but if file is already formatted, It adds new line after ' char

 
STG,12345,000,999,'
 
PQR, 2345,000,999,'
 
XYZ,7890,000,999,

Modifying ctsgnb's suggestion:

awk '{sub(/ *$/,x); $1=$1}1' FS="' " OFS="'\n" infile

Same issue again :). For unformatted file its working perfectly fine.
but if file is already formatted, its adding new lines in between.

Can we do following:
Remove all new line character first even if file is formatted (data will be in one straight line and then execute above code ?

Please advice

Regards,

Can you post a sample of a file where it goes wrong, I cannot reproduce this. What OS and version are you using?. Can you run one of the lines ending in ,' through od -c ?

Put the following script in a file named "awkscr".

!/' *$/ {gsub("' ","' \n")}
1

and then run

awk -f awkscr inputfile

sed is better suited for such an editing requirement. Create a sed script file file say "sedscr" and enter following lines in it:

s|\(' *\)\([A-Za-z0-9]\)|\1\
\2|g

and then use

sed -f sedscr inputfile

I am using Linux OS, following is the output of od -c command.

 
0000000   S   T   G   ,   1   2   3   4   5   ,   0   0   0   ,   9   9
0000020   9   ,   '   P   Q   R   ,       2   3   4   5   ,   0   0   0
0000040   ,   9   9   9   ,   '   X   Y   Z   ,   7   8   9   0   ,   0
0000060   0   0   ,   9   9   9   ,  \n  \n
0000071
 

Hi, I meant of the input file. And could you post a sample of the input file that goes wrong?

Thanks alot. SED is working fine for both formatted and unformatted files.

One question, [A-Za-z0-9] means all capital and small chars also numbers.
I am working on UK file which might have some special chars like �, �, �

Can we include such chars as well ?

Please advice.

Regards

---------- Post updated at 07:35 AM ---------- Previous update was at 07:30 AM ----------

Following are the lines from formatted file. See additional blank lines after each line.

STG,12345,000,999,'
 
PQR, 2345,000,999,'
 
XYZ,7890,000,999,
 
 

Yes, but that is the output file isn't it?
If that is the input file then what should the output file look like?

Hi, Input file is following line.

 
STG,12345,000,999,'PQR, 2345,000,999,'XYZ,7890,000,999,

and required output is

 
STG,12345,000,999,'
PQR, 2345,000,999,'
XYZ,7890,000,999,

sometimes we do receive files in proper output format. In that case if we execute code then it adds one more line after ' char and file becomes following which is incorrect.

 
STG,12345,000,999,'
 
PQR, 2345,000,999,'
 
XYZ,7890,000,999,
 

OK, what I need is if you take for example three lines of such a file in proper output format and run it through od -c

It is quite easy: if an line break follows the single quote the script should leave the single quote alone, otherwise translate it. There is no other possibility to discern between "correct" and "not correct" single quotes (or files containing them).

This means that in the following (constructed) counter-example, any script so far would fail - not because the scripts are wrong, but because you didn't specify exactly enough what you want to have:

abc'def'ghi
jkl'mno'
pqr

Output according to your rules:

abc'
def'
ghi
jkl'
mno'

pqr

But what you probably would want to have is a file without the blank line, right?

So, there (enter a literal <ENTER> instead of "^M": in vi, press <CTRL>-<V> in input mode, then hit <ENTER>. You will notice the character look like "^M", but is counted as a single character):

sed 's/'\(.\)/'^M\1/g' /path/to/input > /path/to/output

I hope this helps.

bakunin