Results 1 to 4 of 4
guys,
I need a steer in the right direction for this issue. it would be great if anyone of you can help me out.
i have a textfile where i ...
- 07-28-2009 #1Just Joined!
- Join Date
- Mar 2009
- Posts
- 8
Bash script questions for gurus
guys,
I need a steer in the right direction for this issue. it would be great if anyone of you can help me out.
i have a textfile where i want to swap the lines based on the user input.
The textfile is looks like the
#file 1 name
TB
#file 1 ID
1000
#
#file for ID1 system1
AB_12_TB_3000
#
#file for ID1 system2
BC_12_TB_3000
#file 2 name
CO
#file 2 ID
5000
#
#file for ID2 system1
AB_12_CO_3000
#
#file for ID2 system2
BC_12_CO_3000
if the user enters CO then i want CO,5000 and the files should be file 1 and the TB should be file 2. basically interchanging it and making the user entry to be on top and putting the top one to be on the placeholder of file2.here i have mentioned only 2 files, but i am dealing with more than 2 in my files.
The code i have written so far is like this. it works except the ID part.
THE FILENAME IS ROCK.IN i.e that why u see rock.in at the end.
MY QUESTION WOULD BE
1)IS THERE A MORE ROBUST AND EASIER WAY TO DO THIS.
2)MY LAST SED STATEMENT IS NOT CHANGING IT PROPERLY
Code:#!/bin/bash user_rock=$(grep -hi 'tb' rock.in) val=$(grep -A 3 -m 1 -hi 'tb' rock.in) rock1=$(sed -n -e '/file 1/ {n;p;}' rock.in) D1=`echo $rock1 | cut -d ' ' -f3` r_name=`echo $D1| cut -d '_' -f3` D2=`echo $rock1 | cut -d ' ' -f2` D3=`echo $val | cut -d ' ' -f5` D6=`echo $val | cut -d ' ' -f3` D4=`echo $user_rock | cut -d ' ' -f3` u_name=`echo $D4| cut -d '_' -f3` sed -i "s/$u_name/ch/gi" rock.in sed -i "s/$r_name/$u_name/gi" rock.in sed -i "s/ch/$r_name/gi" rock.in sed -i "/file 1 ID/,/{n;p;}/s/$D2/$D3/g" rock.in sed -i "/file $D6 ID/,/{n;p;}/s/$D3/$D2/g" rock.in #echo $D3 $D2 exit 0
I WOULD APPRECIATE ANY IDEAS OR SUGGESTIONS. THANKS AGAIN
- 07-28-2009 #2Just Joined!
- Join Date
- Mar 2009
- Posts
- 8
let me post the desired output..
#file 1 name
CO
#file 1 ID
5000
#
#file for ID1 system1
AB_12_CO_3000
#
#file for ID1 system2
BC_12_CO_3000
#file 2 name
TB
#file 2 ID
1000
#
#file for ID2 system1
AB_12_TB_3000
#
#file for ID2 system2
BC_12_TB_3000
once the user input is CO, i move the CO and its desired files to the file1 and the old values of file 1 to where the CO was.
hope this is clear if not i can explain it again.
- 07-28-2009 #3
So the first thing I notice is that your script doesn't actually take in any user input. The second thing I notice is that this is going to be very difficult with only two IDs, and will get nearly impossible with more than two.
I do not believe that Bash is the right tool for the job. This is because the way that I would model the problem is as follows:
I would write this in a language that uses classes (or in C, using structs). I would declare the following class:
I would then have my program create a linked list of these. Linked lists are nice because you can very easily move the elements around.Code:struct file { string name; string id; string system1; string system2; }
Having done this, I first read the file. Ignoring all lines that begin with a '#', I populate my linked list with structs that contain the information. They are presently ordered in the order of the file.
Having done this, I get user input. The user input matches the "name" field of one of the structs. I traverse the list and look for that name: when I find it, I cut it out and put it at the beginning of the list. Then I print out the file, in this new order.
This is difficult to do in Bash because Bash does not support linked lists (or structs, though that can be worked around), and it is therefore very difficult to model the data.
Does this make sense?DISTRO=Arch
Registered Linux User #388732
- 07-28-2009 #4Just Joined!
- Join Date
- Mar 2009
- Posts
- 8
Cabhan,
for the sake of testing i have hard coded the user input for time being.
i got it working with the above script i have mentioned. all i am asking is that if there is a more robust way of doing it.
Does this makes sense?
please read the post before answering...


Reply With Quote