Help with file editing while keeping file format intact


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with file editing while keeping file format intact
# 1  
Old 12-20-2010
Help with file editing while keeping file format intact

Hi,

I am having a file which is fix length and comma seperated. And I want to replace values for one column.
I am reading file line by line in variable $LINE and then replacing the string.
Problem is after changing value and writing new file temp5.txt, formating of original file is getting lost. It is removing all the blank spaces.

Sample value: Original File
Code:
LINE="000001077,03190, ,                                          1 Main Street,                                                       ,                                                       ,                                                       ,     Foxboro                            ,           02035,1,MA,12-13-2010 22:43:42,12-13-2010 22:43:42"

I want to replace MA with 00021

I am reading file line by line in variable $LINE and then replacing the string
Code:
echo $LINE | sed "s/",MA,"/",00021,"/g" >>temp5.txt

temp5.txt:
Code:
000001077,03190, , 1 Main Street, , , , Foxboro , 02035,1,00021,12-13-2010 22:43:42,12-13-2010 22:43:42

Please help.

Thanks

Last edited by Franklin52; 12-21-2010 at 08:46 AM.. Reason: Please use code tags, thank you
# 2  
Old 12-20-2010
U may try:
Code:
echo $LINE | sed 's/,MA,/,00021,/g' >>temp5.txt

Quote:
Problem is after changing value and writing new file temp5.txt, formating of original file is getting lost. It is removing all the blank spaces.
But I don't see where is the problem. How original file is being affected.
# 3  
Old 12-20-2010
The problem is in echo $LINE. You need to use:
Code:
echo "$LINE"

to keep spacing intact.

But: you do not need to read line by line as sed can do all that in one go (sed '...' file)

Last edited by Scrutinizer; 12-20-2010 at 06:46 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-20-2010
Thanks for the reply.

Sorry my mistake.

Original file looks like:
Code:
000001077,03190, ,                                          1 Main Street,                                                       ,                                                       ,                                                       ,     Foxboro                            ,           02035,1,MA,12-13-2010 22:43:42,12-13-2010 22:43:42

Its a fix length file. So having number of blank spaces around the column. But after I use 'sed', it is removing all blank spaces.

---------- Post updated at 04:45 PM ---------- Previous update was at 04:39 PM ----------

Hi

I tried with quotes. But it is still not working.
Code:
echo "$LINE" | sed "s/",$SERVICE_CODE,"/",$SERVICE_CODE_NUM,"/g" >>temp5.txt

simple echo "$LINE" keeps the spaces. But with sed it's not working.

Last edited by Franklin52; 12-21-2010 at 08:47 AM.. Reason: Please use code tags, thank you
# 5  
Old 12-20-2010
Check if this helps.
Code:
echo "$LINE" | sed "s/,$SERVICE_CODE,/,$SERVICE_CODE_NUM,/g" >>temp5.txt

OR just (if suitable)
Code:
echo "$LINE" | sed "s/$SERVICE_CODE/$SERVICE_CODE_NUM/g" >>temp5.txt

If not, how is the output, any error? Pls post the result.
Also as Scrutinizer told, U can process whole file in one go, no need to read file line by line
Code:
sed "s/$SERVICE_CODE/$SERVICE_CODE_NUM/g" inputFile >temp5.txt

# 6  
Old 12-20-2010
Code:
echo "$LINE" | sed "s/,$SERVICE_CODE,/,$SERVICE_CODE_NUM,/g" >>temp5.txt
 
echo "$LINE" | sed "s/$SERVICE_CODE/$SERVICE_CODE_NUM/g" >>temp5.txt

Both is giving same output as before, removing all space. It does not give any error.

-----------------------------
Code:
sed "s/$SERVICE_CODE/$SERVICE_CODE_NUM/g" inputFile >temp5.txt

-----------------------------
works fine. but i am not sure if i can use it.
Because I need to do some validations on each line before updating it.

Last edited by Franklin52; 12-21-2010 at 08:48 AM.. Reason: Please use code tags, thank you
# 7  
Old 12-20-2010
How are you reading lines in LINE variable, Pls post that peice of code.
Also some sort of validations can be done in sed too (but all kind of validations).
U may post the validations to be done in lines to see if sed can do that or not.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Keeping record of file 2 based on a reference file 1 in awk

I have 2 input files (tab separated): file1: make_A 1990 foo bar make_B 2010 this that make_C 2004 these those file2: make_X 1970 1995 ref_1:43 ref_2:65 make_A 1970 1995 ref_1:4 ref_2:21 ref_3:18 make_A 1980 2002 ref_1:7 ref_2:7 ref_3:0 ... (2 Replies)
Discussion started by: beca123456
2 Replies

2. Shell Programming and Scripting

Remove duplicates by keeping the order intact

Hello friends, I have a file with duplicate lines. I could eliminate duplicate lines by running sort <file> |uniq >uniq_file and it works fine BUT it changes the order of the entries as it we did "sort". I need to remove duplicates and also need to keep the order/sequence of entries. I... (1 Reply)
Discussion started by: magnus29
1 Replies

3. Shell Programming and Scripting

Grab data between 2 keywords any do an array operation and write the file intact

Hi Unix Gurus, I need to grep for a block that is between a start and end keyword and then in between I need to find and replace a keyword. for eg: I need to search between Test = 000; and Test = 000; and find K9 and replace with M9 INPUT FILE Define { Replace = K9; Test =... (6 Replies)
Discussion started by: naveen@
6 Replies

4. Shell Programming and Scripting

Remove last few characters in a file but keeping Header and trailer intact

Hi All, I am trying write a simple command using AWK and SED to this but without any success. Here is what I am using: head -1 test1.txt>test2.txt|sed '1d;$d' test1.txt|awk '{print substr($0,0,(length($0)-2))}' >>test2.txt|tail -1 test1.txt>>test2.txt Input: Header 1234567 abcdefgh... (2 Replies)
Discussion started by: nvuradi
2 Replies

5. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

6. Shell Programming and Scripting

Keeping the number intact

Currently I have the following to separate the numeric values. However the decimal point get separated. ls -lrt *smp*.cmd | awk '{print $NF}' | sed 's/^.*\///' | sed 's/\(*\)/ & /g' As an example on the files n02-z30-dsr65-terr0.50-dc0.05-4x3smp.cmd... (8 Replies)
Discussion started by: kristinu
8 Replies

7. UNIX for Dummies Questions & Answers

sort by keeping the headings intact?

Hi all, I have a file with 3 columns separated by space. Each column has a heading. I want to sort according to the values in the 2nd column (ascending order). Ex. Name rank direction goory 0.05 --+ laby 0.0006 --- namy 0.31 -+- ....etc. Output should be Name rank direction laby... (3 Replies)
Discussion started by: Unilearn
3 Replies

8. HP-UX

Editing a UNIX file in Hexadecimal format

Hi, I am a newbie to the UNIX world. I am asked to edit the file in hexadecimal format and save it. Later I should be able to print the file in char mode. please anyone tell me how to do that mostly using VI editor. (1 Reply)
Discussion started by: vkudire
1 Replies

9. UNIX for Dummies Questions & Answers

creating separate directories according to file extension and keeping file in different directory as

unix program to which a directory name will be passed as parameter. This directory will contain files with various extensions. This script will create directories with the names of the extention of the files and then put the files in the corresponding folder. All files which do not have any... (2 Replies)
Discussion started by: Deekay.p
2 Replies

10. Shell Programming and Scripting

Keeping the format ...

Hi all, Am trying to execute a loop but having some troubles... Files that will be query'd use the Julian date (eg: cpu032, cpu365) in their naming convention. I'm a little lost how to maintain the three character format of the numeric portion of the file name while cycling backwards(or... (13 Replies)
Discussion started by: Cameron
13 Replies
Login or Register to Ask a Question

Featured Tech Videos