LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-17-2003, 09:27 PM   #1
Gantrep
Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 78

Rep: Reputation: 15
text processing


How would I go about removing the first 7 characters and the last eight characters of every line of a file (or text stream). I've read man pages for cut, head, tail, and none seem to do exactly what I want. The stuff in between those characters I want removed is of variable length, otherwise I think cut, head, tail would do the job.

Thanks in advance.
 
Old 02-17-2003, 09:38 PM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Write a C program. It would be just a couple of lines.
 
Old 02-17-2003, 09:57 PM   #3
Wolven
Member
 
Registered: Jan 2003
Posts: 195

Rep: Reputation: 30
You would use sed. It's a "Stream EDitor."

Here is an example that would cut away the first and last 3 lines of text. If you can't do it, I'll write you the script that will rip the letters you want.



sed -ne '1{n;n;n;N;N;}' -e '$d;N;P;D'



Here's a link to a sed tutorial for you. It has links to other tutorials as well.

http://www.selectorweb.com/sed_tutorial.html

Edit:
One more thing. If the words are all exactly the same, for example, if you had the numbers 1234567 at the beginning and end of every file, you could just run


sed s/"1234567"//g FILENAME



and it would replace "1234567" with whatever is between the slashes. In this case, nothing is between the slashes, so it replaces it with nothing. The same as deleting it.

Last edited by Wolven; 02-17-2003 at 10:03 PM.
 
Old 02-17-2003, 10:15 PM   #4
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Here is quick and dirty:
Didn't know if you wanted to count newlines??
Make sure you test it, i'm sure it has bugs.

Code:
#include <stdio.h>
#include <unistd.h>

#define BUFLEN 2048
#define STRIPBEG 7
#define STRIPEND 9  /* Adding one additional for newline */

main()
{
   char szInString[ BUFLEN  + 1];
   int iStrLen = 0;
   int iNewEnd = 0;

   while ( fgets( szInString, BUFLEN, stdin   ) != NULL )
   {
      iStrLen = strlen( szInString );
      if ( iStrLen > ( STRIPBEG + STRIPEND ))
      {
         iNewEnd = iStrLen - STRIPEND;
         szInString[iNewEnd] = (char)NULL;
         printf ("%s\n", szInString + STRIPBEG );
      }
   }

}
 
Old 02-17-2003, 10:37 PM   #5
Gantrep
Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 78

Original Poster
Rep: Reputation: 15
Hey thanks folks. Sed was what I was looking for I guess.

I've actually had some C++ experience, but I was trying to stay away from that. I may use your code anyway though crabboy. I am grateful that you posted it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch Processing ankan Linux - Software 3 10-11-2013 10:52 AM
Data Processing joelhop Linux - General 8 01-01-2006 08:08 PM
text processing, maintaining a log jonhewer Linux - Newbie 9 08-25-2005 02:24 AM
input text file processing the gnu way? zero79 Programming 3 03-04-2005 07:41 PM
text editingor processing exodist Linux - General 2 11-29-2003 10:24 PM


All times are GMT -5. The time now is 12:21 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration