Save output into file bash scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Save output into file bash scripting
# 1  
Old 08-26-2014
Save output into file bash scripting

Hi there.

i have created a program that in the end it will give output like this
Code:
1   2   3   4   5
10  9   8   7   6
11  12  13  14  15
..............  17

i wonder how to save the output into a single string and into a file.
i.e 1 10 11 12 9 2 3 8 13 14 7 4 5 6 15 17 (in this order, its like a snake)

i googled it but i only came across with saving it row by row (1 2 3 4 5 10 9......)

any idea how?
# 2  
Old 08-26-2014
Is this a homework assignment?

It is kind of hard to guess at how to change your program to write its output in the order you want if you don't show us the code that is writing output in snake order... Show us your code!
# 3  
Old 08-26-2014
its an assignment. im stuck at this part.

Code:
#!/bin/bash
#program for columnar transposition
#file from previous question (a)
file="encrypted.txt"

#remove all the spaces and newline and put it on a new file
name=$(cat $file)
echo $name | tr -d ' ' > temp.txt

file4="temp.txt"
name4=$(cat $file4)
echo $name4 |  sed 's/\(.\)/\1\n/g' > file4.txt

echo "enter how many cols:"
read cols

IFS=$'\n' a=($(cat file4.txt))
for j in $(seq ${#a[*]}); do
    [[ ${a[$j-1]} = $name ]] && echo "${a[$i]}"
done

for (( i=0; i<=$(( ${#a[@]} / cols )); ++i )); do
    for (( j=0; j<cols; ++j )); do
        if (( i%2 )); then idx=$(( (i + 1) / 2 * 2 * cols - j - 1 ))
        else idx=$(( (i / 2) * 2 * cols + j )); fi
        printf "%-4s  " "${a[idx]}"
    done
    printf "\n"
done

---------- Post updated at 05:17 PM ---------- Previous update was at 05:14 PM ----------

Quote:
Originally Posted by Don Cragun
Is this a homework assignment?

It is kind of hard to guess at how to change your program to write its output in the order you want if you don't show us the code that is writing output in snake order... Show us your code!
the output is all ok and correct.
now i dont know how to put all the output into 1 single string.
like i said, i googled it and found how to combine all but not with any kind of sorting
# 4  
Old 08-26-2014
Homework needs to be posted to the Homework & Coursework Questions forum and needs to be submitted with a fully filled out template as provided in the Rules for Homework & Coursework Questions Forum.

Please fill out the form and resubmit your question in the proper forum.

This thread is closed.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to save output of echo and awk to a file

Hi, I am generating a YAML file from a hosts file, but am having trouble saving it to a new file. hosts file 127.0.0.1 localhost 192.168.1.2 host1 192.168.1.3 host2 192.168.1.4 host3 192.168.1.5 host4 YAML file $ echo 'host_entries:' && awk '{printf " %s:\n ip:... (3 Replies)
Discussion started by: sand1234
3 Replies

2. Shell Programming and Scripting

Bash to select and save file in new directory

I am trying to select a file in bash and save it to a directory. The below does run but no selected file is saved. Thank you :). bash # select file printf "please select a file to analyze with entered gene or genes \n" select file in $(cd... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Save value from output of Corestat and save in a list for each core

I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below: Core Utilization CoreId %Usr %Sys %Total ------ ----- ----- ------ 5 4.91 0.01 4.92 6 0.06 ... (0 Replies)
Discussion started by: Zam_1234
0 Replies

4. Homework & Coursework Questions

Save output into file bash scripting

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: Hi there. i have created a program that in the end it will give output like this 1 2 3 4 5 10 9 ... (1 Reply)
Discussion started by: shdin271
1 Replies

5. Shell Programming and Scripting

PERL Scripting: Diff 2 files and save output to file3

Hi, I need to create a script to compare 2 files and store the output in a 3rd file. This is how I do manually, but since I need to do this for about 150 files every week, I am trying to automate it using perl. diff -u file1 file2 > file3.patch For my script, - I have 2 files... (4 Replies)
Discussion started by: script2010
4 Replies

6. Shell Programming and Scripting

Way to save output result of a program into another new file...

Does anybody know any alternative way to save output result of a program into another new file? I got try the command below: program_used input_file > new_output_file program_used input_file >> new_output_file Unfortunately, both the ">" and ">>" is not work at this case to save the output... (6 Replies)
Discussion started by: patrick87
6 Replies

7. Shell Programming and Scripting

Save cURL verbose output to file or do it like browser "save as.."

hi there ! i have exactly the same problem like this guy here https://www.unix.com/shell-programming-scripting/127668-getting-curl-output-verbose-file.html i am not able to save the curl verbose output.. the sollution in this thread (redirecting stderr to a file) does not work for me.... (0 Replies)
Discussion started by: crabmeat
0 Replies

8. Shell Programming and Scripting

[bash] redirect (save) array to a file

Hi, I'm trying to write a function that redirects the contents of an array to a file. The array contains the lines of a data file with white space. The function seems to preserve all white space when redirected except that it seems to ignore newlines. As a consequence, the elements of the... (7 Replies)
Discussion started by: ASGR
7 Replies

9. Solaris

terminal output - save to file?

I have a window open on my ultra 10 - a terminal window connecting to a server. Is there any way I can log all output to this window to a log file on my ultra 10 ? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question