how to introduce a space in a single column without distrubing the other columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to introduce a space in a single column without distrubing the other columns
# 1  
Old 10-29-2009
how to introduce a space in a single column without distrubing the other columns

Hello Experts,

I am new to this forum, I would like to do the following changes in one of the column of a txt file, which is having around 9 column.

For example, column 3 is having letters like this

Code:
AB11
AB12
C
CA
CB
AC1
AC2

I would like to convert the same column as follows

Code:
1AB1
2AB1
 C
 CA
 CB 
 AC1
 AC2

if it is having less than four letters, then the program need to introduce a space first and the the letter. If it is having four letters, then the program should bring the last number to the first place.

Need to do the conversion only in that particular column and the rest should be as such.

Can any one help me in this regard?

Expecting your replies and thanks in advance.

Regards
Fredrick.

Last edited by Franklin52; 10-29-2009 at 11:29 AM.. Reason: Please use code tags!
# 2  
Old 10-29-2009
With sed:
Code:
$> cat infile
AB11
AB12
C
CA
CB
AC1
AC2
$> sed 's/^.\{1,3\}$/ &/g; s/^\([^ ]..\)\(.\)$/\2\1/g' infile
1AB1
2AB1
 C
 CA
 CB
 AC1
 AC2

# 3  
Old 10-29-2009
awk alternative:

Code:
$ awk -v FS="" 'NF==4{$0=$4 $1 $2 $3}NF<4{$0=" " $0}1' file

# 4  
Old 10-29-2009
Thank you very much for your reply. The input file i mentioned here, is having 9 columns and i need to change the above mentioned, only in 3rd column.

Is there any other way to do this?

expecting your replies and thanks in advance.

Regards
Fredrick.
# 5  
Old 10-29-2009
It's easier when provided with a realistic sample file from the beginning.

Code:
$ cat f
col1 col2 AB11 col4
col1 col2 AB12 col4
col1 col2 C col4
col1 col2 CA col4
col1 col2 CB col4
col1 col2 AC1 col4
col1 col2 AC2 col4

$ awk 'length($3)==4{$3=substr($3, 4) substr($3,1,3)}length($3)<4{$3=" " $3}1' f
col1 col2 1AB1 col4
col1 col2 2AB1 col4
col1 col2  C col4
col1 col2  CA col4
col1 col2  CB col4
col1 col2  AC1 col4
col1 col2  AC2 col4

# 6  
Old 10-29-2009
Hi Zaxxon,

Code:
$> sed 's/^.\{1,3\}$/ &/g; s/^\([^ ]..\)\(.\)$/\2\1/g' infile

Thats cool, its working fine. But the input file i mentioned here, is having 9 columns and i need to change the above mentioned, only in the 3rd column.

Is there any other way to do this?

Expecting your replies and thanks in advance.

Regards
Fredrick.

---------- Post updated at 05:06 PM ---------- Previous update was at 04:55 PM ----------

Hi Ripat,

Good, its working fine..
Thank you..

regards
Fredrick.

Last edited by zaxxon; 10-29-2009 at 01:17 PM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to generate one long column by merging two separate two columns in a single file?

Dear all, I have a simple question. I have a file like below (separated by tab): col1 col2 col3 col4 col5 col6 col7 21 66745 rs1234 21 rs5678 23334 0.89 21 66745 rs2334 21 rs9978 23334 0.89 21 66745 ... (4 Replies)
Discussion started by: forevertl
4 Replies

2. Shell Programming and Scripting

Insert space in specific column among many columns

Hello, I have some problem in inserting the space for the pairs of columns. I have the input file : I used this code below in replacing it using space in specific column (replace space in each two columns) sed -e "s/,/ /2" -e "s/,/ /3" inputfile Output showed : However, I have many... (3 Replies)
Discussion started by: awil
3 Replies

3. Shell Programming and Scripting

Transpose multiple rows (with a mix of space and enter) to a single column

How to change the uploaded weekly file data to the following format? New Well_Id,Old Well_Id,District,Thana,Date,Data,R.L,WellType,Lati.,Longi. BAG001,PT006,BARGUNA,AMTALI,1/2/1978,1.81,2.29,Piezometer,220825,901430 BAG001,PT006,BARGUNA,AMTALI,1/9/1978,1.87,2.29,Piezometer,220825,901430... (3 Replies)
Discussion started by: sara.nowreen
3 Replies

4. Shell Programming and Scripting

Multiple columns to a single column

I have this input: 10 22 1 100 11 22 10 1 50 14 3 1 100 23 3 1 100 24 15 1 100 10 22 5 3 1 33.333 11 22 1 100 It has an inconsistent number of fields but the last field is determined by 100/(NF-2) using awk. I want to take this multiple columned input file and transform so that... (2 Replies)
Discussion started by: mdlloyd7
2 Replies

5. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

6. Shell Programming and Scripting

How to convert a single column into several columns?

Hi I have a ksh script which gives me the output as a single column with several rows like: AAA BBB CCC DDD EEE FFF GGG HHH III I want to be able to create a new file from this file which allows me to set the number of rows and columns in the new file, i.e. for this example, if I... (30 Replies)
Discussion started by: pinpe
30 Replies

7. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

8. UNIX for Dummies Questions & Answers

How to convert a single column into several rows and columns?

I have a program which gives me the output as a single column with hundreds of rows like: 213 314 324 324 123 I want to be able to create a new file from this file which allows me to set the number of rows and columns in the new file, i.e. for this example, if I specify 3 rows and 2... (5 Replies)
Discussion started by: ashton_smith
5 Replies

9. Shell Programming and Scripting

Single column to multiple columns in awk

Hi - I'm new to the awk programming language. I'm trying to print a single column of data to several columns, and I found an article on iTWorld.com (ITworld.com - Printing in columns). It looks like the mkCols2 script is very close to what I need to do, but it looks like the end of the code... (2 Replies)
Discussion started by: astroDave
2 Replies

10. UNIX for Dummies Questions & Answers

single column to multiple columns

Hello, I have a single column of data that I would like to cut/print (with awk or ...) into multiple columns at every empty row (or common character). Input: 5.99123 5.94693 7.21383 5.95202 0.907935 5.99149 6.08427 0.975774 6.077 Output: 5.99123 5.95202 6.08427 5.94693... (7 Replies)
Discussion started by: agibbs
7 Replies
Login or Register to Ask a Question