Need help with shell commands (not sure which command is appropriate)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with shell commands (not sure which command is appropriate)
# 1  
Old 08-21-2014
Need help with shell commands (not sure which command is appropriate)

Dear All,

I have 2 files namely file1 and file2.
in file1 I have something like this
Code:
Min X = -250.000000, Max X =  250.000000
Min Y = -95.000000, Max Y =  95.000000
Min Z = -95.000000, Max Z =  136.500000

I have to write this date to file2 as
Code:
xMin -250.000000;
xMax 250.000000;

yMin -95.000000;
yMax 95.000000;

zMin -95.000000;
zMax 136.500000;

xg  #calc "abs((250.000000-(-250.000000))/$level0)";
yg  #calc "abs((95.000000-(-95.000000))/$level0)";
zg  #calc "abs((136.500000-(-95.000000))/$level0)";


I hope one can easily understand what I am doing. Its all coping values and replacing previous values.

Note: files will have other entities. So echo kind of commands will not work for me.

Thanks & Regards,
linuxUser_
# 2  
Old 08-21-2014
Dear linuxUser_,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • Are you needing the answer in the last few rows to be calculated to give an answer? ... and where does $level0 come from?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.



Robin
# 3  
Old 08-21-2014
Dear rbattel,

Thanks for the reply.
To be specific, I want to do all there operations using shell script.
I am using linux 12.04 LTS.

Things I tried so far.
1. I know how to read complete file as variable. This can be done by simple shell script as follows
Code:
#!/bin/bash
readFile=$(<fileName)

now readFile is a variable having all the information in specified file. to confirm one can check as follows
Code:
echo "readFile"

This will print complete file contents.
2. If I know the value of a variable to replace, I can do somthing like this
Code:
sed 's/xValue .*;/xValue 19.1434;/' file > file.$$ && mv file.$$ file

(Thanks to Don Cragun for the above code)
3. I don't know how to read particular value. Only thing I know is the variable names I wanted to read from file.
As specifies in my thread
Code:
Min X = minXvalue, Max X = maxXvalue
similarly Y and Z

...
to read this I can get whole line using grep command as follows
Code:
grep -r -n "Min X" fileName

will get complete line
called
Code:
Min X = minXvalue, Max X = maxXvalue

So, All I need now is how to read a particular value from the file and store in some variable and write it to new file.


Thanks & Regards,
linuxUser_
# 4  
Old 08-21-2014
Thanks for the response,

It's good to see what you have got already. Will the input always be in this fixed format? i.e. two labels, equals, value, comma, two labels, equals, value

If it's always three lines, then we can work with that. Would you like something fast, or something easy to read?



Robin
# 5  
Old 08-21-2014
Yes, Input is always in the same format.
Code:
Min X = -250.000000, Max X =  250.000000
Min Y = -95.000000, Max Y =  95.000000
Min Z = -95.000000, Max Z =  136.500000

But the values can be anu real numbers.
As of now I need something very fast. I would like to know if any easy ways available as well.

Thanks for your help.

Regards,
linuxUser_
# 6  
Old 08-21-2014
So for fast, I presume you are reading many files in, so perhaps someone can consider that with a good awk command.

For a relatively simple but probably slow ksh/bash script, you could try:-
Code:
sed -n 1p file | read a b c minx d e f maxx
sed -n 2p file | read a b c miny d e f maxy
sed -n 3p file | read a b c minz d e f maxz

minx="${minx%,}"
miny="${miny%,}"
minz="${minz%,}"

That will give you the values, so you can write the first rows of the output. You will then have to calculate the results and write the last records too.


By way of explanation:-
  • The sed is getting the line number referenced as x in -n xp
  • The read has place holders a-f to ignore some of the record
  • The minx="${minx%,}" etc. trims the trailing comma off the string.
You can get the ABS value by the using the last trick with a different format. You would use:-
Code:
value="${value#-}"

... to trim off a leading hyphen. Now the floating point calculation might be a bit trickier.


There is surely an awk function to do this all in one, but I'm not very experienced in it. If someone else would like to offer something, I will not be upset, but I will have a think.



Robin
This User Gave Thanks to rbatte1 For This Post:
# 7  
Old 08-21-2014
I will try it out and let you know the status.

Thanks a lot for your help Smilie

Regards,
linuxUser_
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

decipher shell commands

Hi Guys, I am busy trying to re-write a shell script that was written way back. I need help with these codes: # Process switches if ; then echo "usage : process <optional instance>" exit 99 fi What does the above code mean? What does these $? -gt 1 mean? Then I have... (3 Replies)
Discussion started by: Phuti
3 Replies

2. Shell Programming and Scripting

Any shell or hack that makes the shell command line take vi commands?

basically i'm tired of hitting the left arrow a few dozen times when correcting a mistake or modifying a history command i'd like to use vim style key shortcuts while on the command line so that a 55 moves the cursor 55 places to the left... and i want all the other vi goodies, search of... (3 Replies)
Discussion started by: marqul
3 Replies

3. UNIX for Dummies Questions & Answers

Help with shell commands

hi!i want to print all registered users(theres a difference for registered users and logged users right?we can print logged users using who command but registered?) in the system,the groups and my current running processes(with the command ps -ef , ok i can get all the running processes in the... (13 Replies)
Discussion started by: strawhatluffy
13 Replies

4. Shell Programming and Scripting

Help in shell commands

Hi, I am working on HP-AUX Unix machine. I need to find a file whether it has been updated in 15 minutes. Please help me with the command. I am using Ksh (1 Reply)
Discussion started by: chinniforu2003
1 Replies

5. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

6. Shell Programming and Scripting

Shell commands

Hey, I have two seperate files with a column og data about item 277 and 289 in each. Fx: 277 -1.34534 -0.98272 0.12293 etc and 289 -4.58493 9.88273 9.33829 etc. How do i get the second input for item 277 (i.e. -.098272) and add or subtract it from the similar second input from... (2 Replies)
Discussion started by: Wenani
2 Replies

7. UNIX for Dummies Questions & Answers

what are some different commands in c shell and korn shell??

I am doing this simple script using c shell and korn shell. The commands I use are fgrep , ls, and also some redirecting. Is there any difference in using both of these commands in c shell and korn shell? Thanks and sorry for the stupid question. (1 Reply)
Discussion started by: EquinoX
1 Replies

8. Shell Programming and Scripting

Hide my shell commands

I am writing a tool that connects using SSH to a remote server and perform some actions (through root) However - I would like to hide my operations so they will be hard to track. I tried STRACE on the SSHD process and saw all the traffic going there so I am quite transparent to STRACE ... (1 Reply)
Discussion started by: yamsin789
1 Replies

9. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies

10. Shell Programming and Scripting

KORN Shell - Spawn new shell with commands

I want to be able to run a script on one server, that will spawn another shell which runs some commands on another server.. I have seen some code that may help - but I cant get it working as below: spawn /usr/bin/ksh send "telnet x <port_no>\r" expect "Enter command: " send "LOGIN:x:x;... (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question