Adding blank white sapce at specific column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding blank white sapce at specific column
# 1  
Old 08-07-2007
Adding blank white sapce at specific column

I have a file looking like this. But, if you look at second line, the number are stick together(e.g. 33.9918.913418.9570). What I want to do is to separate these number by white space. I tried to use tr below, but it doesn't work. Any help would be appreciated.
<Input>
7.23 7.32 5.21 10.13 5.06 5.06 0.2381 0.7772 0.6152
41.28 45.18 48.37 51.92 33.41 33.9918.913418.9570 1.1610
6.53 7.44 8.22 9.14 5.06 5.06 0.5788 0.5788 0.6050

<Anticipated result>
7.23 7.32 5.21 10.13 5.06 5.06 0.2381 0.7772 0.6152
41.28 45.18 48.37 51.92 33.41 33.99 18.9134 18.9570 1.1610
6.53 7.44 8.22 9.14 5.06 5.06 0.5788 0.5788 0.6050

cut -c 30-35 file > file.tmp1
cut -c 36-42 file > file.tmp2
cut -c 43-56 file > file.tmp3

cat file.tmp1 file.tmp2 file.tmp3 > file.tmp4

But, it doesn't work. Any easy solution using awk and/or sed?

Thanks,
Jae
# 2  
Old 08-07-2007
Quote:
41.28 45.18 48.37 51.92 33.41 33.9918.913418.9570 1.1610
Could you please elaborate this ?

How do you identify there should be a space only after 33.99 and 18.9134 ?

Any specified conditions for that ?
# 3  
Old 08-07-2007
Quote:
41.28 45.18 48.37 51.92 33.41 33.9918.913418.9570 1.1610

The original format looks above no space between 33.99,18.9134, and 18.9570. That is the problem.
My goal is to add "space" between these values so that all values should be separated by space like this.
41.28 45.18 48.37 51.92 33.41 33.99 18.9134 18.9570 1.1610

>Could you please elaborate this ?

>How do you identify there should be a space only after 33.99 and 18.9134 ?

>Any specified conditions for that ?
# 4  
Old 08-07-2007
Code:
41.28 45.18 48.37 51.92 33.41 33.9918.913418.9570 1.1610

The original format looks above no space between 33.99,18.9134, and 18.9570. That is the problem.
My goal is to add "space" between these values so that all values should be separated by space like this.
41.28 45.18 48.37 51.92 33.41 33.99 18.9134 18.9570 1.1610

This is still not clear.

I can get the output.

But how do you transform it. Is there any logic for the grouping of numbers mentioned above ?

For example :

I could just split the above as

33.9 918.913 418.9570 something like that

Now I could see that there is a comma in the format posted just now and no such comma in the original file format

Sorry that its not clear for me !
# 5  
Old 08-07-2007
Let me try to clarify.
Input file has many lines.
7.23 7.32 5.21 10.13 5.06 5.06 0.2381 0.7772 0.6152
41.28 45.18 48.37 51.92 33.41 33.9918.913418.9570 1.1610
6.53 7.44 8.22 9.14 5.06 5.06 0.5788 0.5788 0.6050
31.38 25.17 18.37 41.92 13.41 31.2017.211218.2870 7.1710
21.28 35.15 28.37 21.92 3.41 23.9216.817619.4525 2.3615
...
...
but all values should be separated by spaces.
As you can see, column 7,8, and 9 can not be separated in line 2, 4, and 5 because all numbers are stick together without space.
The reason is that the precision of the first 6 values are two decimal points after period, while that of last 3 values are four decimal points after period.
Such high precision of the last 3 values are used up the space separator between values.

As you said, there should be a space only after 33.99 and 18.9134 in second line, 31.20 and 17.2112 in 4th line, 23.92 and 16.8176 in 5th line..and so on.

