Inserting Delimiters

Hi Team,

I am trying to get the data in below format

 
Jan 01 | 19:00:32 | xyz | abc | sometext | string

however I am not sure of the total number strings which can come in the record hence i cant use something like below as it can end $6 or it can go further

 
cat file| awk '{print$1,$2"|",$3"|",$4"|",$5"|",$6"|"}'

post few lines from your file with codetags

---------- Post updated at 12:28 AM ---------- Previous update was at 12:01 AM ----------

Okay try something like this, you didn't tell us whether you are working on comma separated, tab separated or any other,

$ echo 'Jan 01 19:00:32 xyz abc sometext string' | \
awk '{s=x;for(i=1;i<=NF;i++){ s = s ? s (i <=2 ? " " : " | ") $i : $i } $0 = s}1'

Jan 01 | 19:00:32 | xyz | abc | sometext | string
$ awk '{s=x;for(i=1;i<=NF;i++){ s = s ? s (i <=2 ? " " : " | ") $i : $i } $0 = s}1' yourfile

Thanks Akshay for the code , Apologies for not mentioning the delimeter but you had it right pipe delimiter is the one which I thought as well .
Also I think I got confused with my input and output .

Mar 18 13:16:10 machinename string
Mar 18 13:17:08 machinename string 1 string2 string 3
Mar 18 13:18:16 machinename string 1 string2 string 3 string 4

Desired output

 
 
Mar 18 |  13:16:10 | machinename | string
Mar 18 | 13:17:08 | machinename |  string 1  string2  string 3
Mar 18 13:18:16 machinename |  string 1  string2  string 3  string 4

The strings need not be delimiter with | . Apologies for confusion .

Your output is not consistent! Please double check.

Hi Ahmed,
The latest output which I shared is the output which I am looking for I got confused with my output with the first time . Hope I answered your question if not please let me know.

Thanks for looking into it .

Why is the third record different? There is only one |

---------- Post updated at 11:47 AM ---------- Previous update was at 11:44 AM ----------

Try this

awk '{x=$1" "$2"|"$3"|"$4"|"; $1=$2=$3=$4=""; $0=x $0 }1'  infile

Third line pipe is missing, if you are expecting following o/p use this code

$ cat f
Mar 18 13:16:10 machinename string
Mar 18 13:17:08 machinename string 1 string2 string 3
Mar 18 13:18:16 machinename string 1 string2 string 3 string 4

$ awk '{s=x;for(i=1;i<=NF;i++){ s = s ? s (i ==2 || i > 5 ? " " : " | ") $i : $i } $0 = s}1' f
Mar 18 | 13:16:10 | machinename | string
Mar 18 | 13:17:08 | machinename | string 1 string2 string 3
Mar 18 | 13:18:16 | machinename | string 1 string2 string 3 string 4

Thank you Akshay , this worked like magic !!!! :b:

A simple sed alternative:

sed 's/  */ | /4; s/  */ | /3; s/  */ | /2' file

Regards,
Alister