removing spaces from first column

11,296

Solution 1

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.

Solution 2

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.

Solution 3

You can try this:

awk '$1=$1' file

to remove all leading space in first column.

Share:
11,296

Related videos on Youtube

user61398
Author by

user61398

Updated on September 18, 2022

Comments

  • user61398
    user61398 over 1 year

    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   
    
    • S edwards
      S edwards about 10 years
      it's really unclear what you want,your example is a bit confusing, what do you want to do with data in first column ?
    • mulaz
      mulaz about 10 years
      ^his comment + where did you get the "2" in 3rd line?
    • user61398
      user61398 about 10 years
      Dear Kiwy, Sorry my out put file shoul be same as input but removing all spaces.
    • Graeme
      Graeme about 10 years
      Your example doesn't have different numbers of spaces, it has different lengths of numbers!
    • user61398
      user61398 about 10 years
      i wrote my input with different space, but i dont why it is not showing.
    • user61398
      user61398 about 10 years
      16504 16516 1650811 16520 1651 16524 16516111 16528
    • Graeme
      Graeme about 10 years
      The spaces show up now, I edited the question. You still need to explain the different length of numbers though.
    • S edwards
      S edwards about 10 years
      the problem here is for this line for example: 16516111 16528 how it can become 16516 16528 where are the trainling 111 of your first column
    • user61398
      user61398 about 10 years
      The numbers will be kept the same but i had problem while pasting the numbers in your editor. The ouput should be in this format : 16504 16516 1650811 16520 1651 16524 16516111 16528 165204 16532
    • Rahul Patil
      Rahul Patil about 10 years
      if you want both column properly then you can use column -t < input_file
    • slm
      slm about 10 years
      Can you please edit your input data or output data so that it includes the correct data? See the "edit" link above Graeme's name, click it to edit your question to refine it.
    • terdon
      terdon about 10 years
      Also, please read formatting help as peterph suggested.
  • S edwards
    S edwards about 10 years
    didn't know this tool, it's really nice
  • slm
    slm about 10 years
    @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.