How to search the files with the value in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search the files with the value in a variable
# 1  
Old 09-09-2008
How to search the files with the value in a variable

Hi

Am taking a value in a variable and i have to use that in listing files using grep

like
ls -ltr | grep "$variablename" filename...
but the variable contains spaces in it and i am not able to get the correct value(i.e., wid spaces)
if the variable has "day 2"(wid 2 spaces) in it am able to use this, only when it is enclosed in double qoutes.otherwise the value is "Day 2"(wid one space)

Can anyone help me out.....Smilie
# 2  
Old 09-09-2008
Do you mean that you want to be able to find day..2 and day.2 (dots=spaces)?
That means the variable will have to be a regular expression or you will have to have two separate values to grep for like this:
Code:
grep -e 'day 2' -e 'day  2' filename

# 3  
Old 09-09-2008
i am taking out themonth and day from the date command like

d=`date +%b %e`

am getting the result as Sep 9
but i want the value as Sep 9
and i have to take the result into a variable and again use that variable in other command

please help me out....
# 4  
Old 09-09-2008
I'm not understanding - the two Sep 9 in your post above are the same. How are the supposed to be different - exactly.
# 5  
Old 09-09-2008
Get the date as:
Code:
d=`date +%b%e`

# 6  
Old 09-10-2008
Hi

Sorry ..It was mistyped
The first one is Sep 9 (wid one space between Sep and 9)
The second one is Sep 9(wid two spaces)
# 7  
Old 09-10-2008
I think you mean "with", not "wid".

Try this:

Code:
ls -ltr | grep "$(date +'%b *%e')"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk -Search pattern through Variable

Hello, We have wrote shell script for multiple file name search pattern. file format: <numner>_<20180809>.txt starting with single number and ending with 8 digits number Command: awk -v string="12_1234" -v serch="^+_+$" "BEGIN{ if (string ~/serch$/) print string }" If sting matches... (4 Replies)
Discussion started by: koti_rama
4 Replies

2. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

3. Shell Programming and Scripting

awk search using variable

Hi Forum. I have the following script that I would like to use a variable to search using awk but it's not working as expected: # Define working variables token_search_string=536088 token_search_length=16 This code example works by hardcoding 536088 in the string search: awk -v... (10 Replies)
Discussion started by: pchang
10 Replies

4. UNIX for Dummies Questions & Answers

Search and replace with variable

Hello all, I stumbled upon a command line for multiple search and replace within given destination perl -pi -w -e 's/SEARCH_FOR/REPLACE_WITH/g;' *.html I want to replace the following line where the date is the variable, from <div class="meta"> <ul> <li>05.05.2015 with date tags, like... (5 Replies)
Discussion started by: uninuub
5 Replies

5. Shell Programming and Scripting

How to search file for a date variable?

Hello, I'm trying to write a ksh script which will allow me to to search for yesterday's date within a rtf file. I just need someway to know if the date is in the file, echo results to a text file, and then mail that out. The format of the date is mm/dd/yyyy. I had to make a variable... (2 Replies)
Discussion started by: ronan1219
2 Replies

6. Shell Programming and Scripting

search replace with loop and variable

Hi, could anyone help me with this, tried several times but still not getting it right or having enough grounding to do it outside of javascript: Using awk or sed or bash: need to go through a text file using a for next loop, replacing substrings in the file that consist of a potentially multi... (3 Replies)
Discussion started by: wind
3 Replies

7. SuSE

Search all files based on first and in all listed files search the second patterns

Hello Linux Masters, I am not a linux expert therefore i need help from linux gurus. Well i have a requirement where i need to search all files based on first patterns and after seraching all files then serach second pattern in all files which i have extracted based on first pattern.... (1 Reply)
Discussion started by: Black-Linux
1 Replies

8. UNIX for Advanced & Expert Users

Search files with specfic extention and later search content

Hi, I would appriciate if somebody can help me figure out how to search for all the *.xml file under a specific directory and subdirectroies (/home/username) and later search of content "<start>" inside the xml file returned by search. -Lovin.V (2 Replies)
Discussion started by: lovi_v
2 Replies

9. Shell Programming and Scripting

Search in variable for character

qwertyuioplkjhgfdsa (1 Reply)
Discussion started by: rorey_breaker
1 Replies

10. Shell Programming and Scripting

search & replace in variable

Can I use search & replace in any variable? Suppose I have one variable named var1 which holds value "abcabc" I need to search 'a' in var1 and want to replace with 'x' like 'xbcxbc'. Is it possible? Can you provide me an example? Malay (3 Replies)
Discussion started by: malaymaru
3 Replies
Login or Register to Ask a Question