Text parsing question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Text parsing question
# 1  
Old 03-23-2002
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  
Old 03-23-2002
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.
thehoghunter
# 3  
Old 03-23-2002
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  
Old 03-24-2002
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

Jimbo
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Text parsing

Hi All! Is it possible to convert text file: to: ? (6 Replies)
Discussion started by: y77
6 Replies

2. Shell Programming and Scripting

Another parsing question (awk)

Input File Name of the session: filesrv_quo snap Logical Units UID: 60:06:01:60:01:7B:25:00:C8:86:B0:CA:5B:A2:E0:11 Name of the session: verspn2_at_176_0218 snap Logical Units UID: Name of the session: DRT-ny-iadsql1-c_ny-iadsql2-c snap Logical Units UID: ... (4 Replies)
Discussion started by: greycells
4 Replies

3. Shell Programming and Scripting

another parsing question

Input File Information about each HBA: HBA UID: 20:00:00:00:C9:9A:62:88:10:00:00:00:C9:9A:62:88 Server Name: 20:00:00:00:C9:9A:62:88:10:00:00:00:C9:9A:62:88 Server IP Address: UNKNOWN HBA Model Description: HBA Vendor Description: HBA Device Driver... (2 Replies)
Discussion started by: greycells
2 Replies

4. Shell Programming and Scripting

Help with text/number parsing

Hello I have a file that contains 10 rows as below: "ID" "DP" "ID=GRMZM2G015073_T01" "23.6044288292005" "ID=GRMZM2G119852_T01" "59.7782287606723" "ID=GRMZM2G100242_T02" "61.4167813736184" "ID=GRMZM2G046274_T01" "6.63061838134219" "ID=GRMZM2G046274_T02" ... (5 Replies)
Discussion started by: cs_novice
5 Replies

5. Shell Programming and Scripting

Question about reading and parsing text file

Hello, I'm just getting started with BASH programming. I would like to write a script to solve a file renaming problem I have. I received a directory containing a collection (>2000) of files whose names are in DOS 8.3 format, and woild like to rename the filenames to a longer and more... (8 Replies)
Discussion started by: polomora
8 Replies

6. Shell Programming and Scripting

Perl parsing question

I need some help loading an array. I have two unique delimiters, but I keep running into recursion. #!/usr/bin/perl $INFILE="/root/scripts/data.txt"; $pat1="SCRIPT####"; $pat2="SCRIPT#echo"; $flag=0; $inc=0; $chunk=""; open(INFILE,"<$INFILE")|| die; while(<INFILE>) { if... (2 Replies)
Discussion started by: s_becker
2 Replies

7. Shell Programming and Scripting

Parsing text

Hello all, I have some text formatted as follows Name: John doe Company: Address 1: 7 times the headache Address 2: City: my city State/Province: confusion Zip/Postalcode: 12345 and I'm trying to figure out how I could extract the data after the colon so that the result would be ... (6 Replies)
Discussion started by: mcgrailm
6 Replies

8. Shell Programming and Scripting

Parsing question.

Hi There, First time poster here. I've got a parsing question and have a solution but am sure there is a much better way of doing this with just awk. My knowledge of awk is pretty limited so hope someone out there can give me a better solution. Here's the problem, I'm receiving a file from a... (2 Replies)
Discussion started by: little_happosai
2 Replies

9. Shell Programming and Scripting

text parsing querry

$ A=/t1/bin/f410pdb oIFS=$IFS IFS=/ $ set -- $A $ IFS=$oIFS $ echo $2 t1 $ echo $3 bin $ echo $4 f410pdb can any one please explain me what is done with IFS and how it is working internally ...i am interested to know in depth (2 Replies)
Discussion started by: mobydick
2 Replies

10. Shell Programming and Scripting

Parsing question

Hi Guys, I was wondering if you could help me out - I have a directory /home/users/datafiles/ which contain files "dat dd-mm-yy.xls" I am trying to write a script which does the following - (1) loops through all the files (2) retrieves the dd-mm-yy string and converts it into a... (12 Replies)
Discussion started by: muser
12 Replies
Login or Register to Ask a Question