Search for a particular file and then cat its contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a particular file and then cat its contents
# 1  
Old 07-27-2014
Search for a particular file and then cat its contents

Hi Friends,

I am looking for a particular file abc* for today's date and then i want to cat its contents to another variable. How to do this. The first step has been accomplished i.e. searching for that file. Now i want to cat the result of this output i.e. abc.txt

Code:
 
 
bash-3.2$ ls -l /folder/abc* -al --time-style=+%D | grep $(date +%D)
-rwxrwxrwx 1 admin admin 401 07/26/14 /folder/abc.txt

# 2  
Old 07-27-2014
@srkmish

Try below one:

Code:
ls -l /folder/abc* -al --time-style=+%D | grep $(date +%D) |awk '{print $7}'| xargs cat

# 3  
Old 07-27-2014
Hey this worked perfectly thanks. Now i want to grep for a string in that file and copy the grepped result into a variable. However, i am not able to do so. What am i doing wrong?

Code:
 
a='ls -l /folder/abc* -al --time-style=+%D | grep $(date +%D) |awk '{print $7}'| xargs cat | grep 'Authentication''
bash: }| xargs cat | grep Authentication: command not found

# 4  
Old 07-27-2014
Srkmish: I'm on vacation and dont have my test machine with me to test the command and give you a perfect one.But I think your using single quote which should be replaced by back quote or $(your command),below is the one may helpful to you :

Code:
a=$(ls -l /folder/abc* -al --time-style=+%D | grep $(date +%D) |awk '{print $7}'| xargs cat | grep 'Authentication')

or
Code:
a=`ls -l /folder/abc* -al --time-style=+%D | grep $(date +%D) |awk '{print $7}'| xargs cat | grep 'Authentication'`

May be helpful to you
# 5  
Old 07-27-2014
Oh yeah, the backquoute works perfect. Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

2. Shell Programming and Scripting

search for a date and print the contents below the line

Hi, We have a script which takes the backup of some files and writes the output into a log file for each run on a daily basis. Following is the extract from the log file. Date:20120917 ********************************************************** * BACKUP ACTIVITY STARTED ... (5 Replies)
Discussion started by: svajhala
5 Replies

3. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

4. UNIX for Dummies Questions & Answers

How to search two strings in a file and print the contents in between to a file

I have a file called po.txt. Here is the content of the file: <!DOCTYPE PurchaseOrderMessage (View Source for full doctype...)> - <PurchaseOrder> - <Header> <MessageId>cdb3062b-685b-4cd5-9633-013186750e10</MessageId> <Timestamp>2011-08-01T13:47:23.536-04:00</Timestamp> </Header> -... (4 Replies)
Discussion started by: webbi
4 Replies

5. UNIX for Dummies Questions & Answers

search contents of a file and use it to name the file

Hi All! I have text files containing data as follows: file1 file2 file3 file4 abc pqr def ghi 76 43 34 3 43 34 3 23 34 3 89 8 i would like to search the contents of all files for the... (3 Replies)
Discussion started by: chen.xiao.po
3 Replies

6. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

7. UNIX for Dummies Questions & Answers

How do i search for the contents of list1 in list2?

hello all, I have 2 files, list1 and list2. They can each have several thousand lines each. I want to check if list2 has any of the contents of list1, perhaps using a grep so that for instance if list1 contained arb1000 and list2 contained arb1000tt It would match this and pipe it... (5 Replies)
Discussion started by: grom
5 Replies

8. Shell Programming and Scripting

Search a pattern in a file with contents in a single line

Hi all I am searching for a pattern in a file . The file content is in a single line.If am doing a grep or sed for the a particular pattern am getting whole file. I want the result in different lines. attaching the file for reference search pattern "/xxxxxx/hhhh/tttttttt/sss/" and... (4 Replies)
Discussion started by: sparks
4 Replies

9. Shell Programming and Scripting

search for the contents in many file and print that file using shell script

hello have a file1 H87I Y788O T347U J23U and file2 J23U U887Y I99U T556U file3 I99O J99T F557J file4 N99I T666U R55Y file5 H87I T347U file6 H77U R556Y E44T file7 Y788O K98U H8I May be using script we can use file1 to search for all the files and have the output H87I file5... (3 Replies)
Discussion started by: cdfd123
3 Replies

10. UNIX for Dummies Questions & Answers

grep/cat/more -- search in a txt file and display content from a specific "keyword"

Hi, I have a .txt file Sample: ===================== NEXT HOST ===================== AEADBAS001 ip access-list extended BLA_Incoming_Filter ip access-list extended BLA_Outgoing_Filter access-list 1 permit xxxxxxxxxxxxxx access-list 2 permit xxxxxxxxxxxxxx =====================... (4 Replies)
Discussion started by: I-1
4 Replies
Login or Register to Ask a Question