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
Deleting lines in text file hern14 UNIX for Dummies Questions & Answers 5 05-16-2008 03:03 PM
Pulling multiple lines of text DumDum Shell Programming and Scripting 2 03-10-2008 12:15 PM
Deleting Lines from .CSV Files 009satya Shell Programming and Scripting 1 11-13-2006 03:30 PM
deleting multiple files through ftp mgirinath Shell Programming and Scripting 4 04-18-2006 02:44 PM
Deleting Multiple Lines with sed rambo15 Shell Programming and Scripting 1 02-16-2002 06:19 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-25-2008
vrms vrms is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 16
deleting lines from multiple text files

I have a directory full of text data files.

Unfortunately I need to get rid of the 7th and 8th line from them all so that I can input them into a GIS application.

I've used an awk script to do one at a time but due to the sheer number of files I need some kind of loop mechanism to automate it.

The awk script used: -
awk 'BEGIN{getline f;getline t}FNR==f,FNR==t{next}1' numbers.txt inputfile > outputfile

where numbers.txt is merely a document with the numbers 7 and 8

My guess is that I need a way of piping the output from ls (of the directory) into where the inputfile is situated and a counter to loop through till the end.

Any suggestions will be welcome (an awk suggestion would be preferable to pearl)

Thanks all
  #2 (permalink)  
Old 04-25-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,749
sed or awk are good choices - here is sed:
Code:
for file in  `ls *.txtdatafile`
do
      sed '7,8d' $file > tmp.tmp
      mv tmp.tmp $file
done
  #3 (permalink)  
Old 04-25-2008
vrms vrms is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 16
Quote:
Originally Posted by jim mcnamara View Post
sed or awk are good choices - here is sed:
Code:
for file in  `ls *.txtdatafile`
do
      sed '7,8d' $file > tmp.tmp
      mv tmp.tmp $file
done
Thanks for the Reply Jim

Unfortunately this sends them all into one big tmp.file. I need to create a new text file for every single text file that is in the directory.
  #4 (permalink)  
Old 04-25-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Gack, please don't post the same question multiple times. I already replied once.
deleting specific lines from all files in a directory

The second line in Jim's script (and mine, too) moves the tmp file back over the original file. You can't easily redirect back onto a file because the redirection happens before awk gets to read it, so the file ends up being empty. There are various ways to avoid using temporary files but in this particular case it's probably not worth the hassle.

The ls in backticks is an antipattern; see An example of dealing with file names with spaces in them for a discussion of the drawbacks. Simply use "for file in *.txtdatafile" instead.
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 08:55 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