Script to display lines in a file in a particular format


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script to display lines in a file in a particular format
# 1  
Old 10-18-2011
Script to display lines in a file in a particular format

I have a bunch of files in various folders. I want to go through each of them and display certain lines in a particular format

All files have a similar format

Code:
Date:
Time: 
User:
Message:
Miscellaneous:
(and some other stuff)

I want to display to only the "Date:", "Time:" "User:" lines in the format

Code:
Date: <space/tab> Time: <space/tab> User:

For example, if I have two files

Code:
Date: 12/12/12
Time: 00:15
User: usr1
Message: N/A
Miscellaneous: N/A

Code:
Date: 02/12/12
 Time: 01:15
 User: usr2
 Message: N/A
 Miscellaneous: N/A

I want a file with the following

Code:
12/12/12     00:15   usr1
02/12/12     01:15   usr2

(or)

Code:
Date: 12/12/12    Time: 00:15      User: usr1
Date: 02/12/12    Time:  01:15      User: usr2

Note, these files are in various different folder. I tried to use grep -r, but it only displays one line.

Thanks!!!

Last edited by radoulov; 10-18-2011 at 06:51 PM.. Reason: Code tags!
# 2  
Old 10-18-2011
Code:
find . -type f |xargs -i awk -F : '/Date/{d=$2}/Time/{t=$2 FS $3}/User/{u=$2}END{printf "Date:  %s\tTime: %s\tUser: %s\n", d,t,u}'  {}

# 3  
Old 10-18-2011
Another way might be ...

Given files f1 and f2 and awk script ex.awk
cat f1
Date: 12/12/12
Time: 00:15
Age: xxxxxx
Processid: 45
User: usr1
Message: N/A
Miscellaneous: N/A
hunke3@hunkemoller3:~/code/shell$ cat f2
Other: stuff
Time: 01:15
Message: N/A
Date: 02/12/12
Miscellaneous: N/A
Host: somehost
User: usr2
hunke3@hunkemoller3:~/code/shell$
hunke3@hunkemoller3:~/code/shell$
hunke3@hunkemoller3:~/code/shell$ cat ex.awk
BEGIN{
d=""; t=""; u=""; flds=0;
}
#only take lines beginning with Date | Time | User
/^Date/{d=$0;flds=flds+1;}
/^Time/{t=$0;flds=flds+1;}
/^User/{u=$0;flds=flds+1;}
{
if(flds==3){#ok,so we have a complete set of fields
printf("%s %s %s\n",d,t,u);
d=""; t=""; u=""; flds=0
}
}

# run it ....
awk -f awkEx.awk f1 f2
Date: 12/12/12 Time: 00:15 User: usr1
Date: 02/12/12 Time: 01:15 User: usr2
# 4  
Old 10-19-2011
Thanks for the replies, but I got a couple of issues with the codes

@rdcwayx
I tried the code that you had given, but it was only printing blanks i.e. the variable d,t,u were empty.

@munkeHoller

I tested your code against a few files, and it works. But how to I pass in all the files, given using the find command, into your script?

Thanks a lot!!!!
# 5  
Old 10-19-2011
Code:
#
# given my previous snippet .... and files f1 f2 where f1 is in ~/thisdir and f2 is in ~/thatdir
# one way ... 
#
find ~/thisdir ~/thatdir -name 'f?' -exec awk -f awkEx.awk {} \;

# 6  
Old 10-19-2011
It works Smilie

Sorry to ask again...how does the code get modified if I want to print specific lines of each file eg. line x, line y and line z. Its seems my data occasionally has some of the terms (e.g. user) twice in the file. I know it has something to do with NR, but i don't know where to place the conditional.
# 7  
Old 10-20-2011
show us the data pls.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display calendar in correct format using shell script

Hi All, I'm trying to print calendar using shell script and i'm able to print it. But the format is not good. Here is the script. #!/bin/bash echo $(date) echo "Hello $USER" echo Hostname $(hostname) echo Working in $(pwd) echo Here is this month calender echo $(cal) $ sh first.sh... (7 Replies)
Discussion started by: chandrakanth
7 Replies

2. Shell Programming and Scripting

Long list file display different time format.

Hi Gurus, I have some weird issue. when using ls -l the result shows different time format: -rw-r--r-- 1 abc gourp1 3032605576 Jun 14 2013 abc -rw-rw-r-- 1 abc gourp1 1689948832 Aug 10 06:22 abc one display 2013 which is year; another one displays 06:22 which is time. ... (4 Replies)
Discussion started by: ken6503
4 Replies

3. Shell Programming and Scripting

To display last 5 lines of a file

Hi Guys, I want to echo last 5 lines of a file to a mail. My script getting continuously looped and not getting the output. can anyone help? #!/bin/bash read karthick; tail -5 $karthick; echo $karthick | mail -s "genius" someone@gmail.com Thanks NK (2 Replies)
Discussion started by: Karthick N
2 Replies

4. UNIX for Dummies Questions & Answers

Display only the first two characters of all the lines from a file.

how do i Display only the first two characters of all the lines from a file.? (1 Reply)
Discussion started by: ritusubash
1 Replies

5. Shell Programming and Scripting

extract strings from file and display in csv format

Hello All, I have a file whose data looks something like this I want to extract just the id, name and city fields in a csv format and sort them by id. Output should look like this. 1,psi,zzz 2,beta,pqr 3,theta,xyz 4,alpha,abc 5,gamma,jkl (12 Replies)
Discussion started by: grajp002
12 Replies

6. Shell Programming and Scripting

how to display the output file in an html format using perl

Hi, I have written a perl script to dispaly some statements from a file but i want the output statements to be dispalyed in an HTML format.Is it possible for me to do in perl scripting? Please help me with ur thoughts. Thanks In Advance Meva. (1 Reply)
Discussion started by: meva
1 Replies

7. Shell Programming and Scripting

Script to display record spanning over multiple lines

Following are the lines from /etc/sudoers.conf bob SPARC = (OP) ALL : SGI = (OP) ALL fred ALL = (DB) NOPASSWD: ALL ALL CDROM = NOPASSWD: /sbin/umount /CDROM,\ /sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM Could you please help me with shell/perl script to display the records with... (2 Replies)
Discussion started by: Ujan
2 Replies

8. UNIX for Dummies Questions & Answers

Changing display and format of file

I have an input file which looks like this: 601 a 602 a 603 a 601 b 610 c 615 c 603 d 601 d 612 d I need the utput to look like this 601 a 602 603 602 a 601 603 603 a 601 602 601 b 610 c 615 615 c 610 603 d 601 612 (1 Reply)
Discussion started by: wahi80
1 Replies

9. Shell Programming and Scripting

Display file without # lines

Hi to all in this great forum, im sure this has been asked lots of times before but ive been looking for the past day and cant find the answer. I use cat/some/file to display its contents but how can i get it to not display hashed out lines, or do i need another command, Thanks in advance:) (5 Replies)
Discussion started by: dave123
5 Replies

10. UNIX for Dummies Questions & Answers

display few lines of the file

Hi, If I want to have a look at few lines of the file, how do I, what command to use. Eg: If I have a file having length 2000 lines and I want to have a look at the content between 1400 and 1600, How do I look at it ? Also, If I want to have a look at function alone in a file, how do I go... (4 Replies)
Discussion started by: sharuvman
4 Replies
Login or Register to Ask a Question