Specifically name an output file in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Specifically name an output file in bash
# 1  
Old 04-29-2010
Specifically name an output file in bash

What is the best way to specifically name an output file in a bash script? My current script has the following output code.


Code:
awk 'BEGIN {print "CASE          GID   vd      d-theta  GID   vd     d-theta"} {print $0}' > "$TOL"_try.txt

The variable $TOL is an input to the script. I would like to prefix the name of the output file with only the first two characters of the input variable $TOL. e.g....

If $TOL = 1X_in then I would like my output file to be 1X_try.txt

Thanks
# 2  
Old 04-29-2010
Code:
n=${TOL:0:2}

in bash gives the first two characters of the variable TOL
# 3  
Old 04-29-2010
Thanks Jim. Worked great.

What does the 0 mean in the expression?
# 4  
Old 04-29-2010
The 0 is the offset, denoting the beginning of the substring slice. From https://www.unix.com/man-page/Linux/1/bash/
Quote:
${parameter:offset:length}
Substring Expansion. Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter starting at the character specified by offset.... Substring indexing is zero-based....
Note, the text I abbreviated with "...." contains some important details, so read it for yourself ;)

Just in case anyone saw the above and wondered how that can be done in a posix-compliant manner:
Code:
printf '%.2s_try.txt' "$TOL"

Regards,
Alister

Last edited by alister; 04-29-2010 at 01:17 PM.. Reason: Changed link to use local man page :)
# 5  
Old 04-29-2010
Thanks for the info and the link alister.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to extract file prefix and from input to use in output

In the bash below which does execute I am trying to extract the contents of ${id} is 1234, as ${id} stores the variable that changes each time. After the path is removed the contents of ${id} are stored in pref, so they can be used in the output. Currently I am not able to extract the 1234 in the... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. 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

3. Shell Programming and Scripting

Save output into file bash scripting

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

4. Shell Programming and Scripting

redirect time command output to file (cygwin bash)

I have set up a bash script to run a long list of things that I need to time. I would like to redirect the output of time to a file. I have set it up like, echo "Runtimes for servlet 4, 100K structures" > test_times.txt echo "" >> test_times.txt echo "runs where N=10" >> test_times.txt echo... (7 Replies)
Discussion started by: LMHmedchem
7 Replies

5. Shell Programming and Scripting

Searching file for pattern, output to file (BASH)

Hello, I'm trying to write a script in Bash to assist in pentesting. Essentially I'm looking to use a script to search for some terms in a log file and then send that key information into another file. The log files consist of HTTP and SSL information that someone creates while browsing and... (2 Replies)
Discussion started by: Sagesparten007
2 Replies

6. Shell Programming and Scripting

bash: How to send the output to a file upon users' request?

Let's say I have a script which creates a report. I use getopts to have different options available to the user. One of these options is -f which basically exports the output of script to a text file which user may give the name of that text file. How can I output the result to the requested file.... (3 Replies)
Discussion started by: bashily
3 Replies

7. Shell Programming and Scripting

Convert sql output to csv file via bash tools

hi Can anybody help me with converting such structure into csv file for windows : BAT_ID ID_num CVS_LINE A_SEG SKILL_TO A_CUSTOMER_TYPE --------- ---------- --------------------------------- ---------- ------------------ ----------- 14-MAY-11 777752 ... (4 Replies)
Discussion started by: kvok
4 Replies

8. Shell Programming and Scripting

saving output from bash into a file

I am ssh to many servers to get some information... however sometimes the server is unreacheable and i am getting an error. I want to save that output to a file but I am not able to do so... I want to be able to save output of bash into a file.. so when I run this command on a script ssh... (5 Replies)
Discussion started by: eponcedeleonc
5 Replies

9. Shell Programming and Scripting

how to direct scp output to a file in bash shell or script

I can run this from the command line: scp -i identfile /path/file_to_send remotelogin@remotebox:/path_to_put_it/file_to_send and I get: file_to_send 100% |***************************************************************************| 0 00:00 but if I do: scp -i identfile... (6 Replies)
Discussion started by: NewSolarisAdmin
6 Replies
Login or Register to Ask a Question