The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parsing text from file ndnkyd Shell Programming and Scripting 0 04-02-2008 02:42 AM
Need help in parsing text file contents Alecs Shell Programming and Scripting 0 03-30-2008 01:58 PM
Text File Parsing Djlethal Shell Programming and Scripting 2 02-27-2008 03:31 AM
Parsing comma delimited text file chengwei Shell Programming and Scripting 5 02-23-2007 05:38 AM
Parsing Lines from a text file sallender Shell Programming and Scripting 3 08-26-2005 12:11 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 03-23-2002
98_1LE 98_1LE is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2000
Location: Greater Dallas area
Posts: 405
Text parsing question

How would I split a file based on the location of a string, basically I want all entries above the string unix in this example

1
2
3
4
unix
5
6
7

Thanks,
Chuck
  #2 (permalink)  
Old 03-23-2002
thehoghunter
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
One way....I was nice and didn't put it in my standard csh...

#!/bin/ksh
file="file1"
while read -r xx
do
if [ "$xx" = "unix" ]
then
file="file2";
else
echo "$xx" >> $file ;
fi
done < $1

Original file has what you mentioned.

File1 ends up with
1
2
3
4
File2 ends up with
5
6
7
8

If you want the 'unix' in one of the files, just move the echo or add another.
  #3 (permalink)  
Old 03-23-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
I finally got this to work with just sed...
Code:
#! /usr/bin/sed -nf
s/unix/unix/
t loop
w file1
d
: loop
n
w file2
b loop

The script does work and so I thought that I might as well post it. I always have a tough time with these sed scripts though. I kinda fiddle with them until they work. It is very fast...it would take a custom c program to beat it. But it's hard to understand and maintain. So I actually prefer thehoghunter's solution.

To put the unix line in the first file, move the "w file1" up to be the first line in the script. To put that line in the second file, reverse the "n" and "w file2" lines.
  #4 (permalink)  
Old 03-24-2002
Jimbo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
This awk exits when it sees the unix line. To include the unix line into the file, put the print command first. If you want the unix grep string anchored to beginning-of-line, use /^unix/.

Code:
awk '/unix/{exit};{print}' myfile > file1

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 05:42 PM.


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