LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2009, 03:28 AM   #1
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Rep: Reputation: 18
getting the name of the output file


hey i have a doubt, suppose in the script i include an exe type of file for ex. ./a.out, which takes input as a pdb file namely 1sn3.pdb, now i want my output text file to contain the name 1sn3.txt , how do i do that using the shell script ???
is there any method ???? or any command ???

please let me know
 
Old 11-02-2009, 04:22 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Assuming that this is done in bash and there is a variable that holds 1sn3.pdb you can do this:

fullFileName="1sn3.pdb" <-- the varibale I assumed)
namePart=${fullFileName%.*}

namePart now holds everything before the last dot (1sn3 in this case).

Adding a new extension: newFileName="${namePart}.txt"

This could be done in one command: newFileName="${fullFileName%.*}.txt"

Hope this helps.
 
Old 11-02-2009, 09:40 AM   #3
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
No thee input file i give while the script is running. So how can i assign variable for that?
 
Old 11-02-2009, 09:58 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

While running or when starting? Both need a different approach.

For the first one read <var> comes to mind.

The second one can be done in several ways, but script <inputfile> is often used. The name of the inputfile is represented as $1 ($2 being the second option, $3 being the third. I.e: script file1 file2 file3).

Although you can use $1 in all of the script, for readability you transfer it to a "human readable" variable (inputfile="$1") and work with inputfile in the rest of the script.

These questions seem to be hypothetical, if you want a good guide to bash scripting take a look here: Advanced Bash-Scripting Guide.

Hope this helps.
 
Old 11-02-2009, 10:10 AM   #5
AngTheo789
Member
 
Registered: Sep 2009
Posts: 110

Rep: Reputation: 24
Based on Druuna's comments your bash script would look like this:

fullFileName="1sn3.pdb"
newFileName="${fullFileName%.*}.txt"
./a.out fullFileName > newFileName # assuming that a.out outputs to stdout as plain text output

Your script could also read a file containing a list of .pdb filenames - in that case the above sample would need to go into a loop sequence until all entries are processed.
 
Old 11-02-2009, 10:21 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by ibabhelix View Post
hey i have a doubt, suppose in the script i include an exe type of file for ex. ./a.out, which takes input as a pdb file namely 1sn3.pdb, now i want my output text file to contain the name 1sn3.txt , how do i do that using the shell script ???
is there any method ???? or any command ???

please let me know
Somewhat off-topic: this is a question, not a doubt.
http://wordnetweb.princeton.edu/perl/webwn?s=question
http://wordnetweb.princeton.edu/perl/webwn?s=doubt



Cheers,
Tink
 
Old 11-03-2009, 02:55 AM   #7
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
hey i meant tat suppose this is my script :

Quote:
#test.sh
./a.out
clear
when i run it ./test.sh
it ll ask Enter filename:
then suppose i enter 1d6a.pdb
the output file is given as teta

now i want this teta to have the name as 1d6a

how do i do that ?
 
Old 11-03-2009, 03:40 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Here's a simple bash script that does so:

Code:
#!/bin/bash

# clear the screen
clear

# echo question and ask (read) answer
echo ""
echo -n "Enter filename : "
read fileName

# strip all after last dot (xxxx.aa -> xxxx)
newFileName="${fileName%.*}"

# echo new name to screen
echo "New filename is : ${newFileName}"
Testrun:
Code:
$ ./tets.sh

(screen is cleared)

Enter filename : 1d6a.pdb
New filename is : 1d6a

Hope this helps.
 
Old 11-03-2009, 05:37 AM   #9
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Original Poster
Rep: Reputation: 18
Hey u r not getting my exact question. the script includes a exe file which asks for the input file in the command prompt.

like this :
Quote:
#test.sh
./a.out

clear
cut -c 13-28 2K6.rtf > prt1t
exec 6<"prt1t" # assign file descriptor 6 to file1

while read a <&6 # read a line from file1
do
read b <&6 # read the 2nd line from file1
echo "$a" >> prt1 # print both lines from file1
echo "$b" >> prt1
echo "$b" >> prt1

done
exec >&6

sed '1,2d' prt1 > prt2
paste prt1 prt2 a6.txt > ans1t

sed '1d' prt2 > prt3
paste prt1 prt2 prt3 b6.txt > ans2t

sed '1,2d' prt3 > prt4
paste prt1 prt2 prt3 prt4 c6.txt > ans3t

awk '{ if($9 != "") print $0 }' ans1t > ans1
awk '{ if($13 != "") print $0 }' ans2t > ans2
awk '{ if($17 != "") print $0 }' ans3t > ans3


rm prt1t prt1 prt2 prt3 prt4 a6.txt b6.txt c6.txt ans1t ans2t ans3t
when u run ./test.sh it asks, i want the file name ans1 to be written as 1d6a
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GPG - encode file while manipulating output file name itmozart Linux - Newbie 2 10-03-2009 12:28 PM
Output Redirection - Trying to output to screen and file? helptonewbie Linux - Newbie 7 03-19-2009 07:05 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM
why when redirecting output to a file any file extension seems to be fine? dr_zayus69 Linux - General 1 05-21-2005 04:09 AM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration