Extract from stdout the string in [ ], in a nicely way
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Extract from stdout the string in [ ], in a nicely way
cat myfile.txt
Code:
* [82] Download the cdrom [83] Linux Ubuntu Forum
I would like that a script i.e.
Code:
mysuperscript.sh myfile.txt
simply just outputs all intances that are between [ ]:
Code:
82
83
I am using awk, but that is not really easy with it. I like awk since it is much easier than C, perl, and python. ... but that is not made for that at all I guess
#!/usr/bin/perl
$/ = undef; # go into slurp mode into the array @L
$slurp = <>; # slurp the file
@L = $slurp =~ m/\[(.*?)\]/g; # get the match
$" = "\n"; # set array separator which works if you...
print "@L\n"; # print array in quotes
#!/usr/bin/env sh
# @(#) s1 Demonstrate extraction of bracket-bounded numeric string.
set -o nounset
echo
debug=":"
debug="echo"
## Use local command version for the commands in this demonstration.
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash grep sed
echo
echo '* [82] Download the cdrom [83] Linux Ubuntu Forum' >data1
FILE=data1
grep -E -o '\[[0-9]+\]' $FILE |
sed -e 's/\[//' -e 's/\]//'
exit 0
Producing:
Code:
% ./s1
(Versions displayed with local utility "version")
GNU bash 2.05b.0
grep (GNU grep) 2.5.1
GNU sed version 4.1.2
82
83
I see it works, but don't understand how. Can you explain at all please?
My difficulty: I see the primes (') which normally delimit an awk program. So it's essentially
Code:
#2
'$0=$2'
If I run this on it's own, I can see that it is the complete program and what it does in the default environment; it takes the second field and makes it into the total field and prints it ( the default action - in the same way that print infers print $0 ).
Now because the RS="]" FS="[" are outside the program (I would normally have expected to see this type of assignment inside a BEGIN block)[code]
, I expect that they are being passed as parameters and would have expected to see
Code:
#4
awk -v FS="[" -v RS="]" '
$0 = $2' < test.txt
, which without the -v will throw a syntax error.
Well, Radoulov, I have to thank you for the explanation thus far, but here comes the bit I couldn't work out; what makes putting the command line assignments(?) after the program and before the redirected stdin input work?
I hope that this discussion will also be of use to others that hadn't quite thought about the subtleties implicit in your VERY NEAT bit of code. Please share your magic with me/us.
Personally (before today) I would have written the code as shown in #3 above, had I been smart enough to work it out for myself. Honest, I would prefer to leave you guessing . The version at #4 I might have used if it had occurred to me that It was valid to pass values to the awk built-in variables. It didn't although I am otherwise familiar with parameter passing in this manner.
So a super thread at several levels with lots of learning content. Well done everyone.
Sorry Bigears, I'm just an awk kinda guy. Perl is just too rich for me. One day perhaps, but don't hold your breath.
Are both according to the command line syntax rules: From the Sed & Awk manual: awk [-v var=value] [-Fre] [--] 'pattern { action }' var=value datafile(s)
Yes,
as already stated, the variable assignment variable=text
is part of the Awk syntax and its usage may depend on
personal style.
But note that -v variable=text is not the same as variable=text,
for examle:
zsh-4.3.4-dev-2% awk -vv=x 'BEGIN{print v}{print v;exit}' v=z <(yes)
x
z
Another important point is that
older awks don't support the -v syntax.
Anyway,
my Awk examples were kind of joke:
one doesn't have to use Awk for such trivial
task, like Ed Morton says it's like suggesting a pneumatic drill, when it's posisble
to break it up with a toothpick.
Last edited by radoulov; 11-23-2007 at 12:56 AM.
Reason: spelling ..., sorry :)
Thank you for taking the time.
It took a moment, but yes, all understood, the post-code assignment is only good where there are instances of data read in.
My sed is weak (working on it) and my awk/nawk (Solaris) is probably a little dated.
Re the pneumatic drill/toothpick anology, when you have a hammer, every problem is a nail!
Joke or not, it served a very useful update for me. It's appreciated.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.