Fixed width file search based on position value | Unix Linux Forums | Shell Programming and Scripting

  Go Back    

Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Fixed width file search based on position value

Shell Programming and Scripting

Closed Thread    
Thread Tools Search this Thread Display Modes
Old 03-07-2013
onesuri onesuri is offline
Registered User
Join Date: Feb 2008
Last Activity: 13 February 2014, 1:57 PM EST
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Fixed width file search based on position value

I am unable to find the right option to extract the data in the fixed width file.

sample data 

hujk9876       io xgla
dkfj9098           dja

Search based on position 8-9="xg" and print the entire row


Sponsored Links
Old 03-07-2013
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Join Date: Mar 2006
Last Activity: 1 March 2015, 3:57 AM EST
Location: Bangalore,India
Posts: 2,136
Thanks: 14
Thanked 150 Times in 143 Posts

$ awk ' substr($0,9,2) == "xg" ' file

Sponsored Links
Old 03-07-2013
RudiC RudiC is offline Forum Advisor  
Registered User
Join Date: Jul 2012
Last Activity: 1 March 2015, 12:06 PM EST
Location: Aachen, Germany
Posts: 5,413
Thanks: 94
Thanked 1,409 Times in 1,327 Posts
The position of "xg" is 9-10 despite the phrasing in the spec. Try
$ grep -E "^.{8}xg" file

$ sed -En "/^.{8}xg/p" file

Old 03-07-2013
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
Join Date: Oct 2010
Last Activity: 16 December 2014, 5:07 AM EST
Location: France
Posts: 2,930
Thanks: 81
Thanked 624 Times in 596 Posts

grep ^........xg yourfile
egrep ^.{8}xg yourfile

The Following User Says Thank You to ctsgnb For This Useful Post:
rbatte1 (03-07-2013)
Sponsored Links
Old 03-07-2013
rbatte1 rbatte1 is offline Forum Staff  
Join Date: Jun 2007
Last Activity: 26 February 2015, 9:56 AM EST
Location: Lancashire, UK
Posts: 2,105
Thanks: 693
Thanked 374 Times in 333 Posts
Dear ctsgnb,

What a fantastic and concise coding technique. I've fought with this for years, often cobbling some terrible script round it.


Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to split a fixed width text file into several ones based on a column value? bhanja_trinanja Shell Programming and Scripting 14 12-28-2010 03:15 PM
Remove duplicates based on a column in fixed width file Qwerty123 UNIX for Dummies Questions & Answers 1 07-15-2010 07:37 AM
row to column and position data in to fixed column width baluchen Shell Programming and Scripting 4 03-15-2010 07:46 AM
Append line based on fixed position ashikin_8119 Shell Programming and Scripting 2 03-19-2008 05:09 AM

All times are GMT -4. The time now is 04:53 PM.