Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

My input file has positions in first column with different number of spaces (or no space)

16504   16516       
    1650811   16520      
 1651   16524      
  16516111   16528       
 165204   16532       

I need to get an output file where fist column has no spaces at all, while keeping second column as it is.

16504   16516       
16508   16520     
16512   16524      
16516   16528       
16520   16532   
share|improve this question
1  
it's really unclear what you want,your example is a bit confusing, what do you want to do with data in first column ? –  Kiwy Feb 26 at 12:53
1  
^his comment + where did you get the "2" in 3rd line? –  mulaz Feb 26 at 12:55
1  
Your example doesn't have different numbers of spaces, it has different lengths of numbers! –  Graeme Feb 26 at 12:55
1  
The spaces show up now, I edited the question. You still need to explain the different length of numbers though. –  Graeme Feb 26 at 13:04
2  
if you want both column properly then you can use column -t < input_file –  Rahul Patil Feb 26 at 13:13

3 Answers 3

If this is just an alignment issue, and you want to line up the data into columns:

$ column -t somefile
16504     16516
1650811   16520
1651      16524
16516111  16528
165204    16532

The -t switch to column will create a table from the source data, automatically:

   -t, --table
          Determine the number of columns the input contains and create a 
          table.  Columns are delimited with whitespace,  by  default,
          or with the characters supplied using the separator. Table output 
          is useful for pretty-printing.
share|improve this answer
    
didn't know this tool, it's really nice –  Kiwy Feb 26 at 13:24
    
@Kiwy - yes that and expand is another useful tool. I use expand when I post examples of output that include tabs. It removes them, substituting in spaces in their place. You can control the number of spaces w/ it. –  slm Feb 26 at 13:26

You are likely after something like:

sed "s/^ *//" < your_file

which removes any leading spaces (replaces them with an empty string). If you also have variable number of spaces between the columns, you can extend it to:

sed "s/^ *//;s/  */    /g" < your_file

which (after removing the leading spaces) replaces any occurrence of one or more spaces with a fixed string (in this case 4 spaces). You may also want to use \s instead of plain space in the matching pattern to cover cases where a Tab character is used.

share|improve this answer

You can try this:

awk '$1=$1' file

to remove all leading space in first column.

share|improve this answer
    
Thanks, it worked great. –  user61398 Feb 26 at 13:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.