LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-17-2010, 04:33 AM   #1
nikunjbadjatya
Member
 
Registered: Sep 2009
Location: Bangalore
Posts: 33

Rep: Reputation: 0
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.!!
 
Old 02-17-2010, 04:43 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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?
 
Old 02-17-2010, 05:02 AM   #3
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
This code may help you!
Code:
#/usr/bin/perl -w
use strict;
my @files=`ls|grep \".csv.gz\"`;
foreach(@files)
{
s/.*-(2010[0-9]+).*/\1/g;
print;
}
 
Old 02-17-2010, 05:03 AM   #4
nikunjbadjatya
Member
 
Registered: Sep 2009
Location: Bangalore
Posts: 33

Original Poster
Rep: Reputation: 0
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


?????
 
Old 02-17-2010, 05:14 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Code:
for filename in *; do
    timestamp=$(grep -o "2010[0-9]*" $filename)
    echo $timestamp
done
This loops thru all the files in the current directory and extracts the value to assign to timestamp.

Note: In your code, you have this:
Code:
while read filename
do
.
.
done < $no_of_files
This implies that you are trying to get the filenames from a variable "no_of_files". This will not work---the redirection operator is used to specify a file to read from.
 
Old 02-17-2010, 05:17 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
PS: Please put your code in [CODE] tags (Advanced mode--select text and then "#")
 
Old 02-17-2010, 05:41 AM   #7
nikunjbadjatya
Member
 
Registered: Sep 2009
Location: Bangalore
Posts: 33

Original Poster
Rep: Reputation: 0
Hi again,
Thanks alot, It worked with a slight problem..
Code:
for filename in *; do
    timestamp=$(grep -o "2010[0-9]*" $filename)
    echo $timestamp
    cat $filename | while read line
    do
    echo $line
    sed 's/^/$timestamp,/' $line

#will insert $timestamp at the begginnig of every line
    done
done
The timestamp insertion at the begginning of the line using sed isnt working..!!

also,
while read filename
do
.
.
done < $no_of_files
$no_of_files has all the filenames present in a directory.
 
Old 02-17-2010, 05:53 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
To use a variable inside the SED command, you need to use double-quotes:
Code:
 sed "s/^/$timestamp,/" $line
for the redirection issue:

Code:
while read stuff; do
  things
done < $name
This works if the value of the variable "name" is the name of a file in the current directory or is an absolute pathname.
 
Old 02-17-2010, 07:29 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
just use the shell, no need external commands

Code:
declare -a array
for file in 2010*.csv.gz
do
  t=${file##*-}
  t=${t%%.*}
  array=(${array[@]} $t)
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash - awk, sed, grep, ... advice schneidz Programming 13 08-25-2008 09:30 AM
Sed, Awk, grep,Search,delete joyds219 Linux - Newbie 6 04-03-2008 06:15 AM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 12:34 AM
diffrence between grep, sed, awk and egrep Fond_of_Opensource Linux - Newbie 3 08-18-2006 08:15 AM
How can I awk/sed/grep the IPs from the maillog? abefroman Programming 7 03-09-2006 10:22 AM


All times are GMT -5. The time now is 03:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration