Cut column and edit data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut column and edit data
# 1  
Old 03-26-2011
Cut column and edit data

Mar 26 12:32:53 name sshd[3261]: 192.168.1.14
Mar 27 12:42:53 name sshd[3262]: 192.168.1.14


how to make this data in output as:

"Mar 26 12:32:53","name","sshd","192.168.1.14"
"Mar 27 12:42:53","name","sshd","192.168.1.14"


anyone plzz help me out!!!!!!!!!!!!!!
# 2  
Old 03-26-2011
sed 's/ /","/g;s/","//2; s/","//1;s/.*/\"&\"/;s/sshd[^:]:/sshd/' infile

---------- Post updated at 12:56 PM ---------- Previous update was at 12:49 PM ----------

oops, i forgot a wildcard * and some space ... here you go :

Code:
sed 's/ /","/g;s/","/ /2;s/","/ /1;s/.*/\"&\"/;s/sshd[^:]*:/sshd/' infile

Code:
$ cat my
Mar 26 12:32:53 name sshd[3261]: 192.168.1.14
Mar 27 12:42:53 name sshd[3262]: 192.168.1.14
$ sed 's/ /","/g;s/","/ /2;s/","/ /1;s/.*/\"&\"/;s/sshd[^:]*:/sshd/' my
"Mar 26 12:32:53","name","sshd","192.168.1.14"
"Mar 27 12:42:53","name","sshd","192.168.1.14"
$

---------- Post updated at 12:57 PM ---------- Previous update was at 12:56 PM ----------

oops again, no need for escaping the "

Code:
$ sed 's/ /","/g;s/","/ /2;s/","/ /1;s/.*/"&"/;s/sshd[^:]*:/sshd/' my
"Mar 26 12:32:53","name","sshd","192.168.1.14"
"Mar 27 12:42:53","name","sshd","192.168.1.14"


Last edited by Franklin52; 04-01-2011 at 07:19 AM.. Reason: code tags
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 03-26-2011
awk and sed.

Code:
$cat file
Mar 26 12:32:53 name sshd[3261]: 192.168.1.14
Mar 27 12:42:53 name sshd[3262]: 192.168.1.14

Code:
$awk -v OFS='","' "sub(/\[[0-9]+\]:/,\"\",\$5) {print \$1\" \"\$2\" \"\$3,\$4,\$5,\$6}" file | sed 's/.*/\"&\"/'
"Mar 26 12:32:53","name","sshd","192.168.1.14"
"Mar 27 12:42:53","name","sshd","192.168.1.14"

# 4  
Old 03-26-2011
Code:
awk '{sub(/\[[0-9]*\]:/,"",$0);for (i=1;i<=NF;i++)x=(i<4)?((x)?x" "$i:"\""$i):x"\",\""$i; print x"\"";x=z}' my

---------- Post updated at 02:19 PM ---------- Previous update was at 01:59 PM ----------

or
Code:
$ awk '{sub(/\[[0-9]*\]:/,"",$0);for (i=1;i<=NF;i++)x=(i<4)?((x)?x" "$i:$i):x"\",\""$i;gsub(/^|$/,"\"",x); print x;x=z}' my
"Mar 26 12:32:53","name","sshd","192.168.1.14"
"Mar 27 12:42:53","name","sshd","192.168.1.14"


Last edited by Franklin52; 04-01-2011 at 07:20 AM.. Reason: code tags
# 5  
Old 03-26-2011
Another awk and sedSmilie
Code:
awk '{$0="\""$0"\"";$3=$3"\",\"";$5="\",\""substr($5,1,4)"\",\"";gsub(/(\" )|( \")/,"\"")}1' file

Code:
awk '{gsub(/ /,"\",\"");sub(/","/," ");sub(/","/," ");sub(/\[.*\]:/,"");sub(/^/,"\"");sub(/$/,"\"")}1' file

Code:
sed -e 's/^/"/;s/$/"/;s/ /","/g;s/","/ /;s/","/ /;s/\[.*\]://' file

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change data in one column with data from another file's column

Hello, I have this file outputData: # cat /tmp/outputData __Capacity^6^NBSC01_Licences^L3_functionality_for_ESB_switch __Capacity^2100^NBSC01_Licences^Gb_over_IP __Capacity^1837^NBSC01_Licences^EDGE_BSS_Fnc __Capacity^1816^NBSC01_Licences^GPRS_CS3_and_CS4... (1 Reply)
Discussion started by: nypreH
1 Replies

2. Shell Programming and Scripting

Need Help to Edit multiple column of a file

Hello Team, I want to know if there is any one liner command , using which I can edit multiple column of a file. input file input.txt (comma separated), taran, 12.45, uttam, 23.40, babay karan, 12.45, raju, 11.40, rahulg I want to update, 2nd and 4th column, but want all those column... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

3. Shell Programming and Scripting

Edit column using sort

Hi Expert, Please kindly need your help, I dont have any idea how to make this input to be as output. Thanks before $ more input.dat @zmap_fault HEADER , FALT, 80, 1 X (EASTING) , 1, 1, 1, 1, 15, 7, 0.1000000E+31, , 15, 7, 0 Y... (5 Replies)
Discussion started by: ipatah
5 Replies

4. Shell Programming and Scripting

Pick the column value based on another column using awk or CUT

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Please someone help me to resolve this issue. Source column1 column2 column3 column4... (2 Replies)
Discussion started by: Ganesh L
2 Replies

5. Shell Programming and Scripting

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

6. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

7. Shell Programming and Scripting

How to select and edit on a particular column?

How can I use awk to pick a particular column and work on it? For example, I want to count the number of characters in column10 that are separated by |? Thank you. (2 Replies)
Discussion started by: ivpz
2 Replies

8. Shell Programming and Scripting

Extract data based on match against one column data from a long list data

My input file: data_5 Ali 422 2.00E-45 102/253 140/253 24 data_3 Abu 202 60.00E-45 12/23 140/23 28 data_1 Ahmad 256 7.00E-45 120/235 140/235 22 data_4 Aman 365 8.00E-45 15/65 140/65 20 data_10 Jones 869 9.00E-45 65/253 140/253 18... (12 Replies)
Discussion started by: patrick87
12 Replies

9. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

10. Programming

edit, and delete data from you file

Hi, I'm new baby in linux and i need your help to make a script. i have a file like this: nameuser Password="password-user" Data-profile=tipe-profile, IP-Address=ip-data, country=country_user, old-user=old_user Scripts I need that allow search, edit, and delete data from you file. ... (1 Reply)
Discussion started by: dorek
1 Replies
Login or Register to Ask a Question