ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.