Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2010 06:40 PM
тАО08-12-2010 06:40 PM
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
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2010 07:21 PM
тАО08-12-2010 07:21 PM
Re: script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2010 07:55 PM
тАО08-12-2010 07:55 PM
Re: script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2010 08:13 PM
тАО08-12-2010 08:13 PM
Re: script
- 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. ???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2010 08:30 PM
тАО08-12-2010 08:30 PM
Re: script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2010 08:32 PM
тАО08-12-2010 08:32 PM
Re: script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2010 09:11 PM
тАО08-12-2010 09:11 PM
Re: script
/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2010 09:17 PM
тАО08-12-2010 09:17 PM
Re: script
grep -A3 aaa file | grep -v "^--$"
You need the second grep to get rid of the separator lines.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-12-2010 10:10 PM
тАО08-12-2010 10:10 PM
Re: script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2010 05:11 AM
тАО08-13-2010 05:11 AM
Re: script
Another way:
# perl -ne '{if (m/aaa/) {$i=3};$i-->=0 and print}' file
Regards!
...JRF...