Get the list with sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get the list with sed or awk
# 1  
Old 12-04-2012
Get the list with sed or awk

Code:
#cat file
[General]
aa
bb
cc
[Sabino B]
dd
ee
[XXX C]
[QQQ D]
[zzz D]
77
dd
[FAFS EE]
gg
xx

I want to get: when VAR="zzz D" get below out put
Code:
aa
bb
cc
77
dd

the rules is add General items to zzz D item. and get the list
# 2  
Old 12-05-2012
Try

Code:
awk '/^\[/{a=""}
a{print}
/\[General\]/{a=1}
/\[zzz D\]/{a=1}' file

# 3  
Old 12-05-2012
Quote:
Originally Posted by pamu
Try

Code:
awk '/^\[/{a=""}
a{print}
/\[General\]/{a=1}
/\[zzz D\]/{a=1}' file

Thanks, it works,can you explain it to me

Lei
# 4  
Old 12-05-2012
Quote:
Originally Posted by yanglei_fage
Thanks, it works,can you explain it to me

Lei
Minimized by one more line...Smilie
Code:
awk '/^\[/{a=""}                          # If line starts with "[" (means VAR) set a="" (null)
a{print}                                  # If a is present then print
/\[General\]|\[zzz D\]/{a=1}' file      # If line contains [General] or [zzz D]  then set a=1, So that we can print all the lines after these values.

I hope this helpsSmilie

pamu
# 5  
Old 12-05-2012
Quote:
Originally Posted by pamu
Minimized by one more line...Smilie
Code:
awk '/^\[/{a=""}                          # If line starts with "[" (means VAR) set a="" (null)
a{print}                                  # If a is present then print
/\[General\]|\[zzz D\]/{a=1}' file      # If line contains [General] or [zzz D]  then set a=1, So that we can print all the lines after these values.

I hope this helpsSmilie

pamu
I understand it partly, if change zzz D to a variable, can we get it eg: VAR="zzz D"
# 6  
Old 12-05-2012
Through Sed..
Code:
$ uname -rs
SunOS 5.10
$ sed -n 'H;${
x
s/[^]]*]\n\([^[]*\).*\[zzz D\]\n\([^[]*\)\n.*/\1\2/p
}' inputfile
aa
bb
cc
77
dd
$

# 7  
Old 12-05-2012
Code:
awk '/^\[/{a=""}
a{print}
$0~"[General]" || $0~var {a=1}' var="[zzz D]" file

Edit: This gives to much, not sure whats wrong

Last edited by Jotne; 12-05-2012 at 02:17 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get some data from a list, using grep or sed

Hi, my dear friends, I have to use very frequently the command of qsum to check the free node for job submitting. However, qsum always shows all the nodes regardless of their status. occupied p1 100000mb 48:00:00 1 p72 51200mb -- 1 p73 51200mb -- ... (4 Replies)
Discussion started by: liuzhencc
4 Replies

2. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

3. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

4. UNIX for Advanced & Expert Users

Argument list too long w/ sed

Hi all, I am using GNU sed (named gsed under macports) in OSX. I have a directory with a series of files named pool_01.jpg through pool_78802.jpg. I am trying to use this command to rename the files to their checksum + extension. md5sum * | gsed -e 's/\(*\) \(.*\(\..*\)\)$/mv -v \2 \1\3/e' ... (3 Replies)
Discussion started by: openthomas
3 Replies

5. Shell Programming and Scripting

sed/awk script to parse list of bandwidth rules

Hello all gurus, I have a long list of rules as below: 20 name:abc addr:203.45.247.247/255.255.255.255 WDW-THRESH:12 BW-OUT:10000000bps BW-IN:15000000bps STATSDEVICE:test247 STATS:Enabled (4447794/0) <IN OUT> 25 name:xyz160 addr:203.45.233.160/255.255.255.224 STATSDEVICE:test160... (3 Replies)
Discussion started by: sb245
3 Replies

6. Shell Programming and Scripting

Converting a line into a list using awk or sed

Hello, I am trying to convert a line into a list using awk or sed. Line: 345 897 567 098 123 output: 345 897 567 098 123 thanks (7 Replies)
Discussion started by: smarones
7 Replies

7. Shell Programming and Scripting

Replacing or removing a long list of pattern by using awk or sed

Input: >abc|123456|def|EXIT| >abc|203456|def|EXIT2| >abc|234056|def|EXIT3| >abc|340056|def|EXIT4| >abc|456000|def|EXIT5| . . . Output: def|EXIT| def|EXIT2| def|EXIT3| def|EXIT4| def|EXIT5| . . My try code: (9 Replies)
Discussion started by: patrick87
9 Replies

8. Shell Programming and Scripting

sed/awk help to match list of patterns and remove from org file

Hi, From the pattern mentioned below remove lines based on pattern range. Conditions 1 Look For all lines starting with ALTER TABLE and Ending with ; and contains the word MOVE.I wanto to remove these lines from the file sample below. Note : The above pattern list could be found in... (1 Reply)
Discussion started by: rajan_san
1 Replies
Login or Register to Ask a Question