LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-14-2010, 04:39 AM   #1
paliga
Member
 
Registered: Feb 2010
Posts: 39

Rep: Reputation: 0
Putting to much into one line


Is there a way to put something like:

ls /some/dir 2>/tmp/.myname_FUNNYPART_$RANDOM,

where FUNNYPART is the output of pwd, where the "/" are switched to "-",

in one line?

thanks in advance!

cherioos...
 
Old 09-14-2010, 05:22 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by paliga View Post
Is there a way to put something like:

ls /some/dir 2>/tmp/.myname_FUNNYPART_$RANDOM,

where FUNNYPART is the output of pwd, where the "/" are switched to "-",

in one line?

thanks in advance!

cherioos...
You can do it like this:

Code:
ls /some/dir 2>"/tmp/.myname_$(pwd | tr '/' '-')_${RANDOM}"
In this, the code "pwd | tr '/' '-'" gets run first, and whatever it puts onto stdout will get inserted into the command.

(Note: you need {}-style braces to make "${RANDOM}", not "$(RANDOM)")
 
1 members found this post helpful.
Old 09-14-2010, 05:37 AM   #3
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by paliga View Post
Is there a way to put something like:

ls /some/dir 2>/tmp/.myname_FUNNYPART_$RANDOM,

where FUNNYPART is the output of pwd, where the "/" are switched to "-",

in one line?

thanks in advance!

cherioos...
You should use:
Code:
$ ls . | tee /tmp/.yourname_$(echo `pwd` | sed 's/\//-/g')
 
1 members found this post helpful.
Old 09-14-2010, 06:40 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
No need to invoke external commands in bash:
Code:
2> /tmp/.myname_${PWD//\//-}_$RANDOM
 
1 members found this post helpful.
Old 09-14-2010, 12:29 PM   #5
cbh2000
LQ Newbie
 
Registered: Sep 2010
Distribution: openSUSE 11.3
Posts: 14

Rep: Reputation: 0
Thumbs up

Off topic:
@colucix I love your signature!
 
Old 11-16-2010, 09:01 AM   #6
paliga
Member
 
Registered: Feb 2010
Posts: 39

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
No need to invoke external commands in bash:
Code:
2> /tmp/.myname_${PWD//\//-}_$RANDOM
but that wouldnt be one line anymore, since I need a PWD=`pwd`, dont I?
 
Old 11-16-2010, 09:06 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by paliga View Post
but that wouldnt be one line anymore, since I need a PWD=`pwd`, dont I?
No; $PWD is envalued by bash
 
Old 11-16-2010, 09:11 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by paliga View Post
but that wouldnt be one line anymore, since I need a PWD=`pwd`, dont I?
Nope. PWD is an internal shell variable: it is automatically updated every time you change directory, as well as OLDPWD, which retains the path of the previous working directory (useful when you use "cd -" to return to the previous location). You can use PWD at any time and it will give you the path of your current working dir. Moreover it is common to all the shells, not only bash.
 
Old 11-16-2010, 11:00 AM   #9
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
I'd recommend you to have a look at "sed" or "tr" commands online rather than copying and pasting what the others have suggested. They are really helpful commands which will come in handy.
 
Old 11-16-2010, 11:29 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by ghantauke View Post
I'd recommend you to have a look at "sed" or "tr" commands online rather than copying and pasting what the others have suggested.
Indeed sed and tr are very useful commands, but can you explain why they should be better than the efficient and quick shell's parameter substitution?
 
Old 11-16-2010, 07:00 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
I am with colucix ... why reinvent the wheel or use additional applications when the solution is built in
 
Old 11-17-2010, 09:05 AM   #12
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
Quote:
Originally Posted by ghantauke View Post
I'd recommend you to have a look at "sed" or "tr" commands online rather than copying and pasting what the others have suggested. They are really helpful commands which will come in handy.
Your solution is indeed more efficient than mine for this problem. But a combination and tr and sed would be more powerful than the inbuilt substitution alone, for other cases. And learning these commands just take minutes. I'm just trying to make paliga know more about linux commands rather than spoon feed him.
 
Old 11-17-2010, 09:15 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ghantauke View Post
But a combination and tr and sed would be more powerful than the inbuilt substitution alone, for other cases. And learning these commands just take minutes.
Especially sed is very powerful and if you can learn it in minutes you are a genius
 
Old 11-17-2010, 09:22 AM   #14
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
Quote:
Originally Posted by catkin View Post
Especially sed is very powerful and if you can learn it in minutes you are a genius
I agree with you. It might take a while to learn about sed from scratch. But if you've programmed before and are familiar with the concept of regular expression already then it'd just take minutes.
 
Old 11-18-2010, 07:49 AM   #15
paliga
Member
 
Registered: Feb 2010
Posts: 39

Original Poster
Rep: Reputation: 0
Well... I would like to direct my thanks to all of you : )

cheerios...
 
  


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
Putting blank line after the search pattern. dina3e Programming 2 09-21-2008 07:38 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
putting word in front of each line in a file stryka Slackware 3 12-06-2005 02:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:59 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