LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-22-2010, 05:35 PM   #1
ifeatu
Member
 
Registered: Sep 2008
Distribution: Fedora 9
Posts: 68

Rep: Reputation: 15
Syntax Grep and Cat


I'm trying to give a single user a membership to multiple Unix groups...my syntax appears to work to stdout but I can't get it to append to the file itself:

Code:
grep printeradmin group.tmp | sed 's/$/',abrambila,root,administrator,Administrator,alekich,alice,andra,backuppc,bogus1,colofson,Contreras,dave,domainer,don,dthompson,dthompson_new,elaine,fseto,groberts,inf09$,inf11,inmantmp,jeff,jlasky,joel,jwenceslao,katy,klance,lfrantz,lhills,lhoward,madelyn,mcarter,root,nbaldwin,pmalbrough,printeradmin,rmok,rreyes,savin,savin$,sfriese,stevenc,stevenf,stvn,tftpd,tsmith,ttorbon,root,vheistermann,wbaatin,wmadden'/' > group.tmp
This command overwrites the group.tmp file with nothingness. How would I incorporate cat? So far I tried

Code:
(grep printeradmin group.tmp | sed 's/$/',abrambila,root,administrator,Administrator,alekich,alice,andra,backuppc,bogus1,colofson,Contreras,dave,domainer,don,dthompson,dthompson_new,elaine,fseto,groberts,inf09$,inf11,inmantmp,jeff,jlasky,joel,jwenceslao,katy,klance,lfrantz,lhills,lhoward,madelyn,mcarter,root,nbaldwin,pmalbrough,printeradmin,rmok,rreyes,savin,savin$,sfriese,stevenc,stevenf,stvn,tftpd,tsmith,ttorbon,root,vheistermann,wbaatin,wmadden'/' | cat > group.tmp
and

Code:
grep printeradmin group.tmp | sed 's/$/',abrambila,root,administrator,Administrator,alekich,alice,andra,backuppc,bogus1,colofson,Contreras,dave,domainer,don,dthompson,dthompson_new,elaine,fseto,groberts,inf09$,inf11,inmantmp,jeff,jlasky,joel,jwenceslao,katy,klance,lfrantz,lhills,lhoward,madelyn,mcarter,root,nbaldwin,pmalbrough,printeradmin,rmok,rreyes,savin,savin$,sfriese,stevenc,stevenf,stvn,tftpd,tsmith,ttorbon,root,vheistermann,wbaatin,wmadden'/' | cat
 
Old 04-22-2010, 05:55 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
You can't read and write to the same file in the same pipeline. Use a temp.
 
Old 04-22-2010, 05:58 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ifeatu View Post
I'm trying to give a single user a membership to multiple Unix groups...my syntax appears to work to stdout but I can't get it to append to the file itself:

Code:
grep printeradmin group.tmp | sed 's/$/',abrambila,root,administrator,Administrator,alekich,alice,andra,backuppc,bogus1,colofson,Contreras,dave,domainer,don,dthompson,dthompson_new,elaine,fseto,groberts,inf09$,inf11,inmantmp,jeff,jlasky,joel,jwenceslao,katy,klance,lfrantz,lhills,lhoward,madelyn,mcarter,root,nbaldwin,pmalbrough,printeradmin,rmok,rreyes,savin,savin$,sfriese,stevenc,stevenf,stvn,tftpd,tsmith,ttorbon,root,vheistermann,wbaatin,wmadden'/' > group.tmp
This command overwrites the group.tmp file with nothingness. How would I incorporate cat? So far I tried

Code:
(grep printeradmin group.tmp | sed 's/$/',abrambila,root,administrator,Administrator,alekich,alice,andra,backuppc,bogus1,colofson,Contreras,dave,domainer,don,dthompson,dthompson_new,elaine,fseto,groberts,inf09$,inf11,inmantmp,jeff,jlasky,joel,jwenceslao,katy,klance,lfrantz,lhills,lhoward,madelyn,mcarter,root,nbaldwin,pmalbrough,printeradmin,rmok,rreyes,savin,savin$,sfriese,stevenc,stevenf,stvn,tftpd,tsmith,ttorbon,root,vheistermann,wbaatin,wmadden'/' | cat > group.tmp
and

Code:
grep printeradmin group.tmp | sed 's/$/',abrambila,root,administrator,Administrator,alekich,alice,andra,backuppc,bogus1,colofson,Contreras,dave,domainer,don,dthompson,dthompson_new,elaine,fseto,groberts,inf09$,inf11,inmantmp,jeff,jlasky,joel,jwenceslao,katy,klance,lfrantz,lhills,lhoward,madelyn,mcarter,root,nbaldwin,pmalbrough,printeradmin,rmok,rreyes,savin,savin$,sfriese,stevenc,stevenf,stvn,tftpd,tsmith,ttorbon,root,vheistermann,wbaatin,wmadden'/' | cat
Do you need the items in red ? I mean in the first code snippet.
 
Old 04-22-2010, 06:00 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tuxdev View Post
You can't read and write to the same file in the same pipeline. Use a temp.
Yep.
 
Old 04-22-2010, 08:38 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I would be asking why bother with the grep at all seeing sed can just as easily find "printeradmin"??
Code:
sed -i '/<what you are looking for>/s/<what you want to change>/<changes you want/' file
 
1 members found this post helpful.
Old 05-16-2010, 12:17 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please mark as SOLVED if you have no more questions
 
  


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
cat|grep|awk pudhiyavan Programming 6 07-14-2008 01:56 AM
is the syntax NUM_NODES=`cat $PBS_NODEFILE |wc -l` correct? cy163 Linux - Newbie 1 05-16-2008 08:24 AM
cat and grep dipuasks Linux - General 11 04-21-2008 03:39 AM
cat to grep to textfile? jabfinger Linux - General 3 09-05-2006 03:25 PM
Question About Cat And Grep George2 Linux - Software 10 03-09-2006 03:33 AM

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

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