Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Fixed width file search based on position value

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-07-2013   -   Original Discussion by onesuri
onesuri onesuri is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 11 January 2016, 10:05 AM EST
Posts: 82
Thanks: 1
Thanked 0 Times in 0 Posts
Fixed width file search based on position value

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


Code:
sample data 

abcd1234xgyhsyshijfkfk
hujk9876       io xgla
loki8787eljuwoejroiweo
dkfj9098           dja

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

abcd1234xgyhsyshijfkfk

Thanks
onesuri
Sponsored Links
    #2  
Old Unix and Linux 03-07-2013   -   Original Discussion by onesuri
anbu23's Unix or Linux Image
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 8 February 2017, 4:04 AM EST
Location: Bangalore,India
Posts: 2,153
Thanks: 21
Thanked 159 Times in 150 Posts

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

Sponsored Links
    #3  
Old Unix and Linux 03-07-2013   -   Original Discussion by onesuri
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 November 2017, 12:12 PM EST
Location: Aachen, Germany
Posts: 11,622
Thanks: 320
Thanked 3,615 Times in 3,320 Posts
The position of "xg" is 9-10 despite the phrasing in the spec. Try
Code:
$ grep -E "^.{8}xg" file
abcd1234xgyhsyshijfkfk

or
Code:
$ sed -En "/^.{8}xg/p" file
abcd1234xgyhsyshijfkfk

    #4  
Old Unix and Linux 03-07-2013   -   Original Discussion by onesuri
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 11 October 2017, 11:47 AM EDT
Location: France
Posts: 2,962
Thanks: 85
Thanked 637 Times in 608 Posts

Code:
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
    #5  
Old Unix and Linux 03-07-2013   -   Original Discussion by onesuri
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 17 November 2017, 12:53 PM EST
Location: Lancashire, UK
Posts: 3,365
Thanks: 1,453
Thanked 665 Times in 598 Posts
Dear ctsgnb,

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



Wonderful.


Robin
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 09:09 PM.