- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Need help with scripting control characters
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
Forums
Discussions
Discussions
Discussions
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
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
03-31-2006 12:47 AM
03-31-2006 12:47 AM
Not sure if this can be explained so well but I'll give it my best shot...
I need to write a shell script that would read a text file as input and output all entries in the file with a blank line between "circuit.." and "Type.." lines (or 2 carriage return characters before the line beginning with the word "Type").
Please look at the following lines as an example of the text file:
*************
circuit1
Type1
circuit2
Type2
circuit3
Type3
circuit4
Type4
*************
So here, I would want the script to output circuit3 and circuit4 since they have blank line gaps between ciruit and Type text.
I'm not too hot on pattern matching over multiple lines. Any advice will be appreciated.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2006 01:10 AM
03-31-2006 01:10 AM
Re: Need help with scripting control characters
INPUTFILE=
OUTPUTFILE=
cat $INPUTFILE |while read LINE; do
case $LINE in
Type*)
echo "" >> $OUTPUTFILE
echo "" >> $OUTPUTFILE
echo $LINE >> $OUTPUTFILE
;;
cir*)
echo $LINE >> $OUTPUTFILE
esac
done
Or you can use something like this
# File convert_sh:
cat $1 |while read LINE; do
case $LINE in
Type*)
echo "" >> $2
echo "" >> $2
echo $LINE >> $2
;;
cir*)
echo $LINE >> $2
esac
done
Usage: convert.sh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2006 01:31 AM
03-31-2006 01:31 AM
Re: Need help with scripting control characters
# cat .perl.sh
#!/usr/bin/perl
while (<>) {
if (/circuit/) {
$last=$_;
next;
}
if (/^\s+$/) {
print $last if defined $last;
}
if (/Type/) {
undef $last;
}
}
...run as:
# ./perl.sh filename
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2006 01:59 AM
03-31-2006 01:59 AM
Solution$ awk '/^circuit/{b=0;c=$1} /^Type/&&(b==1){print c} (NF==0){b++}' your-file
or
$ perl -ne "if (/(circuit\d+)/){$c=$1;$b=0}; $b++ if /^$/; print "$c\n" if ($b==1 and /^Type/)" your-file
in both cases:
When 'circuit' is seen at the begin of a line, remember the first word in c, and clear the blank-line counter b.
When a blank line is seen, increment b
When 'Type' is seen at the begin, and the blank-line-counter is 1, then print.
Now if there can be no-blank lines, then it gets more tricky. You can then for example remember while line (NR in awk, $. in perl) the the first word was found, and check the rigth advance on the second word. For example:
awk '/^circuit/{n=NR;b=0;c=$1} /^Type/ && b==1 && NR==n+2 {print c} (NF==0){b++}" your-file.
or your can to the 'staged search approach' in shell or other script:
loop
if first condition then set stage-1
if stage-1 and second condition then set stage-2
if stage-2 and last condition then report.
clear stage on input and mismatch.
so many options...
cheers,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2006 02:29 AM
03-31-2006 02:29 AM
Re: Need help with scripting control characters
perl -ne 'do {$x=$_;$y=$.} if /^circuit/;print $x if /^Type/ and $.-$y>1' yourdata
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2006 02:34 AM
03-31-2006 02:34 AM
Re: Need help with scripting control characters
I tested all of them and got Heins and Rodneys to work successfully.
Thanks all of you for your help...
Safdar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2006 04:31 AM
03-31-2006 04:31 AM
Re: Need help with scripting control characters
# sed -n '/^circuit/N;s/\n$//p' infile
cheers!