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 a line from a file rohini UNIX for Dummies Questions & Answers 8 01-07-2009 06:34 AM
How to delete line from a file ramezernest UNIX for Dummies Questions & Answers 0 11-01-2007 11:56 AM
delete line in file with sed forever_49ers Shell Programming and Scripting 8 09-12-2007 08:39 AM
delete line in a file dta4316 Shell Programming and Scripting 12 05-18-2005 10:20 AM
Delete line(s) from file elchalateco UNIX for Dummies Questions & Answers 4 10-11-2002 01:25 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-17-2007
dfb500 dfb500 is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 14
Delete first line from file and more....

Hello, I have to deal with several files that will be named something like this:
E00001.TXT, E00002.TXT etc.
Each file will have a alpha character on the first position of the first line which I want to place in a variable, then delete the entire line leaving the remainder of text. This new file I want to be named the original filename prefixed by the character I previously extracted so the new file might be RE00001.TXT. Usually, out of habit I call tcl scripts to do file manipulation but I wanted to know if there was a fairly starighforward way to do this all in my Unix script (ksh). I don't know much about sed or awk so if you give examples with that and don't mind providing a brief description of what does what that would be helpful. Could the grep command provide all the functionality I'm asking for?

TIA

D
  #2 (permalink)  
Old 05-17-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
dfb,
See if this works for you:
Code:
for mFile in E?????.TXT
do
  mFirstChar=`head -1 $mFile | cut -c1`
  mNewFile=$mFirstChar$mFile
  echo "new file = "$mNewFile
  mv $mFile $mNewFile
done
  #3 (permalink)  
Old 05-17-2007
dfb500 dfb500 is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 14
Shell, I like it, very simple, I don't think it addresses the issue of my new file containing everything except the first line though, does it? How can I get the newfile to exclude the first line from the source file, is that the cut command again?

Thx again
  #4 (permalink)  
Old 05-17-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Here it is removing the first line:
Code:
for mFile in E?????.TXT
do
  mFirstChar=`head -1 $mFile | cut -c1`
  mNewFile=$mFirstChar$mFile
  echo "new file = "$mNewFile
  sed '1d' $mFile > $mNewFile
done
  #5 (permalink)  
Old 05-17-2007
dfb500 dfb500 is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 14
Shell, I'll try it, my thanks!!!
  #6 (permalink)  
Old 05-17-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
Code:
awk '
FNR==1 { ch=substr($0,1,1)} #get first character from first line
FNR>1 { # get 2nd line onwards
        newfile=ch FILENAME
	print > newfile #save 2nd line onwards to newfile.         
      }
' E*.TXT
Reply

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 05:22 AM.


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