1748244 Members
3761 Online
108760 Solutions
New Discussion юеВ

script

 
ivy1234
Frequent Advisor

script

script

I have the below text file , I would like to extract the data from that , there are many many lines in it , I would like to extract three lines that after the line contains the string "aaa" .

aaax
1
2
3
bbbx
4
5
6
cccx
7
8
9
aaax
10
11
12
dddx
13
14
15

The result that I would like to have is as below .

aaax
1
2
3
aaax
10
11
12

Can advise what can i do ? thx
9 REPLIES 9
Raj D.
Honored Contributor

Re: script

$ awk 'ORS=NR%4?FS:RS' file|awk /^aaa/| tr " " "\n"

" If u think u can , If u think u cannot , - You are always Right . "
ivy1234
Frequent Advisor

Re: script

thx reply,

When use the script to run my file , it pops the error , besides , if I want the line contains aaa not begins aaa , what can i do ? thx

tr: two strings must be given when translating
cat: awk: No such file or directory
cat: ORS=NR%4?FS:RS: No such file or directory
Raj D.
Honored Contributor

Re: script

lby1234,

- See the example below:
- expecting the file to be uniform of , matching pattern aaa then 3 line with numbers .
- There must be some other way with perl as well or sed.

$ cat file
aaax
1
2
3
bbbx
4
5
6
cccx
7
8
9
aaax
10
11
12
dddx
13
14
15
ZZaaax
101
112
123
YYdddx
134
145
156
$




$ cat -n file
1 aaax
2 1
3 2
4 3
5 bbbx
6 4
7 5
8 6
9 cccx
10 7
11 8
12 9
13 aaax
14 10
15 11
16 12
17 dddx
18 13
19 14
20 15
21 ZZaaax
22 101
23 112
24 123
25 YYdddx
26 134
27 145
28 156






## Here is the result: (all mathcing with aaa )
#----------------------


$ awk 'ORS=NR%4?FS:RS' file|awk /aaa/|tr " " "\n"
aaax
1
2
3
aaax
10
11
12
ZZaaax
101
112
123
$

#--------------------------------------------




# make sure os and awk/tr are correct:
$ uname -a ; which awk ; which tr
HP-UX hpux1120 B.11.23 U 9000/800 765567890 unlimited-user
/usr/bin/awk
/usr/bin/tr
$



Hth,
Raj.



* You have assigned to 6 out of 40 responses only. ???
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: script

ivy,

Check this out, this is much easier with sed:


# sed -n '/aaa/{N;N;N;p;}' file




Enjoy, Have fun,
Raj.


* Remember to assign points once done.
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: script

ivy,
See below the example: with sed :
This is good and simple to find the pattern and print next desired lines,


$ cat file
aaax
1
2
3
bbbx
4
5
6
cccx
7
8
9
aaax
10
11
12
dddx
13
14
15
ZZaaax
101
112
123
YYdddx
134
145
156
$


$ sed -n '/aaa/{N;N;N;p;}' file
aaax
1
2
3
aaax
10
11
12
ZZaaax
101
112
123
$


Enjoy,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Dennis Handly
Acclaimed Contributor

Re: script

awk '
/aaa/ {
print $0
getline; print $0 # next 3 lines
getline; print $0
getline; print $0
}' file

> Raj D.: this is much easier with sed:

But sed is likely to be harder to understand.
Dennis Handly
Acclaimed Contributor

Re: script

If you want something even easier, use GNU grep:
grep -A3 aaa file | grep -v "^--$"

You need the second grep to get rid of the separator lines.
Elmar P. Kolkman
Honored Contributor

Re: script

Before I can give a correct answer, I need to know for sure how the data looks like.
For instance:
- the 'markers' (aaax, bbbx, cccx, etc.): do they always start with a letter?
- the 'values': are they always numeric?
- are there always 3 'values' per 'marker'?

If the answer to the third question is yes, then the answers given above hold. If not, the script needs to be a bit more complicated (it needs to read statefull through the file). I could give an awk script for that, but only if needed.
Every problem has at least one solution. Only some solutions are harder to find.
James R. Ferguson
Acclaimed Contributor

Re: script

Hi:

Another way:

# perl -ne '{if (m/aaa/) {$i=3};$i-->=0 and print}' file

Regards!

...JRF...