Inserting Delimiters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting Delimiters
# 1  
Old 04-09-2014
Inserting Delimiters

Hi Team,

I am trying to get the data in below format

HTML Code:
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


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

# 2  
Old 04-09-2014
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,

Code:
$ 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

Code:
$ awk '{s=x;for(i=1;i<=NF;i++){ s = s ? s (i <=2 ? " " : " | ") $i : $i } $0 = s}1' yourfile

# 3  
Old 04-09-2014
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 .

Code:
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
Code:
 
 
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 .
# 4  
Old 04-09-2014
Your output is not consistent! Please double check.
# 5  
Old 04-09-2014
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 .
# 6  
Old 04-09-2014
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
Code:
awk '{x=$1" "$2"|"$3"|"$4"|"; $1=$2=$3=$4=""; $0=x $0 }1'  infile

# 7  
Old 04-09-2014
Quote:
Originally Posted by rakesh_411
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 .

Code:
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
Code:
 
 
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 .
Third line pipe is missing, if you are expecting following o/p use this code

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delimiters with awk?

I have a file which is separated by delimiter "|", but the prob is one of my column do contain delimiter as description so how can i differentiate it? PS : the delmiter does have backslash coming before it, if occurring in column Annual|Beleagured|Desc|Denver... (2 Replies)
Discussion started by: nikhil jain
2 Replies

2. UNIX for Dummies Questions & Answers

delimiters used in UNIX

Can you point me to information on the different delimited in UNIX like colon, spaces and tabs? (1 Reply)
Discussion started by: momhef4
1 Replies

3. Shell Programming and Scripting

Inserting additional comma delimiters in a csv file, after and before certian fields.

Hello I have a csv file which I need to insert addtional commas into. The csv is of the format field1,field2,field3,field4,...etc...,field13,field14 I need to add extra commas in each record so that the final output looks like ... (1 Reply)
Discussion started by: kamal_p_99
1 Replies

4. Shell Programming and Scripting

sort with different delimiters

I have a file with the following lines in it: Inbound1:remote - - 01/Nov/2011:08:29:51 -0500 "GET / HTTP/1.1" 404 2098 HTTP Inbound1:remote - - 02/Dec/2011:08:31:42 -0500 "GET / HTTP/1.1" 404 2098 HTTP Inbound3:remote - - 01/Oct/2011:08:29:52 -0500 "GET / HTTP/1.1" 404 2098 HTTP Inbound4:remote... (5 Replies)
Discussion started by: oldman2
5 Replies

5. Shell Programming and Scripting

Delimiters in awk

Line from input file a : b : c " d " e " f : g : h " i " j " k " l output k b a Its taking 7th word when " is the delimiter, 2nd and 1st word when : is the delimiter and returning all in one line.... I am on solaris Thanks..... (1 Reply)
Discussion started by: shekhar2010us
1 Replies

6. Shell Programming and Scripting

Split using two delimiters

I'm trying to do a split using two delimiters. The first delimiter is ": " (or we could call it :\s). The second is "\n". How can or these delimiters so I can toss the values into an array without issue? I tried @array = split /:\s|\n/, $myvar; This doesn't seem to be working. Any an... (3 Replies)
Discussion started by: mrwatkin
3 Replies

7. Shell Programming and Scripting

Two delimiters with AWK

Hello, this thread is more about scripting style than a specific issue. I've to grep from a output some lines and from them obtain a specific entry delimited by < and >. This is my way : 1) grep -i user list | awk '{FS="<";print $NF}' | sed -e 's/>//g' 2) grep -i user list | cut -d","... (10 Replies)
Discussion started by: gogol_bordello
10 Replies

8. Shell Programming and Scripting

Problems with delimiters

Hello, I have data in a file something like this - UNB+UNOA:1+006415160:1+AR0000012360:ZZ+080701:0552+2++DELFOR++++T'UNH+2+DELFOR:D:97A:UN Here, the delimiters used are + , : and ' . I have a set of such files in which these delimiters vary from one file to another. I am developing a... (4 Replies)
Discussion started by: The Observer
4 Replies

9. Solaris

To extract everything between two delimiters

My input file looks like " @$SCRIPT/atp_asrmt_adj.sql $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1005w.pls $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1006w.pls start $SCRIPT/cim1020d.sql;^M spool $DATA/cim1021m.sql @$DATA/cim1021m.sql ! rm $DATA/cim1021m.sql spool $DATA/cim1021m.sql... (1 Reply)
Discussion started by: dowsed4u8
1 Replies

10. Shell Programming and Scripting

Delimiters missing

Hi I have a pipe-delimited file where I eventually need to replace a string stored on the 3th field on a specific record. This is how the file looks like: A|Mike|Lvl 1|... B|... A|Maria|Lvl 1|... C|... B|... A|Jimmy|Lvl 2|... C|... A|Carry|Lvl 0|... C|... B|... A|John|Lvl 8|...... (2 Replies)
Discussion started by: Indalecio
2 Replies
Login or Register to Ask a Question