help with grep/sed/awk
Hi,
I have a directory containing files of the following pattern: b04-10.232.146.23-20100123000005.csv.gz b04-10.233.146.24-20100123000024.csv.gz ........... ........... I want to copy the pattern 2010* from every filename and store it in a variable. ex. for the above 1st file, i want, 20100123000005 to be stored in some variable. Please help.!! |
You can get the desired number using sed or grep. The regex (regular expression) would be:
2010[0-9]* ## read this as: Literal "2010", followed by any number of digits. For example, try "grep -o "2010[0-9]*" filename Do you want a different variable for each line, or are you going to iterate in a loop and use the variable to do something else? |
This code may help you! :)
Code:
#/usr/bin/perl -w |
Hi,
Yes, I am goin through a loop. basically, the pattern has to matched and stored into variable $timestamp, then this variable has to inserted in each and every line of that file. here's my piece of code: while read filename do echo $filename #The matched pattern 2010* has to be found here #it has to be stored in a variable $timestamp cat $filename | while read line do sed 's/^/$timestamp,/' $line #will insert $timestamp at the begginnig of every line done echo done < $no_of_files ????? |
Code:
for filename in *; do Note: In your code, you have this: Code:
while read filename |
PS: Please put your code in [CODE] tags (Advanced mode--select text and then "#")
|
Hi again,
Thanks alot, It worked with a slight problem.. Code:
for filename in *; do also, while read filename do . . done < $no_of_files $no_of_files has all the filenames present in a directory. |
To use a variable inside the SED command, you need to use double-quotes:
Code:
sed "s/^/$timestamp,/" $line Code:
while read stuff; do |
just use the shell, no need external commands
Code:
declare -a array |
All times are GMT -5. The time now is 12:51 PM. |