• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read charcters from first line of a file  RSS feed

 
Priyaa V Kumar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a file that is position specific. It contains 12 columns of data for each record. It is position specific, ie always column 1 's value is from position 1 to position 5, then delimiter space, then column2's value from position 6 to 20 etc....

If the column's width is say 15 and if the actual value is only 9 chars, then it is left padded with spaces to make it correct length. Hence there will be more than one space between some fields. Also, some fields might be blank too. In that case it it filled with all spaces.

I have to get the value of column 6 ie say from position 135 to 145. I tried like this...

VAR_LINE=`head -1 $INPUT_FILE`

When I printed, I saw that

VAR_LINE contained the first line but the spaces in between were shrunk to 1. So, I was not able to get the value i was looking for when i used

MYVAL=`echo $VAR_LINE | cut -c135-145`

So, I tried

MYVAL=`echo $VAR_LINE|cut -d' ' -f6-6`

This worked fine when they were no empty columns. But it failed when there is a column that is empty and got padded with just spaces to maintain the position.

Please suggest me how I should be approaching this. I am not familiar with awk. I tried to do it... but it would be nice if you could suggest some solution.

Thanks.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I would use java (or c/c++) in this case.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!