Need help with completing a bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with completing a bash script
# 1  
Old 05-15-2013
Need help with completing a bash script

Hello All,

I am automating a task using bash script and got stuck at this. From the below input,

Code:
  Meta Device Members (20) :
        {
        ----------------------------------------------------------------------
                               BCV  DATA                    RDF  DATA         
                      ----------------------------  --------------------------
        Sym    Cap    Std Inv BCV Inv Pair          R1 Inv R2 Inv Pair        
        Dev    (MB)   Tracks  Tracks  State         Tracks Tracks State       
        ----------------------------------------------------------------------
    --> 0AFC  51201        -       -  N/A                0      0 Consistent  
        0AFD  17263        -       -  N/A                0      0 Consistent  
        0AFE  51201        -       -  N/A                0      0 Consistent  
        ----------------------------------------------------------------------
            1024013        -       -                     0      0
        }

I need output like this

Code:
0AFC-51201;0AFD-17263;0AFE-51201

The "-->" is annoying and need help working around it. Also, the no. of devices will be different in each scenario. How can this be done? Any help is appreciated.

Last edited by Sam R; 05-15-2013 at 10:18 PM.. Reason: Adding extra info
# 2  
Old 05-15-2013
Please post your script.
# 3  
Old 05-16-2013
I got it working with the following code but it doesn't appear that graceful. For my learning experience, wanted to know other ways how this can be done.

Code:
 grep -w "[0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F]" filename|awk 'NF{if (NF==8){print $1"-"$2}else if(NF==9){print $2"-"$3};next}1'|grep -w "[0-9,A-F][0-9,A-F][0-9,A-F][0-9,A-F]"|tr '\n' ';'

# 4  
Old 05-16-2013
You could simplify it to:
Code:
awk '/[0-9][A-F]/{sub("-->",X);s=s?s";"$1"-"$2:$1"-"$2}END{print s}' file

OR
Code:
awk '/[0-9][A-F]/{sub("-->",X);printf $1"-"$2";"}END{printf "\n"}' file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 05-16-2013
sed can handle it but has no output formatting.
With sed and tr
Code:
sed -n 's/^[- >]\{5,\} \([0-9A-F]\{4\}\)  *\([0-9][0-9]*\) .*/\1-\2/p' file |
 tr '\n' ';'

awk has output formatting, and an RE or BRE can be transformed to ERE
Code:
awk 'sub(/^[- >]{5,} ([0-9A-F]{4}) +([0-9]+) .*/,"\1-\2")' ORS=";" file

but this only works with a true Posix awk, like Solaris /usr/xpg4/bin/awk.
nawk and GNU awk are missing the {n,m} feature from ERE, so they need some more conversion.
But perl can do it out of the box
Code:
perl -lne 'BEGIN {$\=";"} {print if s/^[- >]{5,} ([0-9A-F]{4}) +([0-9]+) .*/$1-$2/}' file


Last edited by MadeInGermany; 05-16-2013 at 09:44 AM.. Reason: perl!
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 05-16-2013
Thanks for the input guys. Few followup questions. I just realized that these script wouldn't work if the input file is slightly different. For the following input, I would require the same output. How would I achieve this?

