Visit Our UNIX and Linux User Community


How to read file and only output certain content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read file and only output certain content
# 1  
Old 06-03-2010
How to read file and only output certain content

Hi -

I have a file containing data like :-
Code:
cn=tommy,cn=users,c=uk
passwordexpirydate=20100530130623z

cn=jane,cn=users,c=uk
passwordexpirydate=20100423140734z

cn=michael,cn=users,c=uk
passwordexpirydate=20100331020044z

I want to end up with a file that looks like:-
Code:
cn=tommy,cn=users,c=uk
20100530130623
cn=jane,cn=users,c=uk
20100423140734
cn=michael,cn=users,c=uk
20100331020044

I can very inefficiently first remove the blank lines using something like:-
Code:
sed '/^$/d' file1 > file2

I can use awk to just extract the date without the "passwordexpirydate=" and "z".

But what I can not do is do eveything I need to do in one loop .. e.g. using a while read line do ... done loop.

Any ideas please?

Last edited by Franklin52; 06-03-2010 at 07:57 AM.. Reason: Please use code tags!
# 2  
Old 06-03-2010
Hi,

Code:
sed s/^passwordexpirydate=*// IN_FILE| sed '/^$/d' | sed s/z$//

It's sure there is another form in only one sentence....wait for the responses of sed and awk's gurus...
# 3  
Old 06-03-2010
MySQL

Try:

Code:
sed "/^$/d;s/^password[^=]*=\(.*\)z$/\1/" file

HTH Chris
# 4  
Old 06-03-2010
Thanks for the replies. Both answers work perfectly.

---------- Post updated at 03:02 PM ---------- Previous update was at 12:18 PM ----------

This is suddenly got more complicated!!

I need to do what I did before but also delete any line starting cn=users or cn=Users and the following line.

So,

Code:
cn=tommy,cn=users,c=uk
passwordexpirydate=20100530130623z

cn=Users,cn=users,c=uk
passwordexpirydate=20100423100000z

cn=jane,cn=users,c=uk
passwordexpirydate=20100423140734z

cn=michael,cn=users,c=uk
passwordexpirydate=20100331020044z

would end up as :-

Code:
cn=tommy,cn=users,c=uk
20100530130623
cn=jane,cn=users,c=uk
20100423140734
cn=michael,cn=users,c=uk
20100331020044

Thanks in advance for any help.
# 5  
Old 06-03-2010
Code:
sed "/^$/d;/^cn=Users/{N;d;};s/^password[^=]*=\(.*\)z$/\1/" file

# 6  
Old 06-03-2010
Also,

Code:
awk -F= '/^cn/ {print} /^passwordexpirydate/ { gsub("z","",$2);print $2}' file

# 7  
Old 06-03-2010
Cheers thanks all for the help.

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl SCript to read file content (if else statemenet)

Hi All, I wanted to write a perl script to read the content in a file,the file content is either 0 (zero) OR 1. The idea is like this. If (content =1), then it will proceed to perform some step. and then update the file content to 0(zero) else if (content =0), it will update the content to... (11 Replies)
Discussion started by: hploh
11 Replies

2. UNIX for Advanced & Expert Users

Output file content

Hi, When i run a script it is showing me the whole authentication and data to be copied on screen server1:/sasdata/script#sh .TR_CNTO328.sh ************************************************** *********************NOTICE*********************** This system is restricted to authorized users... (11 Replies)
Discussion started by: tushar_spatil
11 Replies

3. Shell Programming and Scripting

While read line ignores the '\' in file content

I need to read temp.$i file content line by line through while loop but somehow the '\' do not appear in output.. Can someone guide how to read this exact content line by line in unix : if then cat temp.$i | head -1 # the file content appears fine while... (13 Replies)
Discussion started by: Prev
13 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

Read a text file and print the content..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... ... (7 Replies)
Discussion started by: samupnl
7 Replies

6. Shell Programming and Scripting

read file content

i have one file abhi.txt its contents are home8/mc09ats/UnixCw/backup/file1 home8/mc09ats/file2 i want to read this content of file using while loop.. in this i want to seperate the content as follows path=home8/mc09ats/UnixCw/backup file=file1 echo path echo file can you... (1 Reply)
Discussion started by: AbhijitIT
1 Replies

7. Shell Programming and Scripting

Read a file content with awk and sed

Hello , I have huge file with below content. I need to read the numeric values with in the paranthesis after = sign. Please help me with awk and sed script for it. 11.10.2009 04:02:47 Customer login not found: identifier=(0748502889) prefix=(TEL) serviceCode=(). 11.10.2009 04:03:12... (13 Replies)
Discussion started by: rmv
13 Replies

8. Shell Programming and Scripting

read a file and use the content for mapping

help me pls.. :( i want to read a mapping file. Below is the content of my mapping file. 6221,189,SMSC1,OMC1,WAP1 6223,188,SMSC2,OMC2,WAP2 so when my program running msisdn="622130302310" while not EOF if substring($msisdn,1,4) == "6221" -- > "6221" read from the file then echo... (0 Replies)
Discussion started by: voidmain
0 Replies

9. Shell Programming and Scripting

Need help with awk - how to read a content of a file from every file from file list

Hi Experts. I need to list the file and the filename comes from the file ListOfFile.txt. Basicly I have a filename "ListOfFile.txt" and it contain Example of ListOfFile.txt /home/Dave/Program/Tran1.P /home/Dave/Program/Tran2.P /home/Dave/Program/Tran3.P /home/Dave/Program/Tran4.P... (7 Replies)
Discussion started by: tanit
7 Replies

10. Shell Programming and Scripting

How to read the content of the particular file from tar.Z without extracting?

Hi All, I want to read the content of the particular file from tar.Z without extracting. aaa.tar.Z contains a file called one.txt, I want to read the content of the one.txt without extracting. Please help me to read the content of it. Regards, Kalai. (12 Replies)
Discussion started by: kalpeer
12 Replies

Featured Tech Videos