The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to delete content in a file (delete content only) kittusri9 Shell Programming and Scripting 5 05-15-2008 10:12 AM
Using the content of a file in the name of another anriot Shell Programming and Scripting 2 09-18-2006 04:56 PM
transfer of specific file content to another file mem101 Shell Programming and Scripting 1 10-18-2005 11:01 AM
find the same content in the file ust UNIX for Dummies Questions & Answers 4 03-25-2005 02:55 AM
find filename based on file content kollerj UNIX for Dummies Questions & Answers 4 06-02-2001 10:31 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-29-2007
ust ust is offline
Registered User
 

Join Date: Feb 2005
Posts: 95
find content from a file

root 0 0 5 0 11/09/07 08:18
root 0 0 6 0 11/09/07 08:56
root 0 0 7 0 11/22/07 11:09
user1 0 0 8 0 11/08/07 15:58
user2 0 0 9 0 11/30/07 08:37
user3 0 0 10 0 11/30/07 08:56
root 0 0 11 0 11/30/07 08:58
test
user4 0 0 12 0 11/30/07 09:02
user5 0 0 13 0 11/30/07 09:34
root 0 0 14 0 11/30/07 09:11
user6 0 0 15 0 11/30/07 09:26
user6 0 0 15 0 11/30/07 09 : 26
root 0 0 16 0 11/30/07 09:28


I have the above file that have many lines , I would like to find some content in the file and output to another file with the below condition as below format .

1. do not have the word root
2. only show column 1 , 3 & 6
3. add a line no. to each row
4. do not show the duplicate line ( as above , user6 is duplicate , then only show ONE time in the new output.



the output should be as below .

1 user1 0 11/08/07
2 user2 0 11/30/07
3 user3 0 11/30/07
4 user4 0 11/30/07
5 user5 0 11/30/07
6 user6 0 11/30/07


can advise what can i do ? thx
Reply With Quote
Forum Sponsor
  #2  
Old 11-29-2007
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
Keeping in mind your comment about removing duplicates is ambiguous (do you mean only show one user6 per day or only one user6 - if so, which one?), here's a guess:

Code:
grep -v 'root' thefile | sort -u | awk '{ print NR,$1,$3,$6 }'
Reply With Quote
  #3  
Old 11-29-2007
Registered User
 

Join Date: Dec 2005
Posts: 74
Your requirement and the desired output is little different
only show column 1 , 3 & 6

then u must get 'test' in your output!!

This is the same line copied from smiling dragon, i have just introduced a check before printing.

grep -v 'root' sed.sh | sort -u | awk '{ if(user != $1){print NR,$1,$3,$6} ;user=$1 }'

RUV
Reply With Quote
  #4  
Old 11-29-2007
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 495
awk

Hi,

This one should be ok.

Code:
sed '/root/d' filename | awk 'NF>=2' | sed 's/ : /:/' |sort -u | uniq -u | awk 'NF>=2 {print NR,$1,$3,$6}'
Reply With Quote
  #5  
Old 11-29-2007
Read Only
 

Join Date: Nov 2007
Posts: 165
How about a method that uses more shell functionality:

Code:
lineno=0
ulast=blah
sort -u myfile | while read s
do
    set -- $s
    case $1 in
       $ulast) continue;;
       user*) ;;
       *) continue;;
    esac
    let lineno=lineno+1
    echo $lineno $1 $3 $6
    ulast=$1
done
Reply With Quote
  #6  
Old 11-29-2007
Registered User
 

Join Date: Nov 2007
Posts: 15
Post

Hi,

I have a very similar requirement. I'm storing the passwords in a flat file, when ever the user updates the password, I'm planning to write the new password along with some other values and delete the old one. I thought of doing it using "grep -v". Can anybody help me out how to pass a variable from C program to unix command. In my previous post, I got a reply from bakunin saying to use sprintf(). I tried the below code.
Code:
main()
{
char v_unix[10];
sprintf(v_unix,"%s",argv[1]);
system("cat filename | grep -v v_test > filename2");
}

This didn't work. Help me to accomplish this.

Thanks & Regards,
Venkatesh.
Reply With Quote
  #7  
Old 11-30-2007
ust ust is offline
Registered User
 

Join Date: Feb 2005
Posts: 95
Quote:
Originally Posted by summer_cherry View Post
Hi,

This one should be ok.

Code:
sed '/root/d' filename | awk 'NF>=2' | sed 's/ : /:/' |sort -u | uniq -u | awk 'NF>=2 {print NR,$1,$3,$6}'
thx ,

this script is quite useful , the output as below,

1 user1 0 11/08/07
2 user2 0 11/30/07
3 user3 0 11/30/07
4 user4 0 11/30/07
5 user5 0 11/30/07
6 user6 0 11/30/07
7 user100 0 11/30/07
8 user1222 0 11/30/07

but if I want the output is more tidy as below , can advise what can i do ?

1 user1 0 11/08/07
2 user2 0 11/30/07
3 user3 0 11/30/07
4 user4 0 11/30/07
5 user5 0 11/30/07
6 user6 0 11/30/07
7 user100 0 11/30/07
8 user1222 0 11/30/07

Last edited by ust; 11-30-2007 at 09:44 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:32 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0