• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Read charcters from first line of a file

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I would use java (or c/c++) in this case.
 
What's that smell? Hey, sniff this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic