Overwrite & Delete in Text File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Overwrite & Delete in Text File
# 8  
Old 08-15-2007
thanks for the reply......the code is working fine.....ur code is :

awk ' { if ( NR == 1 ) { head=substr($0,1,300);} else { last = substr($0,1,300);} arr[i++]=$0; }END{ printf "Header is : %-300s Trailer is : %-300s\n", head, last; for( x=1; x<i-1; x++) { print arr[x] } }' old_file > new_file


The problem remains still the same ......for example if there are 1000 rows, one header and one trailer.......it concatenates hearder and trailer in one line and then copies this along with other 1000 rows to new file.......thus cuasing a lot of I/O........i have millions of records in the file and cannot afford to transfer one million records just for the sake of 2 lines (i.e. header and trailer)................

Before this, i got this code:

awk ' { if ( NR == 1 ) { head=$0 } else { last = $0 } }END{ printf "%-300s %-300s\n", head, last }' old_file > new_file
sed '1d;$d' old_file >> new_file

the above code has the same problem that it appends all the data rows................pls note that output is coming fine in both cases but I am concerned about the performance................all we need to do is:

1-Get header in old file (say string1)
2-Get trailer in old fine (say string2)
3-concatenate string1 and string2 (say the result is string3)
4-update the header in old file with string3
5-delete the trailer in old file.

Thanks....
# 9  
Old 08-15-2007
First command with just awk logically should take lesser time than the second one with both awk and sed as the file is parsed twice in the latter case.
# 10  
Old 08-16-2007
ok that is fine but I was just concerned about the number of files created........there is one problem that I am getting one extra line (blank line) at the end of file...........why is so using this code:


awk ' { if ( NR == 1 ) { head=substr($0,1,149);} else { last = substr($0,1,149);} arr[i++]=$0; }END{ printf "%-149s %-149s\n", head, last; for( x=1; x<i-1; x++) { print arr[x] } }' file1>file2


When I open file2,I get one extra line.......?
# 11  
Old 08-16-2007
This works fine for me.
is there any blank space between trailer and detailed record ?
# 12  
Old 08-16-2007
The output I get is like this:


Header (plus) Trailer
Record1
Recrod2
...
...
Record n
(blank line here)

Everything is perfect except that why this blank line is appearing when we have removed the trailer in original file?I want the last line Record n.

The original file was like this:

Header
Record1
Recrod2
...
...
Record n
Trailer
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. Shell Programming and Scripting

Need Script to ZIP/SAVE & then DELETE Log file & send a mail conformation for any error

ENVIROMENT Linux: RHEL 6.4 Log Path: /usr/iplanet/servers/https-company/logs Log Format: user.log.03-15-2015 I have log4j log rotation enabled rotating files on a daily basis. The rotated logs are NOT compressed & are taking up too much space. I need a script that will run daily that... (1 Reply)
Discussion started by: admin_job_admin
1 Replies

3. UNIX for Dummies Questions & Answers

Delete records based on a text file from a text file

Hi Folks, I am a novice and need to build a script in bash. I have 2 text files data.txt file is big file, column 2 is the we need to search and delete in the output. The filter file contains the rows to be deleted. Data.txt state city zone Alabama Huntsville 4 California SanDiego 3... (3 Replies)
Discussion started by: tech_frk
3 Replies

4. Shell Programming and Scripting

Script to overwrite & before that keep copy a file on many servers

I have ssh password less auth enable & script does the job well as well #/bin/bash for i in `cat ip` do scp /etc/resolv.conf root@$ip done But I need to take backup of the file i will overwrite .. is there any simple way ? Kindly respond (5 Replies)
Discussion started by: heman96
5 Replies

5. Shell Programming and Scripting

How to delete lines of a text file based on another text file?

I have 2 TXT files with with 8 columns in them(tab separated). First file has 2000 entries whereas 2nd file has 300 entries. The first file has ALL the lines of second file. Now I need to remove those 300 lines (which are in both files) from first file so that first file's line count become... (2 Replies)
Discussion started by: prvnrk
2 Replies

6. Shell Programming and Scripting

Better to Delete or Overwrite

Hello All, I had just a question about my Bash Script I'm currently writing. The script I have writes some text to a output file. After I write to the output file I send the file to another server to do some stuff with it. After the file sends in the script, I don't need the output/txt... (4 Replies)
Discussion started by: mrm5102
4 Replies

7. Red Hat

Need Script to ZIP/SAVE & then DELETE Log file & DELETE ZIPS older than 12 months

ENVIROMENT Linux: Fedora Core release 1 (Yarrow) iPlanet: iPlanet-WebServer-Enterprise/6.0SP1 Log Path: /usr/iplanet/servers/https-company/logs I have iPlanet log rotation enabled rotating files on a daily basis. The rotated logs are NOT compressed & are taking up too much space. I... (7 Replies)
Discussion started by: zachs
7 Replies

8. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

9. Shell Programming and Scripting

Unable to overwrite but can delete file

I'm debugging a ksh script written by someone else that does the following: It runs a command and redirects stdout to a file called dberror that already exists using ">". This command fails with the following error: The file access permissions do not allow the specified action. dberror:... (1 Reply)
Discussion started by: savage66
1 Replies

10. Shell Programming and Scripting

Advanced Search & Delete Text File

I have a file in which email messages are stored in. Every email is separated by by a ^Z character (Control-Z). I need to extract all emails after the 65,00th one to another file and delete them from the original file. Any suggests on accomplishing this? (2 Replies)
Discussion started by: maxcell
2 Replies
Login or Register to Ask a Question