The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
grep unix.com with google



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Aug 2009
Posts: 18
Search and replace particular characters in fixed length-file

Masters,
I have fixed length input file like
Code:
FHEAD0000000001XXXX20090901      0000009000Y1000XXX2
THEAD000000000220090901      ITM0000109393813            430143504352N22SP    000000000000RN000000010000EA  P0000000000000014390020090901      
TTAIL0000000003000000
FTAIL00000000040000000002

Note the fields in Bold Italicized.. These fields need to be replaced with variable in shell script( of same format YYYYMMDD ). I used cut command, but it is trimming the spaces between the fields.
How to I achieve this field replacement, without changing the length/ format of the file

---------- Post updated at 03:08 PM ---------- Previous update was at 03:07 PM ----------

Forogt to mention that I have to change only in FHEAD and THEAD records only in those positions.
  #2 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Sep 2006
Posts: 2,604

Code:
while IFS= read -r line
do
    case "$line" in
        FHEAD*) echo "${line:0:19}$(date +%Y%m%d)${line:27}";;
        THEAD*) echo "${line:0:15}$(date +%Y%m%d)${line:23}";;
        *) echo "$line";;
    esac
done <"file"

  #3 (permalink)  
Old 11-27-2009
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 4,670
With awk:


Code:
awk -v var="$(date +%Y%m%d)" '
/^FHEAD/{print substr($0,1,19) var substr($0,28); next}
/^THEAD/{print substr($0,1,15) var substr($0,24,106) var substr($0,138); next}
1' file > newfile

  #4 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Jun 2006
Posts: 468

Code:
$ d="20091128"

$ echo $d
20091128

$ sed "s/\([FT]HEAD[0-9a-zA-Z]*\)[0-9]\{8\}/\1$d/g" urfile

Shorter


Code:
sed "s/\([FT]HEAD\S*\)[0-9]\{8\}/\1$d/g" urfile


Last edited by rdcwayx; 11-27-2009 at 07:31 AM..
  #5 (permalink)  
Old 11-27-2009
Registered User
 

Join Date: Aug 2009
Posts: 18
thanx guys..tried awk command and working the way i want....will try other commands too...
Sponsored Links
Reply

Bookmarks

Tags
cut, fixed length files, replace characters

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search and replace fixed length record file nedkelly007 Shell Programming and Scripting 0 08-06-2009 03:26 PM
awk - replace number of string length from search and replace for a serialized array otrotipo Shell Programming and Scripting 1 07-10-2009 01:04 PM
Convert a tab delimited/variable length file to fixed length file Everton_Silveir UNIX for Dummies Questions & Answers 2 12-19-2008 06:21 PM
What the command to find out the record length of a fixed length file? tranq01 UNIX for Dummies Questions & Answers 9 12-04-2008 04:04 PM
sed replace with fixed length McLan Shell Programming and Scripting 7 05-15-2008 03:57 AM



All times are GMT -4. The time now is 01:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0