find content from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find content from a file
# 1  
Old 11-29-2007
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
# 2  
Old 11-29-2007
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 }'

# 3  
Old 11-30-2007
Your requirement and the desired output is little different
only show column 1 , 3 & 6

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

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
# 4  
Old 11-30-2007
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}'

# 5  
Old 11-30-2007
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

# 6  
Old 11-30-2007
Java

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.
# 7  
Old 11-30-2007
Quote:
Originally Posted by summer_cherry
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; 12-01-2007 at 12:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove exisiting file content from a file and have to append new file content?

hi all, i had the below script x=`cat input.txt |wc -1` awk 'NR>1 && NR<'$x' ' input.txt > output.txt by using above script i am able to remove the head and tail part from the input file and able to append the output to the output.txt but if i run it for second time the output is... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

2. Shell Programming and Scripting

Find difference in content between two particular lines in two files

I have two files named Before.txt and After.txt: Now i want to find the difference in content between <Marker 1> and <Marker 2> in the two files. ---------- Post updated at 05:00 PM ---------- Previous update was at 04:50 PM ---------- Any help will be highly appreciated..:) (3 Replies)
Discussion started by: proactiveaditya
3 Replies

3. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

4. Shell Programming and Scripting

cut the variable from the line and use it to find the file and read the content of that file

Hi, I am working on one script..I am having files in the below format file 1 (each line is separated with : delimeter) SPLASH:SPLASH:SVN CIB/MCH:MCH:SVN Now I want from file 1 that most left part of the first line will store in... (6 Replies)
Discussion started by: rohit22hamirpur
6 Replies

5. Shell Programming and Scripting

how to find a content in all files

Hi, i would like to find 'AppManage' in all files. i have tried the following but it didn't work. /local/home/mani>grep -iR 'AppManage' *.* - not outcome /local/home/mani>grep -iR 'AppManage' - this one hangs thanks (3 Replies)
Discussion started by: lookinginfo
3 Replies

6. Shell Programming and Scripting

Find out the match data content?!

Hi, Long list of Input file1 content: 1285_t 4860_i 4817_v 8288_c 9626_a . . . Long list of Input file2 content: 1285_t chris germany 8288_c steve england 9626_a dave swiss 9260_s stephanie denmark . . . (14 Replies)
Discussion started by: patrick87
14 Replies

7. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

8. Shell Programming and Scripting

how to find out overlaped content

i have two single column text files a and b. in this two files, there are some overlap content, is there any quick way I can find out those overlapped content? example, file a 1 2 3 4 5 6 file b 2 35 7 8 4 2 and 4 are overlapped in these two files so they should be picked up. (3 Replies)
Discussion started by: fedora
3 Replies

9. UNIX for Dummies Questions & Answers

find the same content in the file

I have two files ( eg. file1 and file2 ) , its contents are as below , I want to find out the same content ( same word ) in the two files , in the below example , the same content is "aaa" , could suggest how to do it ? thx. #vi file1 aaa bbb ccc ddd #vi file2 111 222 333 aaa (4 Replies)
Discussion started by: ust
4 Replies

10. UNIX for Dummies Questions & Answers

find filename based on file content

:confused: There is a flat file on my system which contains email addreses of people in my company. This file is utilized when sending notifications for various things. However nobody knows where this file is located or what it is named. The only thing we know is the email address of a user who... (4 Replies)
Discussion started by: kollerj
4 Replies
Login or Register to Ask a Question