3

I have a bunch of files in the following format.

A.txt:

some text1      
more text2    
XXX
more text  
....  
XXX
.  
.  
XXX 
still more text  
text again  

Each file has at least 3 lines that start with XXX. Now, for each file A.txt I want to write all the lines till the 3rd occurrence of XXX (in the above example it is till the line before still more text) to file A_modified.txt.

I want to do this in bash and came up with grep -n -m 3 -w "^XXX$" * | cut -d: -f2 to get the corresponding line number in each file.

Is is possible to use head along with these line numbers to generate the required output?

PS: I know a simple python script would do the job but I am trying to do in this bash for no specific reason.

Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
Graddy
  • 2,828
  • 5
  • 19
  • 25

2 Answers2

4

A simpler method would be to use awk. Assuming there's nothing but files of interest in your present working directory, try:

for i in *; do awk '/^XXX$/ { c++ } c<=3' "$i" > "$i.modified"; done

Or if your files are very big:

for i in *; do awk '/^XXX$/ { c++ } c>=3 { exit }1' "$i" > "$i.modified"; done
Steve
  • 51,466
  • 13
  • 89
  • 103
2

head -n will print out the first 'n' lines of the file

#!/bin/sh

for f in `ls *.txt`; do
  echo "searching $f" 

  line_number=`grep -n -m 3 -w "^XXX$" $f | cut -d: -f1 | tail -1` 

  # line_number now stores the line of the 3rd XXX 

  # now dump out the first 'line_number' of lines from this file
  head -n $line_number $f
done
spartygw
  • 3,289
  • 2
  • 27
  • 51
  • Yeah...but the value of n is different for different files. My question is how to do this for all files in the directory. – Graddy Feb 10 '13 at 01:44
  • Trivia? He asked "is it possible to use head along with these line numbers". How does my answer not apply? – spartygw Feb 10 '13 at 01:47
  • @spartygw can you please elaborate. – Graddy Feb 10 '13 at 01:50
  • Sure, @Graddy. Your original question specifically stated 'head' and bash script so I've edited my original answer to show a bash script that does use head. I'll leave error checking to you. – spartygw Feb 10 '13 at 02:12