To replace the value of the column in a fixed width file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To replace the value of the column in a fixed width file
# 8  
Old 08-03-2013
What's the difference between the 3 sections header,detail,trailer?
Could you give 3 example lines for each? (To find a contents criterion.)
Perhaps there are fixed line numbers, e.g. trailer starts at line 100 or is 10 lines long?
# 9  
Old 08-03-2013
Please find the below record.In this the first and the last record is header & trailer.


Code:
HAR  1129201202:33:13   
D3065507                           
TAR000000000000144

Suppose the file name is Test123.txt if I use the command

sed 's/\(.\{1\}\)../\1yyy/' Test123.txt it will give me the result as mentioned below

Code:
Hyyy  1129201202:33:13   
Dyyy65507                          
Tyyy000000000000144

It should not be like above .It should change only for header & trailer and also it is adding one extra space at the end.I don't want that space
# 10  
Old 08-03-2013
I have copied the CODE segments from your last post replacing each space character with a lowercase letter s as shown here:
Code:
HARss1129201202:33:13sss
D3065507sssssssssssssssssssssssssss
TAR000000000000144ssssss

and
Code:
Hyyyss1129201202:33:13sss
Dyyy65507ssssssssssssssssssssssssss
Tyyy000000000000144s

So, you will note that the output doesn't have an additional space character in the last line of your output; instead, it is missing five space characters. This change in the number of spaces at the end of the last line in your output was not caused by the sed command:
Code:
sed 's/\(.\{1\}\)../\1yyy/' Test123.txt

You must be doing something else that you haven't shown us that is making that change.
# 11  
Old 08-03-2013
Its actually my mistake when I did a copy ..

I don't know how I can make my requirement understand Smilie

Code:
HAR  1129201202:33:13   
D3065507                
TAR000000000000144

In the above example which I show you , I need to replace AR with YYY.
The below code will replace for all the three records, but I need to replace only for the first and last record .And also while replacing, it shouldn't add an extra space.It should remove one space and add that record Means Length should be only 24.
Note: -Its a fixed width file.All the records will have a length of 24
sed 's/\(.\{1\}\)../\1yyy/'
# 12  
Old 08-03-2013
Quote:
Originally Posted by ginrkf
...
Code:
HAR  1129201202:33:13   
D3065507                
TAR000000000000144

In the above example which I show you , I need to replace AR with YYY.
The below code will replace for all the three records, but I need to replace only for the first and last record .And also while replacing, it shouldn't add an extra space.It should remove one space and add that record Means Length should be only 24.
Note: -Its a fixed width file.All the records will have a length of 24
sed 's/\(.\{1\}\)../\1yyy/'
I created a file with your posted data. Two things to note here:
(1) I replaced all spaces by "*" so they are easily visible.
(2) The length of each line is 24. It wasn't so in your post, but I think that was due to copy/paste issues.

Here's my file:

Code:
$ 
$ cat f01
HAR**1129201202:33:13***
D3065507****************
TAR000000000000144******
$ 
$ # check length of each line
$ awk '{print length}' f01
24
24
24
$ 
$

Now you mention two things in your post -

(a) you want to replace all "AR"s by "yyy"
(b) you want to replace only first and last record (the head and tail record)

So, will "AR" be present only in the first and last record i.e. only in the head and tail record? Let's say it does. Then we can use "AR" to match the lines we want to change:

Code:
$ 
$ 
$ sed '/^.AR/{s/AR/yyy/; s/.$//}' f01
Hyyy**1129201202:33:13**
D3065507****************
Tyyy000000000000144*****
$ 
$

Here, "/^.AR/" means - "find out all lines that have any character at the beginning, followed by AR". This will work only on the first and last line. The second line will remain as it is.

The first operation "s/AR/yyy/" performs the required substitution.

The second operation "s/.$//" removes the last character in the line so that the length is constant. Note that it removes the last character - which could be a space or something else.
This User Gave Thanks to durden_tyler For This Post:
# 13  
Old 08-03-2013
Thanks for the reply
Actually I am getting below issue
Code:
sed: command garbled: /^.AR/{s/AR/yyy/; s/.$//}

Can you please look into this

Last edited by Scott; 08-03-2013 at 11:38 AM.. Reason: Code tags
# 14  
Old 08-03-2013
A semi-colon is missing:
Code:
$ sed '/^.AR/{s/AR/yyy/; s/.$//;}' f01

These 2 Users Gave Thanks to Scott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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 using awk on fixed width file.

All, I used to use following command to replace specific location in a fixed width file. Recently looks like my command stopped working as intended. We are on AIX unix. awk 'function repl(s,f,t,v) { return substr(s,1,f-1) sprintf("%-*s", t-f+1, v) substr(s,t+1) } NR<=10 {... (3 Replies)
Discussion started by: pinnacle
3 Replies

3. Shell Programming and Scripting

Print column details from fixed width file using awk command

hi, i have a fixed width file with multiple columns and need to print data using awk command. i use: awk -F "|" '($5 == BH) {print $1,$2,$3}' <non_AIM target>.txt for a delimiter file. but now i have a fixed width file like below: 7518 8269511BH 20141224951050N8262 11148 8269511BH... (5 Replies)
Discussion started by: kcdg859
5 Replies

4. UNIX for Dummies Questions & Answers

Replace the unexpected newline char with space in a Fixed width file

Input eg: Ouput Expected. The #rd line had the unexpted new line, which need to be replaced with space. I was planing to go with checking the length of each line using awk and if the length is less than the defeined limit, (12 in above case) will replace the newline with space. ... (5 Replies)
Discussion started by: deepakwins
5 Replies

5. Shell Programming and Scripting

How to split a fixed width text file into several ones based on a column value?

Hi, I have a fixed width text file without any header row. One of the columns contains a date in YYYYMMDD format. If the original file contains 3 dates, I want my shell script to split the file into 3 small files with data for each date. I am a newbie and need help doing this. (14 Replies)
Discussion started by: bhanja_trinanja
14 Replies

6. UNIX for Dummies Questions & Answers

Remove duplicates based on a column in fixed width file

Hi, How to output the duplicate record to another file. We say the record is duplicate based on a column whose position is from 2 and its length is 11 characters. The file is a fixed width file. ex of Record: DTYU12333567opert tjhi kkklTRG9012 The data in bold is the key on which... (1 Reply)
Discussion started by: Qwerty123
1 Replies

7. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

8. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

9. Shell Programming and Scripting

Comparing column of variable length anf fixed width file

Hi, I have two input files. File1: ID Name Place 1-234~name1~Newyork 1-34~name2~Boston 1-2345~name3~Hungary File1 is a variable length file where each column is seperated by delimitter "~". File2: ID Country 1-34<<11 SPACES>>USA<<7 spaces>> 1-234<<10 SPACES>>UK<<8... (5 Replies)
Discussion started by: manneni prakash
5 Replies
Login or Register to Ask a Question