Code:
Meta Device Members (20) :
        {
        ----------------------------------------------------------------------
                               BCV  DATA                    RDF  DATA         
                      ----------------------------  --------------------------
        Sym    Cap    Std Inv BCV Inv Pair          R1 Inv R2 Inv Pair        
        Dev    (MB)   Tracks  Tracks  State         Tracks Tracks State       
        ----------------------------------------------------------------------
        0AFC  51201        -       -  N/A                0      0 Consistent  
    --> 0AFD  17263        -       -  N/A                0      0 Consistent  
        0AFE  51201        -       -  N/A                0      0 Consistent  
        ----------------------------------------------------------------------
            1024013        -       -                     0      0
        }
            Device : 0AFC (M)
                {
                --------------------------------------------------------------
                 Disk     DA       Hyper       Member    Spare       Disk
                DA :IT   Vol#   Num Cap(MB)  Num Status  Status  Grp#  Cap(MB)
                --------------------------------------------------------------
                02B:D13  2012    80    2906    1 RW      N/A        8   286102
                02C:D14  2102    80    2906    2 RW      N/A        8   286102
                02D:D13  2004    80    2906    3 RW      N/A        8   286102
                02A:D14  2107    80    2906    4 RW      N/A        8   286102

Needed output

Code:
0AFC-51201;0AFD-17263;0AFE-51201

# 7  
Old 05-16-2013
Code:
$ awk '/[0-9][A-F]/&&!/:/{sub("-->",X);printf $1"-"$2";"}END{printf "\n"}' file
0AFC-51201;0AFD-17263;0AFE-51201;

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Bash not calling function after completing

In the below bash the function execute is started with the while true at the end of the script. Currently, this works and processing does begin. The function panel in bold does not get called after execute completes, therefore the user does not get the menu selection and the files just loop... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Code exits before completing

The lines in bold check the value of a user input, if it is not one of the expected genes (GJB2, MECP2, PHOX2B), the user is shown a list of formats to use and the variant is entered. It is then supposed to printf that entered variant? Thank you :). name() { printf "\n\n" printf... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Looping not completing in shell script

Hi, Iam using below code to login to servers to get cpu utilisation. but output is coming for only one server. code is below root@blr-svr-oclan-01 # more SSSC_CPU_UTIL1.sh #!/bin/sh echo "CPU UTILIZATION" while read line; do IDLE=`/usr/local/bin/sshpass -p 'xxx' ssh xxx@$line 'sar 2 2' |... (1 Reply)
Discussion started by: surender reddy
1 Replies

5. Homework & Coursework Questions

Help completing lab.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: ''you are missing the /home/smichaels/Labs/lab2b/group file, please create it as per step 12 of the lab. once... (4 Replies)
Discussion started by: ink
4 Replies

6. Shell Programming and Scripting

shell script exiting before completing

I have a script which has the following statement, /opt/oracle/product/9i/bin/sqlplus << EOC >> $LOG_FILE 2>&1 username/password ---- Enters the SQL prompt @/export/home/oracle/shells/grant_userview.sql ---Runs the SQL script @/export/home/oracle/shells/grant_proc_userview.sql ---Runs the... (6 Replies)
Discussion started by: welldone
6 Replies

7. UNIX for Dummies Questions & Answers

Completing Command Entry without Typing

Hi, In my UNIX (KSH) sytem, in order to Complete Command Entry without Typing the whole path or name, I have to use ESC+\ where as I am used to press TAB key to do the same. Can anyone tell me where we need to change the settings of the keyboard combination so that when I press TAB key the file... (6 Replies)
Discussion started by: jisha
6 Replies

8. Shell Programming and Scripting

tcsh + completing a sub-directory entry

As part of a script Im designing, I am required to allow the command line to finish off part of a series of sub-directory entries. Basically what I want to do is cp /root/parent/part of subs name + other part of subs name/file /other_root/other_parent/other_sub/file without facing... (0 Replies)
Discussion started by: JamesGoh
0 Replies

9. UNIX for Dummies Questions & Answers

Completing ffmpeg installation

Hello. i am new to unix, though have quite a substantial background of other systems. i recently installed ffmpeg and mencoder on a unix server, which holds a website, in order to use these programs from the website. after installation, i can activate both commands directly using putty, no... (1 Reply)
Discussion started by: noamon
1 Replies

10. Shell Programming and Scripting

Terminate session on completing script

Hai all.. How do i terminate my telnet session automatically when my java applicatiion exits. i have a file run which executes my java application and takes care of all class and library path settings prior to the execution. I would like to terminate my session when my application exits. The... (4 Replies)
Discussion started by: deepsteptom
4 Replies
Login or Register to Ask a Question