LinuxQuestions.org
Help answer threads with 0 replies.
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 02-14-2009, 01:57 PM   #1
da1
Member
 
Registered: May 2007
Distribution: FreeBSD
Posts: 113

Rep: Reputation: 16
:> and << or >>


hell-o ppl.

bassicly I'm over my head here. Here's what I whant to do:

create a file with a date stamp on it (excuse my poor english please)

# :> filename`date +-%d-%m-%Y` #works like a charm

then, in that file I whant to dump the output of:

# pkg_version |grep "<"

separate they work like a charm, but toghether they dont. I tryed:

# :> filename`date +-%d-%m-%Y` << pkg_version |grep "<"

and

# :> filename`date +-%d-%m-%Y` << `pkg_version |grep "<"`

and

# :> filename`date +-%d-%m-%Y` << `pkg_version`

and

# touch filename`date +-%d-%m-%Y` << pkg_version

just about every posible combination logical to me. I'm not a programer I admit. I only know very basic sh and perl scripting.

I don't know/understand why they don't work toghether.

Some advice pls.

ups, I forgot to mention my OS is FreeBSD 7.1-STABLE

Last edited by da1; 02-14-2009 at 01:59 PM.
 
Old 02-14-2009, 02:06 PM   #2
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
You didn't specify you shell. Try:

Code:
 pkg_version |grep "<" > filename`date +-%d-%m-%Y`
Explanation:

In bash > is the redirect operator. It will put the output of the command into the file with that name. So you put first what you would want to end in the file (pkg_version and the grep) and then redirect the output to the file named in the specific way.

Last edited by vladmihaisima; 02-14-2009 at 02:08 PM. Reason: (added more detailed explanation)
 
Old 02-14-2009, 02:10 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Try it the other way around.

pkg_version |grep "<" > filename`date +-%d-%m-%Y`

the > will send the output of the previous command to the filename specified, creating a new file if needed, or overwriting the file contents if it already exists. If you want to append the new data to an existing file instead, use >>.

Edit: Hah...beaten to the punch again.

Last edited by David the H.; 02-14-2009 at 02:12 PM.
 
Old 02-14-2009, 02:24 PM   #4
da1
Member
 
Registered: May 2007
Distribution: FreeBSD
Posts: 113

Original Poster
Rep: Reputation: 16
sorry, shell is csh (FreeBSD default)

I tryed pkg_version |grep "<" > touch filename`date +-%d-%m-%Y` before, just forgot to post it.... tryed with > and >>, then inverted the comand and tryed < and <<, but still, doesn't work. I get (with pkg_version [...] >> touch [...]) grep: filename-actual-date-here: No such file or dir.

one more thing.... the file the output will be dumped to doens not exist, has to/will be created prior to the actual comand

Code:
[da1@da1.ro ~]# pkg_version |grep "<" >> touch filename`date +-%d-%m-%Y`
grep: filename-14-02-2009: No such file or directory
[da1@da1.ro ~]# pkg_version | grep "<" > touch filename`date +-%d-%m-%Y`
grep: filename-14-02-2009: No such file or directory
[da1@da1.ro ~]# touch filename`date +-%d-%m-%Y` << pkg_version |grep "<"
? <- remains stuck here, unles i do ^C
and

Code:
[da1@da1.ro ~]# touch filename`date +-%d-%m-%Y` < `pkg_version`
^C
^C
^C
^D
^C
^X <- stuck, whatever I do. Have to terminate user session to get back shell
happens with `pkg_version` and pkg_version after <

Last edited by da1; 02-14-2009 at 02:33 PM.
 
Old 02-14-2009, 02:37 PM   #5
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
Try single quotes to embed the pattern:
Code:
pkg_version | grep '<' > filename`date +-%d-%m-%Y`
Moreover, you don't need the touch command since redirection will create the file automatically.

Edit: Looking at your command lines... I bet you have a file called touch in your current directory, now!

Last edited by colucix; 02-14-2009 at 02:43 PM.
 
Old 02-14-2009, 02:46 PM   #6
da1
Member
 
Registered: May 2007
Distribution: FreeBSD
Posts: 113

Original Poster
Rep: Reputation: 16
hmm, works...now if you might explain a bit why it works with single quote and not double.

haha, I forgot that >> automagicly creates the file if inexistent... silly me.

Last edited by da1; 02-14-2009 at 02:48 PM.
 
Old 02-14-2009, 02:53 PM   #7
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
Well, single quotes prevent any substitution by the shell. If you want a special character to be interpreted literally you have to embed in single quotes, whereas double quotes still permit some substitutions.

Anyway, I think that actually the error was the touch command. In your command line
Code:
pkg_version | grep "<" >> touch filename`date +-%d-%m-%Y`
the grep command look for the pattern "<" in the standard input, redirect the output to a file called touch and look for the pattern in the non-existent file filename-14022009. Hence the grep error.

Another tip: if you want the files with date in their name to be sorted in alphanumeric order AND by date when listed, use the format %Y%m%d.
 
Old 02-14-2009, 02:58 PM   #8
da1
Member
 
Registered: May 2007
Distribution: FreeBSD
Posts: 113

Original Poster
Rep: Reputation: 16
thaks a lot. i owe you one
 
  


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



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

All times are GMT -5. The time now is 07:04 AM.

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