The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Delete line in text file Berserk UNIX for Dummies Questions & Answers 6 12-16-2008 03:44 PM
How to delete a particular text without opening the file. stevefox Shell Programming and Scripting 12 02-21-2008 01:24 PM
Delete first line from any text file ? aungomarin Shell Programming and Scripting 5 05-16-2006 09:42 PM
how to delete away text in a file? forevercalz Shell Programming and Scripting 5 01-08-2006 10:28 PM
delete last line from text file hcclnoodles Shell Programming and Scripting 4 06-25-2002 09:52 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-14-2007
33junaid 33junaid is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 27
Overwrite & Delete in Text File

Dear All,

I have text file like this:

Header
Record 1
Record 2
.......
Record n
Tail

This line of code :
awk '{ if ( NR == 1 ) { head=substr($0,1,300);} else { last = substr($0,1,300);}END{printf "Header is : %-300s Trailer is : %-300s\n", head, last}' filename

converted Header and Trailer in one line and within 300 character each.The output is just one line.

Now I want to overwrite this one line above (Header+ Tail) over the first line of original file (orginal header) and delete the Tail........i can copy this header and tail to new file and then append Record 1 to Record n to this new file, but that will cause too low performance........so I think better to update first and the last line of the this file.................so how can i do that?

i.e. 1-update the header with new line i.e. (header+trailer)
2-delete the tail in original file (may be using cut tail -1 filename?)
  #2 (permalink)  
Old 08-14-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
Dont bump up your issue by opening a new thread. You can address it in your old thread

Header and Footer
  #3 (permalink)  
Old 08-14-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
Save the output from the awk into a variable like

Code:
New_head=$(awk '{ if ( NR == 1 ) { head=substr($0,1,300);} else { last = substr($0,1,300);}END{printf "Header is : %-300s Trailer is : %-300s\n", head, last}' filename )
And then you can do something like

Code:
sed -e "1 c\ $New_head" -e '$d' filename
  #4 (permalink)  
Old 08-14-2007
33junaid 33junaid is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 27
Thanks.....I tried the code but sed command is not working properly,it is giving output:

sed: command garbled: 1 c\ Header is: Headervalue.......Trailer is: Trailer value.....


It makes no change in the file as well.
  #5 (permalink)  
Old 08-14-2007
33junaid 33junaid is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 27
any luck for me as well?
  #6 (permalink)  
Old 08-14-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,932
There has been another thread as lorcan said and this seems to be only a continuation of the above thread in a new post.

Please continue to post your question in the same thread so that it would be easier to follow up rather than starting a new thread for the same question.

Assuming I have understood your requirement try this,

Code:
awk ' { if ( NR == 1 ) { head=$0 } else { last = $0 } arr[i++]=$0; }END{ printf "Header is : %s Trailer is : %s\n", head, last; for( x=0; x<i-1; x++) { print arr[x] } }' filename
Code:
Header is : header Trailer is : trailer
header
record 1
record 2
record 3
combined header and trailer is available followed by that
header is available
all the records are displayed
trailer record is not displayed

If this is not what you had asked for, let us know
  #7 (permalink)  
Old 08-15-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
To print the Header and trailer in one line with 300 char each. Removal of header and trailer line. Use the below

Just a flavour to matrixmadhan's command as per your req

Code:
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] } }' filename
Sponsored Links
Closed Thread

Bookmarks

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 On



All times are GMT -4. The time now is 03:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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