getting the stanza names if the pattern found


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Data getting the stanza names if the pattern found

Hi Friends,
I have a file as below :

machine1:
abc
xyz
qwerty
machine2:
jkl
mno
machine3:
hhh
kkk
qwerty

Now ...i need to find all the stanza names that have the pattern "qwerty'"
in it...( i need to get the output as machine1 and machine3 since qwerty pattern is found there).
Can anybody share there knowledge in this?

Thanks in advance

Last edited by vijaya2006; 09-21-2007 at 11:07 AM.. Reason: error in the text
# 2  
Code:
awk '/machine/{store=$0}
     /qwerty/ {print store}' "file"

output:
Code:
# ./test.sh
machine1:
machine3:

# 3  
CPU & Memory printing stanzas that have the required pattern

Hi ghostdog74,
The output printed is only machine3 not machine1.
Let me put my question again ..

I have a file with many stanzas...as below..

Monday:
designs
velocity
Tuesdaey:
Projetn
Manage
wednesday:
work
velocity
********
(all the non-stanza lines have a space eg ..design,veloocity,manage..etc all have a space at the beginning)

I need to get the stanza names that have the pattern "velocity"
The o/p should be as below...
Monday
Wednesday

can u plz help me...its really imp...

Thanks in advance...
Quote:
Originally Posted by ghostdog74
Code:
awk '/machine/{store=$0}
     /qwerty/ {print store}' "file"

output:
Code:
# ./test.sh
machine1:
machine3:

# 4  
According to your first example data file, ghostdog's awk command should, and indeed does, return machine1 and machine3

Try it again, but type the command carefully. Also, check that your example data file has no typos. The example file I created from your first post returned machine1 and 3 as expected.

Specifically adding a space at the beginning of a search string (for whatever reason you might have...) is easily accomplished:

awk '/machine/{store=$0} / qwerty/ {print store}' "file"

Note the difference from the original:
awk '/machine/{store=$0} /qwerty/ {print store}' "file"

But the original will still return the correct results since "qwerty" (based on the data you provided) is in the string regardless of there being anything before or after the word.

In your second example, the common element in your stanza names would seem to be that they end with a colon.
awk '/:/{store=$0} / velocity/ {print store}' "file"

Last edited by [MA]Flying_Meat; 09-21-2007 at 06:20 PM..
# 5  
Quote:
Originally Posted by [MA]Flying_Meat
According to your first example data file, ghostdog's awk command should, and indeed does, return machine1 and machine3

Try it again, but type the command carefully. Also, check that your example data file has no typos. The example file I created from your first post returned machine1 and 3 as expected.

Specifically adding a space at the beginning of a search string (for whatever reason you might have...) is easily accomplished:

awk '/machine/{store=$0} / qwerty/ {print store}' "file"

Note the difference from the original:
awk '/machine/{store=$0} /qwerty/ {print store}' "file"

But the original will still return the correct results since "qwerty" (based on the data you provided) is in the string regardless of there being anything before or after the word.

In your second example, the common element in your stanza names would seem to be that they end with a colon.
awk '/:/{store=$0} / velocity/ {print store}' "file"
Hi Ghostdog74 and Flying_Meat,
Thanks to both of u ....the code works as expected..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #112
Difficulty: Easy
macOS was not the largest Unix largest installed base in 2017.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing the pattern of the file names in 2 different directories

Hi, I have got a requirement for which i need your help. The following problem is required to get solved in PERL SCRIPT. Here is the requirement. There are 4 folders say SRC_DIR1, SRC_DIR2 and TGT_DIR_1,TGT_DIR_2 (Note: both path of SRC_DIR1 & SRC_DIR2 are different but both path of... (4 Replies)
Discussion started by: shadow_fawkes
4 Replies

2. AIX

Fsize in default: stanza.

The default: stanza in /etc/security/limits is still set to 2097151 on fsize (max file size). I know tar had issues with large files but is there any other reasons for it? I'm thinking yes since it's still set to that by IBM. Cheers, DH (4 Replies)
Discussion started by: Devyn
4 Replies

3. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

4. Shell Programming and Scripting

How to find pattern in file names?

I have some files, those are abbreviated (ed,ea, and bi) company_ed_20100719.txt company_ea_20100719.txt company_bi_20100719.txt I would like to rename these files by replacing ed with EmployeeDetails ea with EmployeeAddress bi with BankInfomration as company_... (3 Replies)
Discussion started by: LinuxLearner
3 Replies

5. Shell Programming and Scripting

Remove repeating pattern from beginning of file names.

I want a shell script that will traverse a file system starting at specific path. And look at all file names for repeating sequences of and remove them from the file name. The portion of the name that gets removed has to be a repeating sequence of the same characters. So the script would... (3 Replies)
Discussion started by: z399y
3 Replies

6. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

7. UNIX for Dummies Questions & Answers

copying a pattern of files in one directory into other with new pattern names...

Hi, I have to copy a set of files abc* in /path/ to /path1/ as abc*_bkp. The list of files appear as follows in /path/: abc1 xyszd abc2 re2345 abcx .. . abcxyz I have to copy them (abc* files only) into /path1/ as: abc1_bkp abc2_bkp abcx_bkp .. . (6 Replies)
Discussion started by: new_learner
6 Replies

8. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

9. Shell Programming and Scripting

selecting the stanza fields

Hi Friends, I have a stanza file as below : CuDv: name = "hdisk34" status = 0 chgstatus = 3 ddins = "scsidisk" location = "06-08-02" parent = "fscsi0" connwhere = "W_0" PdDvLn = "disk/fcp/mpioosdisk" CuDv: ... (1 Reply)
Discussion started by: vijaya2006
1 Replies

10. Shell Programming and Scripting

Pattern not found : AWK , help

Hi All, My file : $ cat my.txt AA:22:note:AA BB:AA:help:XX CC:14:AA:CC ZZ:AA:hello:AA A) <searching "AA" as 2nd field in all lines of my.txt > $ awk -F ":" '{ if ($2 ~ /AA/) print "found in line - " NR; else print "Not found"}' my.txt Not found found in line - 2 Not found found... (7 Replies)
Discussion started by: jkl_jkl
7 Replies

Featured Tech Videos