Hope this is help for better understanding.
Thanks,
# 6  
Old 08-07-2007
Code:
#!/bin/ksh
typeset -L2 mTwo
typeset -L4 mFour
sed 's/ //g;s/\./ /g' input_file | \
while read m1 m2 m3 m4 m5 m6 m7 m8 m9 m10
do
  mTwo=${m2}
  mF1=${m1}'.'${mTwo}

  mTwo=${m3}
  mF2=`echo ${m2} | cut -c3-`'.'${mTwo}

  mTwo=${m4}
  mF3=`echo ${m3} | cut -c3-`'.'${mTwo}

  mTwo=${m5}
  mF4=`echo ${m4} | cut -c3-`'.'${mTwo}

  mTwo=${m6}
  mF5=`echo ${m5} | cut -c3-`'.'${mTwo}

  mTwo=${m7}
  mF6=`echo ${m6} | cut -c3-`'.'${mTwo}

  mFour=${m8}
  mF7=`echo ${m7} | cut -c3-`'.'${mFour}

  mFour=${m9}
  mF8=`echo ${m8} | cut -c5-`'.'${mFour}

  mFour=${m10}
  mF9=`echo ${m9} | cut -c5-`'.'${mFour}

  echo ${mF1} ${mF2} ${mF3} ${mF4} ${mF5} ${mF6} ${mF7} ${mF8} ${mF9}
done

# 7  
Old 08-08-2007
Assuming your numbers are at most 2 digits to the left of the decimal
Code:
perl -pe 's/\B\d\d\./ $&/g' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split column when value in column is blank in any row

Hi Experts, In short : Need to split file when field in column 5 is blank and need to generate two file in which column 5 is blank and other in which column 5 has values along with other rows and column data My issue is i am not able to get header for column from raw file into new file which... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

3. Shell Programming and Scripting

Removing blank/white spaces and special characters

Hello All , 1. I am trying to do a task where I need to remove Blank spaces from my file , I am usingawk '{$1=$1}{print}' file>file1Input :- ;05/12/1990 ;31/03/2014 ; Output:- ;05/12/1990 ;31/03/2014 ;This command is not removing all spaces from... (6 Replies)
Discussion started by: himanshu sood
6 Replies

4. Shell Programming and Scripting

Insert data in first column(if blank) from previous line first column

Dear Team I need to insert field(which is need to taken from previous line's first field) in first column if its blank. I had tried using sed but not find the way. Detail input and output file as below. Kindly help for same. INPUT: SCGR SC DEV DEV1 NUMDEV DCP ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

5. Shell Programming and Scripting

Shell command to Strip white spaces at specific location

Hello, I have a Comma separated input file with one of the sample records as below: -9223372036854477528,"834","834003325515BXNI00101012013C","5","PLAN_VALUE","PPO, General Cable","C",20130101,99991231,"A","2012-12-25-13.58.14.434000","ZPL2 ","2012-12-25-13.58.14.434000","ZPL2 ... (4 Replies)
Discussion started by: Praveenkulkarni
4 Replies

6. Shell Programming and Scripting

Add a character C in a column if that column is blank

I have some files that look as follows. I need to add a character 'C' in the fifth column if that column is blank. I prefer in-place editing. 1 1 B M 0 0 203 0, 0.0 0, 0.0 0, 0.0 0, 0.0 0.000 360.0 360.0 360.0 141.9 15.4 28.8 66.1 2 2 B A ... (21 Replies)
Discussion started by: thejitha
21 Replies

7. UNIX for Dummies Questions & Answers

Adding tags to a specific column of a space delimited text file

I have a space delimited text file with two columns. I would like to add NA to the first column of the text file. Input: 19625 10.4791768259 19700 10.8146489183 19701 10.9084026759 19702 10.9861346978 19703 10.9304364984 Output: NA19625 10.4791768259 NA19700 10.8146489183... (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

remove white space from specific columns in text file

Hello i have a text file like this: 1 AB AC AD EE 2 WE TR YT WW 3 AS UY RF YT the file is bigger , but that's an example of the data what i want to do is to merge all columns together except the first one, it will become like this : 1 ABACADEE 2 WETRYTWW 3 ASUYRFYT (8 Replies)
Discussion started by: shelladdict
8 Replies

9. Solaris

Swap sapce Adding ..??

How to add the Swap space... i know this procedure but it doesn/t work. swap -l to see the swap space (swap -s) Creak swap file mkdir 100m swap.file add the swap file swap -a swap.file... but it not work ..!! (9 Replies)
Discussion started by: udayn
9 Replies

10. Shell Programming and Scripting

Sh scripting problem, matching specific white space

Hey, I'm desperately in need of a solution to a seemingly easy problem. How can I match a specific number of spaces and replace them. As in, I have a file that instead of being broken into parts by new lines is broken into parts via 500+ spaces. How can I replace any grouping of more than 400... (7 Replies)
Discussion started by: Dickalicious
7 Replies
Login or Register to Ask a Question