Print lines between a regExp & a blank line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print lines between a regExp & a blank line
# 1  
Old 11-30-2011
Print lines between a regExp & a blank line

Hi,

I have a file, say files_list, as below (o/p of ls -R cmd)

Code:
 
$ cat files_list
/remote/dir/path/to/file:
sub-dir1
sub-dir2
sub-dir3
...
 
/remote/dir/path/to/file/sub-dir1:
remote_file1.csv.tgz
<blank line 1>
/remote/dir/path/to/file/sub-dir2:
remote_file2.csv.tgz
<blank line 2>
/remote/dir/path/to/file/sub-dir3:
remote_file3.csv.tgz
<blank line 3>
....

I want to read this file in a loop and produce an o/p like
Code:
 
Output file
-----------
/remote/dir/path/to/file/sub-dir1/remote_file1.csv.tgz
/remote/dir/path/to/file/sub-dir2/remote_file2.csv.tgz
/remote/dir/path/to/file/sub-dir3/remote_file3.csv.tgz
.....

I was able to extract all sub-dir in a file, say sub_dir_file
Code:
 
$ cat sub_dir_file
sub-dir1
sub-dir2
sub-dir3

I thought of looping through the list of sub-dir and using below sed between
Code:
 
for line in cat sub_dir_file
do
sed -n '/remote/dir/path/$line, <blank line of this block i.e. blank line1>/p' files_list
.........more formatting..........
done

Am I on the right path or can you please correct the sed /from/,/to pattern/p command?

-dips
# 2  
Old 11-30-2011
Bug

please give this a try

Code:
ls -R | gawk ' /:$/ { dir=gensub(/^(.*):$/, "\\1/", "g"); next; } { print dir $NF }'

# 3  
Old 11-30-2011
You are on the right path, however, note that a slash is a metacharacter for sed. You have to escape your path slashes with backslash. Like:
Code:
sed -n '/\path\/to\/somewhere/,/^$/p' files_list

Yeah, and the /^$/ is an empty line. A pattern /^[ \t]*$/ would be even better, if you suspect there may be some whitespace chars hidden.

However, isn't this too much work? Have you looked at find(1) yet?

Try
Code:
find /remote/dir/path/to/file -type f

# 4  
Old 11-30-2011
Similar awk solution..
Code:
ls -R | awk -F'[ :]' '$0~/^[.]*\/.*/ {r=$1;next} r!="" && $0!="" {print r "/" $0}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

Print 4th line back from regexp

I'm looking for a way to print the 4th line back from a regular expression. Kind of like the below but it has to be the 4th line before the regexp. Print the line immediately before regexp, but not the line containing the regexp. sed -n '/regexp/{g;1!p;};h' here is an example of logs(i... (11 Replies)
Discussion started by: senormarquez
11 Replies

3. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

4. Shell Programming and Scripting

sed print first line before regexp and all lines after

Hi All I'm trying to extract the line just above a regexp and all lines after this. I'm currently doing this in two steps sed -n -e "/^+---/{g;p;}" -e h oldfile.txt > modified.txt sed -e "1,/^+---/d" -e "/^$/d" oldfile.txt >>modified.txt Sample sometext will be here sometext will be... (3 Replies)
Discussion started by: Celvin VK
3 Replies

5. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

6. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies

7. Shell Programming and Scripting

How to print blank lines before and after???

Dear all, I'm a new guy in Linux scripting. Basically, I have no idea on how to write a script. Now, I have a problem in preparing my gaussian inputs. For example, I have a file like this File name: input.gjf H 1.418400 1.180500 -1.247900 H -1.894500 1.628800 -0.000200 Product H... (7 Replies)
Discussion started by: liuzhencc
7 Replies

8. Shell Programming and Scripting

print 2 lines above regexp

I am on a Solaris 10 x86 system sample code before3 before2 before1 group after1 after2 after3 I want to grab the second line above my regexp regexp=group I want to grab ONLY the before2 line I have numerous sed and awk ways of grabbing X line below the regexp, but no luck... (1 Reply)
Discussion started by: snoman1
1 Replies

9. Shell Programming and Scripting

Unix help to find blank lines in a file and print numbers on that line

Hi, I would like to know how to solve one of my problems using expert unix commands. I have a file with occasional blank lines; for example; dertu frthu fghtu frtty frtgy frgtui frgtu ghrye frhutp frjuf I need to edit the file so that the file looks like this; (10 Replies)
Discussion started by: Lucky Ali
10 Replies

10. UNIX for Dummies Questions & Answers

print the line immediately after a regexp; but regexp is a sentence

Good Day, Im new to scripting especially awk and sed. I just would like to ask help from you guys about a sed command that prints the line immediately after a regexp, but not the line containing the regexp. sed -n '/regexp/{n;p;}' filename What if my regexp is 3 word or a sentence. Im... (3 Replies)
Discussion started by: ownins
3 Replies
Login or Register to Ask a Question