substitute in first column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substitute in first column
# 1  
Old 04-06-2012
substitute in first column

Hi,

I have a file which has multiple columns in and i want to replace first column with : in between them.

input_file

Code:
21000012343456 asdf
21000012343478 asd3
21000012343423 asdf
21000012343445 asdf
21000012343489 asdf

output file
Code:
21:00:00:12:34:34:56 asdf
21:00:00:12:34:34:78 asd3
21:00:00:12:34:34:23 asdf
21:00:00:12:34:34:45 asdf
21:00:00:12:34:34:89 asdf

I want to substitute ":" after every second digit in the fist column. Please let me know if I can do in awk. I can do it using sed. Is there any way if I can use sed in awk.

Thanks in advance.

Last edited by joeyg; 04-06-2012 at 02:47 PM.. Reason: Please wrap data and coding in CodeTags.
# 2  
Old 04-06-2012
Code:
[root@dist unix]# sed 's/../&:/g;s/://7g' file
21:00:00:12:34:34:56 asdf
21:00:00:12:34:34:78 asd3
21:00:00:12:34:34:23 asdf
21:00:00:12:34:34:45 asdf
21:00:00:12:34:34:89 asdf

This User Gave Thanks to SaCai For This Post:
# 3  
Old 04-06-2012
Code:
awk 'BEGIN{FIELDWIDTHS="2 2 2 2 2 2 2 7"}{OFS=":"; print $1,$2,$3,$4,$5,$6,$7 $8}' infile

This User Gave Thanks to complex.invoke For This Post:
# 4  
Old 04-06-2012
See if this helps :

Code:
awk '{gsub("[0-9][0-9]","&:",$1);sub(":$","",$1) }1 ' input

Regards
Peasant
This User Gave Thanks to Peasant For This Post:
# 5  
Old 04-06-2012
Quote:
Originally Posted by SaCai
Code:
[root@dist unix]# sed 's/../&:/g;s/://7g' file

Quote:
Originally Posted by huaihaizi3
Code:
awk 'BEGIN{FIELDWIDTHS="2 2 2 2 2 2 2 7"}{OFS=":"; print $1,$2,$3,$4,$5,$6,$7 $8}' infile

NOTE: these solutions are GNU only, nothing wrong with that, but this will not work on every platform...

Last edited by Scrutinizer; 04-06-2012 at 03:48 PM..
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 6  
Old 04-07-2012
thanks a lot everyone.

Thanks a lot everyone!!!. You have been great help.
# 7  
Old 04-09-2012
Some awks can do this:
Code:
awk '{for(i=2;i<=12;i+=2)$i=$i":"}1' FS= OFS= infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Substitute first column based on second column

Hi, I have files with lines that look like HSQ1008:141:D0CC8ACXX:3:1106:17255:124378 163 chr14 I'm wondering how I can output a file that appends /1 if the second column is 99 or 83 and a /2 if the second column is 163 or 147. Also I want to put \n + \n between each the second... (1 Reply)
Discussion started by: jyu429
1 Replies

2. UNIX for Dummies Questions & Answers

How can i substitute a header column?

Hi, I have a txt file with multiple columns and i want to substitute the header of the first column. Example: Seq. Name Seq. Length #Hits min. eValue mean Similarity #GOs GOs Enzyme Codes InterProScan comp1000201_c0_seq1 ---NA--- 337 0 0 - comp1000297_c0_seq1 ---NA--- 612 0 0 -... (4 Replies)
Discussion started by: alisrpp
4 Replies

3. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

4. Shell Programming and Scripting

awk to substitute third column if first column is greater than interest

A file 2400 2800 PSC000289 3200 3896 PCS000289 3333 3666 PCS000221 222 1000 PCS000222 3299 3600 PSC000289 Question is while if third column is PCS000289 and first column should be greater than 3000, then replace PCS000289 by YES, remaining the others column same. ... (1 Reply)
Discussion started by: cdfd123
1 Replies

5. UNIX for Dummies Questions & Answers

substitute (')

I usually use : Code: awk '{gsub(/xxx/,"yyy");print}' to substitute xxx with yyy. I have a problem substitute an expression like Code: x ' y Because of the ( ' ) Any idea on how to get over this problem? Thanks (2 Replies)
Discussion started by: cosmologist
2 Replies

6. Shell Programming and Scripting

using awk to substitute data in a column delimited text file

using awk to substitute data in a column delimited text file hello i would like to use awk to do the following calculation from the following snippet. input file C;2390 ;CV BOUILLOTTE 2L 2FACES NERVUREES ;1.00 ;3552612239004;13417 ;25 ;50 ; 12;50000 ; ; ... (3 Replies)
Discussion started by: iindie
3 Replies

7. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

8. Shell Programming and Scripting

vi substitute

My question is how would I substitute for ceratain number of occurences in a line? If this is my input rjohns BFSTDBS01 Standard Silver NPRO30DINCR 2 Client Is it possible to change the first 3 occurences of space " " to a comma? (7 Replies)
Discussion started by: reggiej
7 Replies

9. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

10. UNIX for Dummies Questions & Answers

Substitute in vi

I know in vi you can do :%s/replaceme/withthis/ but if i want to find all lines say without a # at the begining and I want to put it in how would that command be formatted? I can't figure it out for the life of me. #comment blah1 hey1 grrr1 #comment #blah1 #hey1 #grrr1 (5 Replies)
Discussion started by: kingdbag
5 Replies
Login or Register to Ask a Question