need help with script - output to file, but complicated


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help with script - output to file, but complicated
# 1  
Old 10-26-2008
need help with script - output to file, but complicated

ok, so what i want to do is make a script that will do the following:

take out from a command in the terminal
put that output into a text file already on my computer.

the only thing is that i need to put the output in the file kinda weird: i need to take it and put each character of output on a new line, but not create new lines

example:

take output of command 'uptime' (up 32 days, 23:09)
place in file 'myFile'

output:
...
key u
key p
key space
key 3
key 2
key space
key d
...

anyone have any idea on how to do this?

thanks in advance
# 2  
Old 10-27-2008
What are you working on?
What have you attempted so far and and where do you get stuck?

Regards
# 3  
Old 10-27-2008
try something like this:

#!/bin/bash

output=$(uptime)
length=${#output}

for ((i=1; i<=length; i++))
do
character=$(echo $output | cut -c$i)
case "$character" in
[[:space:]]) echo "key: space";;
*) echo "key: $character";;
esac
done
# 4  
Old 10-27-2008
thank you both for replies! i am very new to shell scripts, but not to programming. but i am still having trouble understanding some things about shell programming.

i will try the script that you made. thanks again
# 5  
Old 10-27-2008
Quote:
Originally Posted by creepy cripple
try something like this:

#!/bin/bash

output=$(uptime)
length=${#output}

for ((i=1; i<=length; i++))
do
character=$(echo $output | cut -c$i)
case "$character" in
[[:space:]]) echo "key: space";;
*) echo "key: $character";;
esac
done
okay, i ran this script and it creates exactly what i was looking for. the only thing now is that i want to take that output, and put it in a file that already exists. the file has a bunch of "key Down Arrow" and so on in it on each line.

i want to get to where there is a "key n" where n = any number, 0-9. and overwrite the lines from with the output, and then leave the rest of the file alone.
# 6  
Old 10-27-2008
Quote:
Originally Posted by creepy cripple
try something like this:

#!/bin/bash

output=$(uptime)
length=${#output}

for ((i=1; i<=length; i++))
do
character=$(echo $output | cut -c$i)
case "$character" in
[[:space:]]) echo "key: space";;
*) echo "key: $character";;
esac
done
okay. i am knew to scripting, and am trying to follow this.

what i see is that there is a variable called 'output' that holds the output of the 'uptime' command

i am guessing that the variable 'length' is the length (how many characters) of the variable 'output'.

there is a for loop that goes thru each character

there is another variable called 'character' that takes each letter of 'output'

then it prints "key: " and character to the screen.

my question is how can i put this in a file?

also, i see "*)" on the 3rd to last line. what does that do?
# 7  
Old 10-27-2008
well, i've been messing around with the code, and this is what i have so far:

Code:
#!/bin/bash

output=$(uptime)
length=${#output}

cat tim | while read LINE
do
if [LINE == "key 0"]; then
for ((i=1; i<=length; i++))
do
character=$(echo $output | cut -c$i)
case "$character" in
[[:space:]]) echo "key <space>" >> tim;;
*) echo "key $character" >> tim;;
esac
done

fi
done

the filename is 'tim'. i get an error on line 8 of this code though.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assistance on complicated perl script

As a followup to my previous thread, I'm trying to make a complicated perl script that involves storing information from a text file into a hash, and giving the user the ability to change the information present/write the information currently inside the hash to a new file. This is the code I've... (8 Replies)
Discussion started by: Eric1
8 Replies

2. Shell Programming and Scripting

Help on a fairly complicated shell script

Hello, also with the help of some great users of this forum, I have created following shell script. MM=120 GG=5000 # get size of directory szm=$(du -s --block-size M ./192.168.1.xxx | awk '{print int($0)}') data=$(date --rfc-3339=seconds) if ; then # too big delete older files ... (10 Replies)
Discussion started by: dcaccount
10 Replies

3. Shell Programming and Scripting

Search complicated strings on file

Can someone help me? I been figuring out how I can search and extract a complicated search string from a file. The whole string is delimited by a period. And the file where I'm searching is composed of differnt string such as that. For example, I have this search string: and I have a file... (3 Replies)
Discussion started by: Orbix
3 Replies

4. Shell Programming and Scripting

Script Search replace - complicated

I have a text file for which i need a script which does some fancy search and replace. Basically i want to loop through each line, if i find an occurance of certain string format then i want to carry on search on replace another line, once i replaced this line i will contine to search for the... (7 Replies)
Discussion started by: kelseyh
7 Replies

5. Shell Programming and Scripting

Parsing a Complicated properties file

Hi All, I have a requirement to parse a file. Let me clear you all on the req. I have a job which contains multiple tasks and each task will have multiple attributes that will be in the below format. Each task will have some sequence number according to that sequence number tasks shld... (1 Reply)
Discussion started by: rajeshorpu
1 Replies

6. Shell Programming and Scripting

Complicated(?) text file comparison

I've got two files, both plain text. Each file is a datafeed of products, pipe delimited. The current file is in directory 1 and yesterday's file is in directory 2 (literally, those are the directory names). What I'm trying to do is compare the files and pull out products whose price has changed... (3 Replies)
Discussion started by: Daniel M. Clark
3 Replies

7. Shell Programming and Scripting

complicated search within file

Hi, I have following problem. I have a file with time stamps and some data describing what happened between time stamps. Something like this: 10:00 meeting with K meeting with L 11:00 lunch 12:00 work with K 13:00 From this file I have to get a file with... (7 Replies)
Discussion started by: mmike
7 Replies

8. Shell Programming and Scripting

Very complicated script..

I have a script that I need to create tha involves moving files and renaming them(see previous post) Are there any websites with user made shell scripts? (5 Replies)
Discussion started by: rocinante
5 Replies

9. Shell Programming and Scripting

Need help with complicated script (reading directories, extracting data)

Hi, people. I was searching script tutorials/examples and found this forum, and thought to ask for help, because there seems to be many wise people here. (Try to bare with me, I'm a Windows user, so this stuff is somewhat strange to me, OK? :rolleyes: ) Anyways, I need to create a script that... (3 Replies)
Discussion started by: Gill Bates
3 Replies

10. Shell Programming and Scripting

Complicated string searching in a file

Hi folks, Following a part of opmn.xml file: <process-type id="OC4J_RiGHTv_PLATOR81" module-id="OC4J"> <environment> <variable id="LD_LIBRARY_PATH" value="/home/ias/v10.1.2/lib" append="true"/> <variable id="SHLIB_PATH"... (5 Replies)
Discussion started by: nir_s
5 Replies
Login or Register to Ask a Question