Search and replace particular characters in fixed length-file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace particular characters in fixed length-file
# 1  
Old 11-27-2009
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  
Old 11-27-2009
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  
Old 11-27-2009
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  
Old 11-27-2009
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  
Old 11-27-2009
thanx guys..tried awk command and working the way i want....will try other commands too...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace value based on certain conditions in a fixed width file

Hi Forum. I tried searching for a solution using the internet search but I haven't been able to find any solution for what I'm trying to accomplish. I have a fixed width column file where I need to search for any occurrences of "D0" in col pos.#1-2, 10-11, 20-21 and replaced it with "XD". ... (2 Replies)
Discussion started by: pchang
2 Replies

2. Shell Programming and Scripting

Replace characters at fixed positions

My objective is to replace the 8th, 9th, 10th characters by 1 space per character (total 3 spaces) in a file. I achieved this using following command: sed 's/\(.\)/\1@/7;s/@\(...\)/ /' FileData.txt > FileData_UPDATED.txt Another situation comes when I need to done same but excluding 1st... (5 Replies)
Discussion started by: manishdivs
5 Replies

3. Shell Programming and Scripting

Replace and Increment a value in the fixed length file

Hi Everyone, I need to increment a value in the fixed length file. The file has almost a million rows. Is there any easy way to accomplish this. Ex input file ASDSD ADSD 00000 X AAASD ADSD 00000 X SDDDD ADSD 00000 X Ouput ASDSD ADSD 00001 X AAASD ADSD 00002 X SDDDD ADSD 00003 X ... (7 Replies)
Discussion started by: saratha14
7 Replies

4. Shell Programming and Scripting

Remove characters from fixed length file

Hello I've question on the requirement I am working on. We are getting a fixed length file with "33" characters long. We are processing that file loading into DB. Now some times we are getting a file with "35" characters long. In this case I have to remove two characters (in 22,23... (14 Replies)
Discussion started by: manasvi24
14 Replies

5. Shell Programming and Scripting

Replace Date in a fixed length file

Hello All, I working on ksh. I am using fixed length file. My file is like: ======== IXTTIV110827 NANTH AM IKSHIT ABCDEF 0617 IJAY NAND EENIG ZXYWVU 0912 AP OOK OONG PQRSTU100923 NASA DISH TTY ASDFG 0223 GHU UMA LAM QWERT 0111 ATHE SH THEW ======= From 7th to 12 is a date... (4 Replies)
Discussion started by: AnanthaDikshit
4 Replies

6. Shell Programming and Scripting

search and replace fixed length record file

Hi I need to be search a file of fixed length records and when I hit a particular record that match a search string, substitute a known position field In the example file below FHEAD000000000120090806143011 THEAD0000000002Y0000000012 P00000000000000001234 TTAIL0000000003... (0 Replies)
Discussion started by: nedkelly007
0 Replies

7. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

8. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

9. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

10. Shell Programming and Scripting

sed replace with fixed length

$ cat template s.noName $ sed "s/s.no/1/" template > out $ sed "s/s.no/100/" template >>out $ cat out 1Name 100Name 1 Name 100Name (7 Replies)
Discussion started by: McLan
7 Replies
Login or Register to Ask a Question