How to process from middle of a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to process from middle of a file?
# 1  
Old 01-02-2008
How to process from middle of a file?

Hi There.

I have a file like this .

a nnnn sds
b ssss fdefd
c sdfd dsfd sdfds
...
...
Summary
t1 t2 t3 t4
s1 s2 s3 s4
f1 f2 f3 f4
..

How to use awk to begin process this file from the line that contains "Summary"?

Thanks!
louis
# 2  
Old 01-02-2008
Can you show us how far you've gotten so far?

Regards
# 3  
Old 01-02-2008
Data work aroud.

At present. i have no idea how to do it in awk.
the only way i can think is that .
read line by line , skip all the line until reach the line that having "summary",
then begin to print until end of the file.

But want to know how to use awk to do it. as big file. it seems like
awk run faster then the read file line by line.

Thanks!
# 4  
Old 01-02-2008
Code:
a=`wc -l bigfile.txt | awk '{print $1}'` ; awk -v len=$a 'BEGIN {print len / 2} NR >= (len / 2)  {print}' bigfile.txt

# 5  
Old 01-02-2008
How about this?

Code:
awk '/^Summary/,/*/' data

# 6  
Old 01-02-2008
I got syntax error with both solutions:

Code:
$ cat file
a nnnn sds
b ssss fdefd
c sdfd dsfd sdfds
Summary
t1 t2 t3 t4
s1 s2 s3 s4
f1 f2 f3 f4
$
$ a=`wc -l file | awk '{print $1}'` ; awk -v len=$a 'BEGIN {print len / 2} NR >= (len / 2)  {print}' file
awk: syntax error near line 1
awk: bailing out near line 1
$
$ awk '/^Summary/,/*/' file
awk: syntax error near line 1
awk: bailing out near line 1

Of course, I'm using SunOS so most probably due to awk version/kind - can be easily changed to nawk etc. to fix the issue.

However, how about this:

Code:
$ sed -ne '/Summary/,$p' file
Summary
t1 t2 t3 t4
s1 s2 s3 s4
f1 f2 f3 f4

I think this will be most efficient (if the file is very big) and should work on most unices.

HTH
# 7  
Old 01-02-2008
rikxik - your right. I tested on a linux box. I was able to get it to work with awk/nawk on Solaris by escaping the *. I would be interested in the time/timex reports between sed and awk when run on a large file

Code:
awk '/^Summary/,/\*/' data

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Subsituting contents of entire file to middle of another file using awk

Hi I have a data file 'File2' consisting of 105670 lines. I want to copy and paste 17928 lines from 'File1' to 'File2' but I want to place it in between lines 21 and 17950 of 'File2'. How do I do it in awk? For example- File A has 5 lines X Y 1 2 3 4 5 6 7 8 9 and File B has A b... (1 Reply)
Discussion started by: ananyob
1 Replies

2. Shell Programming and Scripting

Subsituting contents of entire file to middle of another file using awk

Hi I have a data file 'File2' consisting of 105670 lines. I want to copy and paste 17928 lines from 'File1' to 'File2' but I want to place it in between lines 21 and 17950 of 'File2'. How do I do it in awk? For example- File A has 5 lines X Y 1 2 3 4 5 6 7 8 9 and File B has A b... (1 Reply)
Discussion started by: ananyob
1 Replies

3. Shell Programming and Scripting

How to read from middle of a file for a particular text

Hi, Below is my issue which I desperately need and I want a shell script which can do this job. 1. There are 10 log files in a particular location. 2. open each log file. Goto to the end of the file. From the end go up to find a particular text. From this particular text till the end of... (3 Replies)
Discussion started by: kashriram
3 Replies

4. UNIX for Dummies Questions & Answers

Add string to middle of a file

Hi, I want to write a script that takes a file and a string as params and adds the string to the middle line of the file. Also, I want to output the results back to the original file passed without using temp files. I am very much new to UNIX so this is all a little like black magic to me at... (15 Replies)
Discussion started by: Chiefos
15 Replies

5. UNIX for Dummies Questions & Answers

How to insert text in the middle of a file

Hey guys, how do we take a line of text as an argument from a user and then insert it in the middle of a file irrespective of the number of lines in the file. I am trying to do this without SED or AWK. Inserting it in the beginning and at the end is easy, but i am trying to accomplish inserting... (6 Replies)
Discussion started by: kartikkumar84@g
6 Replies

6. Shell Programming and Scripting

add text in the middle of file

Can anyone help me pls? I want to add a text into the middle of file. I've writtenthe following script text to add="$1" file="$2" lines=$(wc -l $2) half_lines=$(expr $lines / 2) head -$half_lines $2 > temp echo "text to add" >> temp ((half_lines=$half_lines + 1)) tail -$half_lines $2... (6 Replies)
Discussion started by: relle
6 Replies

7. Shell Programming and Scripting

removing whitespace from middle of file -help

I have a file in which I clean out a bunch of nonsense text as well as path information. What I end up with is something like the following: johnson.........................................................933 Where the periods represent the whitespace The file comes out originally with... (2 Replies)
Discussion started by: roninuta
2 Replies

8. Shell Programming and Scripting

add a string in the middle of the file

i want to add a string in a very top of a file without using VI or SED or AWK this is what ive done: (echo '0a'; echo 'LINE OF TEXT'; echo '.'; echo 'wq') | ed -s myfile to add astrng right in the middle i could have count the lines of the file and just chenge the address. ... (6 Replies)
Discussion started by: ciroredz
6 Replies

9. Shell Programming and Scripting

How to insert text into first line of the file and middle of the file?

Script 1 Pre-requisites Create a file with x amount of lines in it, the content of your choice. Write a script that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the very top (the... (3 Replies)
Discussion started by: ali hussain
3 Replies

10. Shell Programming and Scripting

appending to sed output of one file into the middle of file

hi, i have a file file1 file2 ----------- ----------------- aa bbb ccc 111 1111 1111 ddd eee fff 222 3333 4444 ggg hhh... (5 Replies)
Discussion started by: go4desperado
5 Replies
Login or Register to Ask a Question